// Author : Quentin BENETHUILLERE // Date of creation : 2021/04/09 // Last modification : 2021/04/21 /* * Program function(s): This programs aims at testing capacitive sensors (in my case simple squares cut in copper sheet). In my situation 3 capacitive sensors are available. - as long as left capacitive sensor is touched : red LED turns ON. - as long as middle capacitive sensor is touched : green LED turns ON. - as long as right capacitive sensor is touched : blue LED turns ON. - Otherwise LEDs are OFF. */ // ----------- // LIBRAIRIES // ----------- #include // ------------------------- // DECLARATION OF DEFINES // ------------------------- // ------------------------- // DECLARATION OF VARIABLES // ------------------------- // ------------------------------------------------ // Adjustable variables (program main parameters) // ------------------------------------------------ const int DETECTION_THRESHOLD = 500; // Threshold above which a touch on a capacitive sensor is considered. Value to be adjusted based on the circuit, and capacitive sensors used. const int CAPACITIVE_MEASUREMENT = 30; // Determines the resolution for the capacitive touch measurement. 30 used in the examples I found. const byte nb_sensors=3; // Number of capacitive sensors. // ------------------------------------------------ // PIN connections // ------------------------------------------------ //* Analogic PIN : //* Digital PIN : const byte PIN_red_LED = 6; // Red LED const byte PIN_green_LED = 7; // Green LED const byte PIN_blue_LED = 8; // Blue LED const byte PIN_sensor_emitter = 2; // PIN that sends the signal to measure feedback from capacitive sensors. const byte PIN_sensor_1 = 3; // PIN that measures capacitive sensor 1. const byte PIN_sensor_2 = 4; // PIN that measures capacitive sensor 2. const byte PIN_sensor_3 = 5; // PIN that measures capacitive sensor 3. // ------------------------------------------------ // Other variables // ------------------------------------------------ byte sequence_position = 0; // variable to determine in which position of the sequence a touch should be registered. long total=0; // variable to measure the output from a capacitive sensor. // ------------------------- // DECLARATION OF OBJECTS // ------------------------- // In this Arduino example, 3 capacitive sensors are used : 1 is the left sensor, 2 is the middle sensor, 3 is the right sensor. CapacitiveSensor cs_1 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_1); // Capacitive Sensor 1 CapacitiveSensor cs_2 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_2); // Capacitive Sensor 2 CapacitiveSensor cs_3 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_3); // Capacitive Sensor 3 CapacitiveSensor cs_list[nb_sensors] = {cs_1, cs_2, cs_3}; // list of the capacitive sensors declared above, for code optimization. // ------------------------- // INITIALIZATION // ------------------------- void setup() { // ------------------------------------------------ // Communication // ------------------------------------------------ Serial.begin(9600); // ------------------------------------------------ // Objects // ------------------------------------------------ // ------------------------------------------------ // Input / Output // ------------------------------------------------ pinMode(PIN_red_LED,OUTPUT); pinMode(PIN_green_LED,OUTPUT); pinMode(PIN_blue_LED,OUTPUT); // ------------------------------------------------ // PIN initial states // ------------------------------------------------ digitalWrite(PIN_red_LED,LOW); digitalWrite(PIN_green_LED,LOW); digitalWrite(PIN_blue_LED,LOW); // ------------------------------------------------ // Other // ------------------------------------------------ } // end setup function void loop() { for (byte i=0; i=DETECTION_THRESHOLD){ digitalWrite(PIN_red_LED,HIGH); } // end if else{ digitalWrite(PIN_red_LED,LOW); } // end else break; case 1: if (total>=DETECTION_THRESHOLD){ digitalWrite(PIN_green_LED,HIGH); } // end if else{ digitalWrite(PIN_green_LED,LOW); } // end else break; case 2: if (total>=DETECTION_THRESHOLD){ digitalWrite(PIN_blue_LED,HIGH); } // end if else{ digitalWrite(PIN_blue_LED,LOW); } // end else break; } // end switch } // end for } // end loop function