Tuesday, June 23, 2015

Sketch 3 Final Arduino Code

#define ledPin 13 // LED connected to digital pin 13 #define ledPin2 12 #define buttonPin 2 // button on pin 2 #include <CapacitiveSensor.h> int value = LOW; // previous value of the LED int buttonState; // variable to store button state int lastButtonState; // variable to store last button state int blinking; // condition for blinking - timer is timing long interval = 10; // blink interval - change to suit long previousMillis = 0; // variable to store last time LED was updated long startTime ; // start time for stop watch long elapsedTime ; // elapsed time for stop watch int fractional; // variable used to store fractional part of time int isPatting=0; CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); void setup() { cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example cs_4_6.set_CS_AutocaL_Millis(0xFFFFFFFF); Serial.begin(9600); pinMode(ledPin, OUTPUT); // sets the digital pin as output pinMode(ledPin2, OUTPUT); digitalWrite(buttonPin, HIGH); // turn on pullup resistors. Wire button so that press shorts pin to ground. } void loop() { int sensorValue = analogRead(A0); // check for button press long total1 = cs_4_2.capacitiveSensor(30); long total2 = cs_4_6.capacitiveSensor(30); if (total1>100 or total2>100){ buttonState=HIGH; } else if (total1<100 or total2<100){ buttonState=LOW; } if (buttonState == LOW && lastButtonState == HIGH && blinking == false){ // check for a high to low transition // if true then found a new button press while clock is not running - start the clock startTime = millis(); // store the start time blinking = true; // turn on blinking while timing delay(5); // short delay to debounce switch lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time } else if (buttonState == LOW && lastButtonState == HIGH && blinking == true){ // check for a high to low transition // if true then found a new button press while clock is running - stop the clock and report elapsedTime = millis() - startTime; // store elapsed time blinking = false; // turn off blinking, all done timing lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time } else{ lastButtonState = buttonState; // store buttonState in lastButtonState, to compare next time } if(total1>300 && total2>300){ Serial.println(1); delay(150); elapsedTime=0; } else if(elapsedTime<2000 && elapsedTime>0){ if(total1>300){ Serial.println(2); elapsedTime=0; } else if(total2>300){ Serial.println(0); elapsedTime=0; } } if ( (millis() - previousMillis > interval) ) { if (blinking == true){ previousMillis = millis(); // remember the last time we blinked the LED // if the LED is off turn it on and vice-versa. if (total1>300 && total2>300){ value = HIGH; digitalWrite(ledPin, value); digitalWrite(ledPin2, value); } else if (total1>300){ value = HIGH; digitalWrite(ledPin, value); } else if (total2>300){ value = HIGH; digitalWrite(ledPin2, value); } } else{ digitalWrite(ledPin, LOW); // turn off LED when not blinking digitalWrite(ledPin2, LOW); } } }

No comments:

Post a Comment