#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
const Button button1( 2 );
const Button button2( 3 );
const uint8_t leds_pins[] = { 12, 11, 10, 9, 8 };
const size_t leds_count = sizeof( leds_pins ) / sizeof( leds_pins[0] );
void setup()
{
Serial.begin( 115200 );
button1.begin();
button2.begin();
for ( uint8_t i = 0; i < leds_count; ++i )
{
pinMode( leds_pins[i], OUTPUT );
digitalWrite( leds_pins[i], LOW );
}
}
void loop()
{
button1.read();
button2.read();
static uint8_t counter = 0;
if ( counter < leds_count && button1.wasReleased() )
{
++counter;
Serial.print( "Compteur = ");
Serial.println( counter );
}
if ( button2.wasReleased() )
{
static uint8_t previous_counter = 0;
for ( uint8_t i = 0; i < previous_counter; ++i )
{
Serial.print( "LED " );
Serial.print( i + 1 );
Serial.println( " off" );
digitalWrite( leds_pins[i], LOW );
}
previous_counter = counter;
if ( counter > 0 )
{
Serial.println( "Attente de 3 secondes..." );
// 2 secondes maintenant, et 1 seconde au début de la boucle for,
// comme ça il n'y a pas de délai après la dernière LED
delay( 2000 );
for ( uint8_t i = 0; i < counter; ++i )
{
delay( 1000 );
Serial.print( "LED " );
Serial.print( i + 1 );
Serial.println( " on" );
digitalWrite( leds_pins[i], HIGH );
}
Serial.println( "Cycle terminé" );
counter = 0;
}
}
}