// Author : Quentin BENETHUILLERE // Date of creation : 2021/04/09 // Last modification : 2021/04/18 /* * Program function(s): This programs is a puzzle that requests capacitive sensors (made out of copper) to be touched in a specified order to solve it. - when touching any sensor the blue LED blinks. - when sensors have been touched in the expected order, the green LED turns ON for a calibrated amount of time. - when sensors have been touched in an order that is not the expected one, the red LED turns ON for a calibrated amount of time. - Note : in this program it is not possible to perform 2 consecutive touches on the same sensor. - after a calibrated amount of time without any sensor to be touched, the sequence is reinitialiazed. - if several sensors are touched simultaneously, only the first one in the identification order will be considered. */ // ----------- // 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 int TIMER_REINITIALIZATION = 5000; // Timer after which the sequence is reinitialized if no new touch is detected. const int TIMER_BLINK_TOUCH = 300; // Timer for which blue LED is turned on when a touch is detected. const int TIMER_SEQUENCE_COMPLETED = 2000; // Timer for which red or green LED is turned on following a complete sequence of touches detected. const byte sequence_length = 7; // Length of the expected sequence of touches to be performed for solving the puzzle. const byte sequence[sequence_length]={5,1,6,2,3,4,7}; // Expected sequence of touches to be performed for solving the puzzle. const byte nb_sensors=7; // Number of capacitive sensors. // ------------------------------------------------ // PIN connections // ------------------------------------------------ //* Analogic PIN : //* Digital PIN : const byte PIN_red_LED = 9; // Red LED const byte PIN_green_LED = 8; // Green LED const byte PIN_blue_LED = 10; // Blue LED const byte PIN_sensor_emitter = 16; // PIN that sends the signal to measure feedback from capacitive sensors. const byte PIN_sensor_1 = 19; // PIN that measures capacitive sensor 1. const byte PIN_sensor_2 = 3; // PIN that measures capacitive sensor 1. const byte PIN_sensor_3 = 5; // PIN that measures capacitive sensor 1. const byte PIN_sensor_4 = 14; // PIN that measures capacitive sensor 1. const byte PIN_sensor_5 = 18; // PIN that measures capacitive sensor 1. const byte PIN_sensor_6 = 15; // PIN that measures capacitive sensor 1. const byte PIN_sensor_7 = 17; // PIN that measures capacitive sensor 1. // ------------------------------------------------ // Other variables // ------------------------------------------------ byte measured_sequence[sequence_length]; // sequence of sensor touches recorded. byte sequence_position = 0; // variable to determine in which position of the sequence a touch should be registered. long last_touch=0; // variable to determine when the last touch was detected. Used to reinitialize the sequence recorded after a certain amount of time byte correct_sequence=1; // variable to determine whether the sequence of touches recorded matches the expected sequence or not (1=OK, 2=KO). byte sequence_initialized=1; // variable used to control the touches sequence reinitialization. long total=0; // variable to measure the output from a capacitive sensor. // ------------------------- // DECLARATION OF OBJECTS // ------------------------- // The copper plate I use presents 7 different cells (= sensors) : 1 is the top one, 2 is on its right side, etc going clockwise, ..., 7 is the center sensor. CapacitiveSensor cs_1 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_1); // green wire (19/A5) CapacitiveSensor cs_2 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_2); // black wire (3) CapacitiveSensor cs_3 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_3); // blue wire (5) CapacitiveSensor cs_4 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_4); // brown wire (A0/14) CapacitiveSensor cs_5 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_5); // yellow wire (A4/18) CapacitiveSensor cs_6 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_6); // red wire (A1/15) CapacitiveSensor cs_7 = CapacitiveSensor(PIN_sensor_emitter,PIN_sensor_7); // orange wire (A3/17) CapacitiveSensor cs_list[nb_sensors] = {cs_1, cs_2, cs_3, cs_4, cs_5, cs_6, cs_7}; // 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); // HIGH = LED ON / LOW = LED OFF digitalWrite(PIN_green_LED,LOW); // HIGH = LED ON / LOW = LED OFF digitalWrite(PIN_blue_LED,LOW); // HIGH = LED ON / LOW = LED OFF // ------------------------------------------------ // Other // ------------------------------------------------ // Initializing the measured sequence with zeros. for (byte i=0;i=DETECTION_THRESHOLD) && (sequence_position==0 || measured_sequence[sequence_position-1]!=i+1)){ // If allowing 2 consecutive touches on the same sensor : //if (total>=DETECTION_THRESHOLD){ digitalWrite(PIN_blue_LED,HIGH); measured_sequence[sequence_position]=i+1; sequence_position=sequence_position+1; last_touch=millis(); print_sequence(); sequence_initialized=0; delay(TIMER_BLINK_TOUCH); digitalWrite(PIN_blue_LED,LOW); break; } // end if } // end for // When a complete sequence of touches has been detected, a comparison with the expected sequence is performed : if (sequence_position==sequence_length){ for (byte i=0;i=TIMER_REINITIALIZATION) && (sequence_initialized==0))){ for (byte j=0;j