Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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