Skip to content

Instantly share code, notes, and snippets.

@fperucic
Last active September 5, 2015 01:14
Show Gist options
  • Save fperucic/bd9d8791371eb83d1797 to your computer and use it in GitHub Desktop.
Save fperucic/bd9d8791371eb83d1797 to your computer and use it in GitHub Desktop.
Arduino button click without delay. Toggle led on button up.
const int buttonPin = 10; // the pin that the pushbutton is attached to
const int ledPin = 9; // the pin that the LED is attached to
int wasClickActive = 0; // state of button click
int ledStatus = 0; // currentLedStatus
void setup() {
pinMode(buttonPin, INPUT); // initialize the button pin as a input:
pinMode(ledPin, OUTPUT); // initialize the LED as an output:
Serial.begin(9600); // initialize serial communication:
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (!wasClickActive) {
wasClickActive = 1;
}
} else {
if (wasClickActive) {
ledStatus = !ledStatus;
wasClickActive = 0;
}
}
digitalWrite(ledPin, ledStatus);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment