Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gallaugher/3df01da70bb81cd8359e2a802814dc82 to your computer and use it in GitHub Desktop.
Save gallaugher/3df01da70bb81cd8359e2a802814dc82 to your computer and use it in GitHub Desktop.
TXPlore Day 12, Learning By Doing 5 - mini project 2 - persistent button press
byte BUTTON_PIN = 7;
byte LED_PIN = 6;
bool newState = LOW;
bool currentState = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
while (digitalRead(BUTTON_PIN) == HIGH) { // If button is pressed
newState = !currentState; // change the newState to the opposite of currentState
digitalWrite(LED_PIN, newState); // and write the newState (HIGH where it was LOW, LOW where it was HIGH)
}
currentState = newState; // This assignment will only be different when button is depressed since state was changed only inside the while loop
digitalWrite(LED_PIN, currentState); // Keep displaying at the currentState
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment