// https://wokwi.com/projects/374000220931822593
// https://forum.arduino.cc/t/single-button-input-controlling-multiple-outputs/1161323
# define led1 8
# define led2 9
# define theButton 2
# define debounceTime 25
unsigned char buttonState;
unsigned long buttonTimer;
unsigned char buttonCycle;
# define timeoutTime 3777
unsigned long timeoutTimer;
# define PRESST LOW
# define ON HIGH
# define OFF LOW
# define heartLED 13
# define heartBit 0x80
void setup()
{
Serial.begin(115200);
Serial.println("Jello Whirled!\n");
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode (theButton, INPUT_PULLUP);
buttonState = digitalRead(theButton) == PRESST;
}
void loop()
{
unsigned long now = millis();
digitalWrite(heartLED, now & 0x200 ? HIGH : LOW);
switch (buttonCycle) {
case 0:
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
break;
case 1:
digitalWrite (led2, HIGH);
break;
case 2:
digitalWrite (led1, HIGH);
digitalWrite (led2, HIGH);
break;
}
if (now - timeoutTimer > timeoutTime && buttonCycle == 2) {
buttonCycle = 0;
timeoutTimer = now; // don't really need to, cuts spam though.
}
// too soon to even look at button again?
if (now - buttonTimer < debounceTime) return;
// no let's look to see if it got pressed
unsigned char button = digitalRead(theButton) == PRESST;
if (buttonState != button) {
buttonState = button;
buttonTimer = now; // haha, is also timeoutTimer, but
timeoutTimer = now;
// button got pressed!
if (button) {
buttonCycle++;
if (buttonCycle >= 3) buttonCycle = 0;
}
}
}