Created
November 4, 2020 21:48
-
-
Save shfitz/8e1f16fc8f42c298cb02fb9db84096f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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