Skip to content

Instantly share code, notes, and snippets.

@baydam
Created April 2, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baydam/e17b133c2979fdb9b15f54baa37143bb to your computer and use it in GitHub Desktop.
Save baydam/e17b133c2979fdb9b15f54baa37143bb to your computer and use it in GitHub Desktop.
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