Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Last active April 10, 2019 15:47
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 Foadsf/b1e9820632d953705fcb49fc75c1f4fb to your computer and use it in GitHub Desktop.
Save Foadsf/b1e9820632d953705fcb49fc75c1f4fb to your computer and use it in GitHub Desktop.
const int buttonPin = 2;
const int ledPin = 7;
int buttonState = 0;
int lastButtonState = buttonState;
bool flag = true;
bool flag_ = flag;
volatile byte state = LOW;
volatile byte state_ = state;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH);
attachInterrupt(digitalPinToInterrupt(buttonPin), changeState, FALLING);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && lastButtonState != buttonState) {
flag = !flag;
}
if (state != state_ || flag != flag_) {
digitalWrite(ledPin, flag ? LOW : HIGH);
}
flag_ = flag;
state_ = state;
lastButtonState = buttonState;
// delay(5);
}
void changeState() {
state = !state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment