Hello people. I am a novice with Arduino and would need some help with the following code using the OneButton.h library.
I've been using the Arduino code below created with the help from this forum on my ebike to control the 2 LED bars used as headlights for a while and it is working great.
I would like to add a new function using the attachMultiClick command to have the LEDs flash as follow:
Flash the white LED 3 times with 70ms between flash then pause 100ms then flash the yellow LED 3 times with 70ms between flash, then pause 100ms and loop back to flash the white LED 3 times and so on...
(Similar to a police car flashing light)
How can I make the code for button.attachMultiClick below to loop or is there a better code to make this flash function?
Here is the code for the OneButton.h library:
#include <OneButton.h>
const uint8_t whiteLed_pin = 12;
const uint8_t yellowLed_pin = 11;
const uint8_t button_pin = 10;
OneButton button( button_pin, true, true );
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( []()
{
setLeds( state, !state );
state = !state;
}
);
button.attachDoubleClick( []()
{
setLeds( true, true );
state = true;
}
);
button.attachLongPressStart( []()
{
setLeds( false, false );
state = true;
}
);
}
void loop()
{
button.tick();
button.attachMultiClick( []()
{
digitalWrite(11,HIGH);
delay(70);
digitalWrite(11,LOW);
delay(70);
digitalWrite(11,HIGH);
delay(70);
digitalWrite(11,LOW);
delay(70);
digitalWrite(11,HIGH);
delay(70);
digitalWrite(11,LOW);
delay(100);
digitalWrite(12,HIGH);
delay(70);
digitalWrite(12,LOW);
delay(70);
digitalWrite(12,HIGH);
delay(70);
digitalWrite(12,LOW);
delay(70);
digitalWrite(12,HIGH);
delay(70);
digitalWrite(12,LOW);
delay(70);
}
);
}
Here is the link for my current working code on wokwi simulator without the button.attachMultiClick section:
https://wokwi.com/projects/329108148327547474
Any help would be appreciated.
Cheers