void setup()
{
Serial.begin( 115200 );
}
void loop()
{
enum sms : uint8_t
{
sms_start,
sms_beep,
sms_wait,
sms_done
};
const uint32_t timestamp_actual = millis();
static uint32_t timestamp_previous = 0;
static uint32_t beep_timer = 0;
static sms state = sms_start;
switch ( state )
{
case sms_start :
{
Serial.println( "start" );
timestamp_previous = 0;
beep_timer = 1000;
state = sms_beep;
break;
}
case sms_beep :
{
if ( timestamp_previous == 0 || timestamp_actual - timestamp_previous >= beep_timer )
{
timestamp_previous = timestamp_actual;
tone(2, 2500);
digitalWrite( LED_BUILTIN, HIGH );
Serial.println( beep_timer );
beep_timer -= 15;
state = sms_wait;
}
break;
}
case sms_wait :
{
if ( timestamp_actual - timestamp_previous >= 100 )
{
timestamp_previous = timestamp_actual;
digitalWrite( LED_BUILTIN, LOW );
noTone(2);
state = beep_timer > 46 ? sms_beep : sms_done;
}
break;
}
case sms_done :
{
Serial.println( "done" );
state = sms_start;
break;
}
default :
{
Serial.println( "error" );
break;
}
}
}