Paso 3: código:
#include
#include #include #include #include #include #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET);
#define Red 7 #define Green 8 #define ResetB 6 #define Sensitive 20 #define delayRun 2000 #define blinkDelay 75
#if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif
CapacitiveSensor cs_4_2 = CapacitiveSensor(5, 4); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired CapacitiveSensor cs_4_6 = CapacitiveSensor(3, 2); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
boolean hold = false, runT = false; long holdTime, stopWatch; long total1 ; long total2 ; long calculator, blinkCount = 0, over; int Hours, Minuts, Seconds, Tseconds;
void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example Serial.begin(9600); pinMode (Red, OUTPUT); pinMode (Green, OUTPUT); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); }
void loop() { // *** Read touch-sensors long total1 = cs_4_2.capacitiveSensor(30); long total2 = cs_4_6.capacitiveSensor(30); //display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); display.display(); // *** A touch bouth sides if (total1 >= Sensitive && total2 >= Sensitive && hold == false) { digitalWrite (Green, HIGH); digitalWrite (Red, HIGH); holdTime = millis() + delayRun; // Serial.print ("holdTime = "); display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); display.setCursor(0,30); display.print ("--READY--"); display.display(); hold = true; }
// *** Go green light after delay if (millis() >= holdTime && hold == true) { digitalWrite (Green, HIGH); digitalWrite (Red, LOW); display.clearDisplay(); display.setCursor(10,30); display.print ("** GO **"); display.display(); while (hold == true) { //Serial.println ("hold loop"); // *** Waiting for bouth untouched while (total1 >= Sensitive && total2 >= Sensitive && hold == true) { total1 = cs_4_2.capacitiveSensor(30); total2 = cs_4_6.capacitiveSensor(30); delay (10); } total1 = cs_4_2.capacitiveSensor(30); total2 = cs_4_6.capacitiveSensor(30); // *** Wating for re-touch while (total1 <= Sensitive || total2 <= Sensitive && hold == true) { if (runT == false) { Serial.println (" *** Zeroing ***"); stopWatch = millis() + 10; runT = true; blinkCount = 0; } total1 = cs_4_2.capacitiveSensor(30); total2 = cs_4_6.capacitiveSensor(30); calculator = millis() - stopWatch; //display.clearDisplay(); //display.setCursor(0,0); //display.print (calculator); //display.display(); Hours = int(calculator / 3600000); over = calculator % 3600000; Minuts = int(over / 60000); over = over % 60000; Seconds = int(over / 1000); Tseconds = over % 1000; display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); display.setCursor(0,30); display.print (Hours); display.print (":"); display.print (Minuts); display.print (":"); display.print (Seconds); display.print (":"); display.println (Tseconds); display.display(); if (calculator >= blinkCount + blinkDelay) { Serial.println ("***BlinK***"); digitalWrite (Green, !digitalRead (Green)); blinkCount = calculator; } } if (runT == true) { stopWatch = millis() - stopWatch; Hours = int(stopWatch / 3600000); over = stopWatch % 3600000; Minuts = int(over / 60000); over = over % 60000; Seconds = int(over / 1000); Tseconds = over % 1000; display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); display.setCursor(0,30); display.print (Hours); display.print (":"); display.print (Minuts); display.print (":"); display.print (Seconds); display.print (":"); display.println (Tseconds); display.display(); hold = false; runT = false; digitalWrite (Green, LOW); blinkCount = millis(); while (!digitalRead (ResetB)) { if (millis() >= blinkCount + blinkDelay) { // Serial.println ("***BlinK***"); digitalWrite (Green, !digitalRead (Green)); digitalWrite (Red, !digitalRead (Green)); blinkCount = millis(); } } } } }
// *** A touch only one side if (total1 >= Sensitive && total2 <= Sensitive || total1 <= Sensitive && total2 >= Sensitive) { digitalWrite (Green, LOW); digitalWrite (Red, HIGH); hold = false; display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); display.setCursor(32,30); display.print ("<<<>>>"); display.display(); Serial.print (total1); Serial.print ("\t"); Serial.println (total1); Serial.println ("ONE Touch"); }
//*** No touch at all if (total1 <= Sensitive && total2 <= Sensitive) { digitalWrite (Green, LOW); digitalWrite (Red, LOW); display.clearDisplay(); display.setCursor (0,0); display.print ("SPEEDsTACK"); hold = false; Serial.println ("NO Touch"); }
/* Serial.print(total1); // print sensor output 1 Serial.print("\t"); Serial.println(total2); // print sensor output 2 */ delay(10); // arbitrary delay to limit data to serial port }