Created
June 11, 2016 16:31
-
-
Save neoxai/742d981c844a16b25991dfab69e516aa to your computer and use it in GitHub Desktop.
Arduino puzzle for holding hands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const short triggerThreshold =50; //Lower is more sensitive | |
#define Relay1 7 | |
//Debound code from: https://www.arduino.cc/en/Tutorial/Debounce | |
int buttonState; // the current reading from the input pin | |
int lastButtonState = LOW; | |
long lastDebounceTime = 0; // the last time the output pin was toggled | |
long debounceDelay = 50; // the debounce time; increase if the output flickers | |
void setup() { | |
pinMode(A0, INPUT); | |
pinMode(Relay1, OUTPUT); | |
delay(1); | |
} | |
void loop() { | |
short value = analogRead(A0); | |
int reading=(value > triggerThreshold); | |
// If the switch changed, due to noise or pressing: | |
if (reading != lastButtonState) { | |
// reset the debouncing timer | |
lastDebounceTime = millis(); | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) { | |
// whatever the reading is at, it's been there for longer | |
// than the debounce delay, so take it as the actual current state: | |
// if the button state has changed: | |
if (reading != buttonState) { | |
buttonState = reading; | |
} | |
} | |
// set the LED: | |
digitalWrite(Relay1, buttonState); | |
//(if this is backwards, use: digitalWrite(Relay1, !buttonState); | |
lastButtonState = reading; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment