Created
April 2, 2016 19:00
-
-
Save baydam/e17b133c2979fdb9b15f54baa37143bb to your computer and use it in GitHub Desktop.
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 unsigned int leftLED = 3; // LED on left side | |
const unsigned int leftButton = 2; // Button on left side | |
const unsigned int rightLED = 10; // LED on right side | |
const unsigned int rightButton = 11; // Button on right side | |
int ledState = LOW; | |
int buttonState; | |
int previousState = HIGH; | |
long lastDebounceTime = 0; | |
long debounceDelay = 50; | |
int flashState = LOW; | |
unsigned long previousMillis = 0; | |
void setup() { | |
pinMode(leftLED, OUTPUT); | |
pinMode(leftButton, INPUT); | |
pinMode(rightLED, OUTPUT); | |
pinMode(rightButton, INPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
int leftValue = debounce(leftButton); | |
int rightValue = debounce(rightButton); | |
if (leftValue == HIGH) | |
flash(leftLED); | |
else | |
digitalWrite(leftLED, LOW); | |
if (rightValue == HIGH) | |
flash(rightLED); | |
} | |
int debounce(int currentButton) { | |
int reading = digitalRead(currentButton); | |
if (reading != previousState) | |
lastDebounceTime = millis(); | |
if (millis() - lastDebounceTime > debounceDelay) { | |
if (reading != buttonState) { | |
buttonState = reading; | |
if (buttonState == HIGH) { | |
ledState = !ledState; | |
} | |
} | |
} | |
previousState = reading; | |
Serial.println(ledState); | |
return ledState; | |
} | |
void flash(int side) { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= 300) { | |
previousMillis = currentMillis; | |
flashState = (flashState == LOW) ? HIGH : LOW; | |
digitalWrite(side, flashState); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment