//LEDs #define pin_led_b 0 #define pin_led_r 1 int brightness = 0; //Magnetism #define pin_hall 3 int sensor_value_magnet = 0; //External switch #define pin_switch_1 6 #define pin_switch_2 7 int sensor_value_switch_1 = 0; int sensor_value_switch_2 = 0; //Capacitive touch #define pin_capacitive_1 10 #define pin_capacitive_2 8 #define pin_capacitive_send 9 int sensor_value_capacitive_1 = 0; int sensor_value_capacitive_2 = 0; //To smooth out the readings const int numReadings = 10; int readings[2][numReadings]; // the readings from the analog input int readIndex[] = {0, 0}; // the index of the current reading int total[] = {0, 0}; // the running total int counter = 0; /////////////////////// SETUP ////////////////////////// void setup() { Serial.begin(9600); //LEDs pinMode(pin_led_b, OUTPUT); pinMode(pin_led_r, OUTPUT); //Switch sensor pinMode(pin_switch_1, INPUT_PULLUP); pinMode(pin_switch_2, INPUT_PULLUP); //Capacitive touch pinMode(pin_capacitive_1, INPUT); pinMode(pin_capacitive_2, INPUT); pinMode(pin_capacitive_send, OUTPUT); //Initialize all the readings to 0 for (int i = 0; i < numReadings; i++) { for (int j = 0; j < 2; j++) { readings[j][i] = 0; }//for j }//for i }//void setup /////////////////////// LOOP /////////////////////////// void loop() { //Get the value from the switch sensor -> 1 = no connection sensor_value_switch_1 = digitalRead(pin_switch_1); sensor_value_switch_2 = digitalRead(pin_switch_2); //Check if switch picked up switchSensor(sensor_value_switch_1, sensor_value_switch_2); //If no switch picked up if(sensor_value_switch_1 == 1 && sensor_value_switch_2 == 1) { //If Hall effect measures something sensor_value_magnet = analogRead(pin_hall); if(sensor_value_magnet <= 506 || sensor_value_magnet >= 512) { magnetismSensor(sensor_value_magnet); delay(30); } else { //Otherwise check for capacitive touch //Sensor 1 sensor_value_capacitive_1 = capacitiveTouchSensor(pin_capacitive_1); int average_1 = sensorAverage(0, sensor_value_capacitive_1); //Sensor 2 sensor_value_capacitive_2 = capacitiveTouchSensor(pin_capacitive_2); int average_2 = sensorAverage(1, sensor_value_capacitive_2); //Adjust brightness of LEDs based on values measured if(average_1 > 115 || average_2 > 115) { //Only print out every 10 loops if(counter%10 == 0) { Serial.print("Sensor 1: "); Serial.print(average_1); Serial.print("\t"); Serial.print("Sensor 2: "); Serial.println(average_2); }//if //The max values could potentially change depending on humidity, temperature and much more, //so they might need to be updated per usage int brightness_1 = map(average_1, min(average_1, 115), max(average_1, 200), 0, 255); int brightness_2 = map(average_2, min(average_2, 115), max(average_2, 200), 0, 255); analogWrite(pin_led_b, brightness_1); analogWrite(pin_led_r, brightness_2); } else { digitalWrite(pin_led_r, LOW); digitalWrite(pin_led_b, LOW); }//else delay(10); }//else } else { //External switch detected delay(100); }//else counter++; }//void loop ///////////////// External Switch ///////////////// void switchSensor(int sensor_1, int sensor_2) { if(sensor_1 == 0 && sensor_2 == 0) { digitalWrite(pin_led_r, HIGH); digitalWrite(pin_led_b, HIGH); Serial.println("Switch 1 and 2 are on"); } else if (sensor_1 == 0 && sensor_2 == 1) { digitalWrite(pin_led_r, HIGH); digitalWrite(pin_led_b, LOW); Serial.println("Switch 1 is on"); } else if (sensor_1 == 1 && sensor_2 == 0) { digitalWrite(pin_led_r, LOW); digitalWrite(pin_led_b, HIGH); Serial.println("Switch 2 is on"); } else if (sensor_1 == 1 && sensor_2 == 1) { digitalWrite(pin_led_r, LOW); digitalWrite(pin_led_b, LOW); }//else }//void switchSensor ///////////////// Magnetism Sensor ///////////////// void magnetismSensor(int sensor) { Serial.print("Magnetism: "); Serial.println(sensor); if(sensor_value_magnet >= 512) { //Adjust the brightness of the red led brightness = map(sensor, 500, max(sensor, 800), 0, 255); analogWrite(pin_led_r, brightness); //Turn off the blue led digitalWrite(pin_led_b, LOW); } else if (sensor <= 506) { //Adjust the brightness of the blue led brightness = map(sensor, min(sensor, 200), 500, 255, 0); analogWrite(pin_led_b, brightness); //Turn off the red led digitalWrite(pin_led_r, LOW); }//else if }//void magnetismSensor ///////////////// Capactive Touch ///////////////// int capacitiveTouchSensor(int pin_id) { digitalWrite(pin_capacitive_send, LOW); delay(10); long start_time = micros(); digitalWrite(pin_capacitive_send, HIGH); while(digitalRead(pin_id) == LOW); int sensor_value = micros() - start_time; return sensor_value; }//int capacitiveTouchSensor //Average the capacitive touch values int sensorAverage(int index, int value) { // subtract the last reading: total[index] = total[index] - readings[index][readIndex[index]]; // read from the sensor: readings[index][readIndex[index]] = value; // add the reading to the total: total[index] = total[index] + readings[index][readIndex[index]]; // advance to the next position in the array: readIndex[index] = readIndex[index] + 1; // if we're at the end of the array wrap around to the beginning: if (readIndex[index] >= numReadings) { readIndex[index] = 0; }//if // calculate the average: int average = total[index] / numReadings; return average; }//int sensorAverage