// https://wokwi.com/projects/402305903980081153
// https://forum.arduino.cc/t/my-brain-hurts-please-help/1277379
bool animation1Playing;
uint32_t currentMillis;
const int button1Pin = 6;
const int theLED = 7;
void setup() {
pinMode(button1Pin, INPUT_PULLUP);
pinMode(theLED, OUTPUT);
}
void loop() {
currentMillis = millis();
buttons();
if (animation1Playing) digitalWrite(theLED, HIGH);
if (!animation1Playing) digitalWrite(theLED, LOW);
}
# define PRESST LOW // for switches puuled up and wired to ground
void buttons() {
static uint32_t lastMillis;
const uint32_t debounceTime = 20;
static bool lastButton1State;
// too soon to even look?
if (currentMillis - lastMillis < debounceTime) return;
bool currentButton1State = digitalRead(button1Pin) == PRESST; // true is pressed
if (lastButton1State != currentButton1State) {
if (currentButton1State) {
animation1Playing = !animation1Playing;
}
lastButton1State = currentButton1State;
lastMillis = currentMillis; // so we don't even look again until
}
}
/*
bool lampState; // true = LED on
unsigned long now; // millis for the entire loop pass
const int theButton = 6;
const int theLED = 7;
void setup() {
pinMode(theButton, INPUT_PULLUP);
pinMode(theLED, OUTPUT);
}
void loop() {
now = millis();
if (buttonCheck())
lampState = !lampState;
if (lampState) digitalWrite(theLED, HIGH);
if (!lampState) digitalWrite(theLED, LOW);
}
# define PRESST LOW // for switches puuled up and wired to ground
// returns true if the button got pressed
bool buttonCheck() {
static unsigned long lastTime;
const unsigned long debounceTime = 20;
static bool lastButton;
bool buttonPressed = false; // unless proven otherwise
// too soon to even look?
if (now - lastTime < debounceTime) return;
bool thisButton = digitalRead(theButton) == PRESST; // true is pressed
if (lastButton != thisButton) {
if (thisButton)
buttonPressed = true;
lastButton = thisButton;
lastTime = now; // so we don't even look again until
}
return buttonPressed;
}
*/