Skip to content

Instantly share code, notes, and snippets.

@eduardobc88
Created July 25, 2021 21:42
Show Gist options
  • Save eduardobc88/e027bd68811b21a6906a71d878ff6a78 to your computer and use it in GitHub Desktop.
Save eduardobc88/e027bd68811b21a6906a71d878ff6a78 to your computer and use it in GitHub Desktop.
// NOTE: 2- BUTTON PUTS LED ON
int pinLed = 3;
int pinButton = 2;
int buttonState = 0;
int pressCount = 0;
bool isFirstPress = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pinLed, OUTPUT);
pinMode(pinButton, INPUT);
Serial.println("== SETUP - BUTTON PUTS LED ON ==");
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(pinButton);
if (buttonState == HIGH) {
if (isFirstPress == false)
isFirstPress = true;
if (isFirstPress)
if (pressCount == 0)
pressCount = 1;
else if (pressCount == 2)
pressCount = 3;
}
if (buttonState == LOW)
if (isFirstPress)
if (pressCount == 1)
pressCount = 2;
else if (pressCount == 3) {
isFirstPress = false;
pressCount = 0;
}
if (pressCount == 1 || pressCount == 2)
digitalWrite(pinLed, HIGH);
else
digitalWrite(pinLed, LOW);
// Serial.println(pressCount); // NOTE: uncomment this for show messages in console
}
@eduardobc88
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment