Skip to content

Instantly share code, notes, and snippets.

@shfitz
Created November 4, 2020 21:48
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 shfitz/8e1f16fc8f42c298cb02fb9db84096f9 to your computer and use it in GitHub Desktop.
Save shfitz/8e1f16fc8f42c298cb02fb9db84096f9 to your computer and use it in GitHub Desktop.
// constants for the pins
const int LEDPin = 2;
const int switchPin = 3;
// variables for the previous switch state and LED
int ledState = LOW;
int lastSwitchVal = LOW;
void setup() {
// configure your pins
pinMode(LEDPin, OUTPUT);
pinMode(switchPin, INPUT);
}
void loop() {
// read the value on the switchPin and store it
// in a variable
int switchVal = digitalRead(switchPin);
// if the button state has changed:
if (switchVal != lastSwitchVal && switchVal == HIGH) {
ledState = !ledState;
}
// set the LED
digitalWrite(LEDPin, ledState);
// save the current reading. Next time through the loop
// it'll be the lastSwitchVal
lastSwitchVal = switchVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment