const uint8_t ledPin = LED_BUILTIN;
void setup()
{
Serial.begin( 115200 );
Serial.println( "LED Thing is ready" );
pinMode( ledPin, OUTPUT );
digitalWrite( ledPin, LOW );
randomSeed( analogRead( A0 ) );
}
void loop()
{
uint32_t now = millis();
static uint32_t past = now;
static uint32_t timer = 0;
static uint8_t state = 0;
static uint8_t blinks = 0;
if ( state == 0 )
{
timer = random( 1000, 5001 );
++state;
Serial.print( "Waiting " );
Serial.print( timer );
Serial.println( " ms..." );
}
else if ( state == 1 && now - past >= timer )
{
timer = 200; //using 200ms so we can see the LED blinking
uint32_t rnd = random( 1000, 5001 );
blinks = rnd / ( timer * 2 );
++state;
Serial.print( "Blinking LED for ~" );
Serial.print( rnd );
Serial.print( " ms (" );
Serial.print( blinks );
Serial.println( " blinks)..." );
}
else if ( state == 2 && now - past >= timer )
{
past = now;
static bool ledState = false;
ledState = !ledState;
digitalWrite( ledPin, ledState ? HIGH : LOW );
Serial.print( "LED is " );
Serial.println( ledState ? "on" : "off" );
if ( ledState == false && --blinks == 0 )
{
state = 0;
Serial.println( "Done, return to state 0" );
}
}
}