#include <OneButton.h>
const uint8_t whiteLed_pin = 12;
const uint8_t yellowLed_pin = 11;
const uint8_t button_pin = 10;
const uint8_t no_pin = 255; // this is no GPIO
OneButton button( button_pin, true, true );
enum class Mode {IDLE, FLASH} mode;
void setLeds( const bool white, const bool yellow )
{
Serial.print( "White " );
Serial.print( white == true ? "ON" : "OFF" );
Serial.print( ", Yellow " );
Serial.println( yellow == true ? "ON" : "OFF" );
digitalWrite( whiteLed_pin, white == true ? HIGH : LOW );
delay(10);
digitalWrite( yellowLed_pin, yellow == true ? HIGH : LOW );
}
void setup()
{
Serial.begin( 115200 );
pinMode( whiteLed_pin, OUTPUT );
pinMode( yellowLed_pin, OUTPUT );
setLeds( true, false );
button.setDebounceTicks( 25 );
button.setClickTicks( 130 );
button.setPressTicks( 300 );
static bool state = false;
button.attachClick( []()
{
Serial.println(F("click"));
setLeds( state, !state );
state = !state;
}
);
button.attachDoubleClick( []()
{
Serial.println(F("double click"));
setLeds( true, true );
state = true;
}
);
button.attachLongPressStart( []()
{
Serial.println(F("long press"));
setLeds( false, false );
state = true;
}
);
button.attachMultiClick( []()
{
Serial.println(F("Multi Click"));
digitalWrite(yellowLed_pin, LOW);
digitalWrite(whiteLed_pin, LOW);
if (mode != Mode::FLASH) mode = Mode::FLASH;
else mode = Mode::IDLE;
}
);
}
struct Flashpattern
{
uint8_t pin;
uint8_t interval;
};
Flashpattern flashpattern[] {
{yellowLed_pin, 70},
{no_pin, 70},
{yellowLed_pin, 70},
{no_pin, 70},
{yellowLed_pin, 70},
{no_pin, 70},
{yellowLed_pin, 70},
{no_pin, 270}, // 270ms Delays between color flashes...
{whiteLed_pin, 70},
{no_pin, 70},
{whiteLed_pin, 70},
{no_pin, 70},
{whiteLed_pin, 70},
{no_pin, 70},
{whiteLed_pin, 70},
{no_pin, 270} // 270ms Delays between color flashes...
};
constexpr size_t noOfFlashpattern = sizeof(flashpattern) / sizeof(flashpattern[0]);
void flash()
{
static byte state = 0;
static uint32_t previousMillis = 0;
uint32_t currentMillis = millis();
if (currentMillis - previousMillis >= flashpattern[state].interval)
{
previousMillis = currentMillis;
if (flashpattern[state].pin != no_pin) digitalWrite(flashpattern[state].pin, LOW);
state++;
if (state >= noOfFlashpattern) state = 0;
if (flashpattern[state].pin != no_pin) digitalWrite(flashpattern[state].pin, HIGH);
}
}
void loop()
{
button.tick();
switch (mode)
{
case Mode::FLASH :
flash();
break;
}
}