In: Computer Science
FOR ARDUINO PROGRAMMING
NEED TO HAVE ALL LEDS FLASH 5 TIMES WHEN THE BOARD STARTS.
I think you want the flashes to stop after 5 on/off repetitions or to have 5 seconds between the flash sequences.
You need to have a variable to count the number of flashes and only increment that variable when a flash actually happens. When the 5 flashes are completed your code will know not to flash anymore.
const byte ledPin = 13;
byte flashCount = 0;
byte state = 0;
unsigned long waitStart;
unsigned long shortPeriod = 500;
unsigned long longPeriod = 5000;
unsigned long currentTime;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
waitStart = millis();
}
void loop()
{
switch (state)
{
case 0:
currentTime = millis();
if (currentTime - waitStart >= shortPeriod)
{
digitalWrite(ledPin, !digitalRead(ledPin));
waitStart = currentTime;
flashCount++;
if (flashCount == 10)
{
state = 1;
}
}
break;
case 1:
currentTime = millis();
if (currentTime - waitStart >= longPeriod)
{
waitStart = currentTime;
flashCount = 0;
state = 0;
}
break;
}
}