Blinking LEDs for a set number of times followed by another action

looking at your attempt, i see a few strange things in the code:

the errors i see:

  • you're trying to blink your leds 3 times, but at the end of the blinking part you put "count = 0", so you can't keep track of how often they blinked yet...

-on the other hand, after lighting a random led you do increase count, but i thought it's the blinking of the 3 leds you're trying to count?

-If the 3 leds blinking are not yet triggered since "millis() - timestamp > delayLength" is false, you'll immediatly go to lighting a random led.

If i read this code, my expectations of the flow would be:

  • you loop the first time.
  • millis() - timestamp > delayLength is false, so you go to the else statement
  • you light a random led (which takes about 3.5 seconds), count is increased to 1
  • you loop the second time
  • millis() - timestamp > delayLength is true (since at least 3.5 seconds have passed due to lighting the random led)
  • count < maxnum is also true, since you only increased count once so far, it's 1 now
  • you light up all 3 leds.
  • count is set to 0
  • you loop the third time
  • millis() - timestamp > delayLength is false, so you go to the else statement
  • you would light a random led (but it's still lit), but will turn it off afterwards, so only 2 leds are lit anymore
    -count is increased to 1 again
  • you loop the 4th time
  • millis() - timestamp > delayLength is true (since at least 3.5 seconds have passed due to lighting the random led)
  • count < maxnum is also true (count is 1)
  • you turn off all the leds again

And this will be repeated forever.

So this indeed exactly described the pattern you described. The arduino is doing exactly what you coded it to do :).

I hope this makes you understand reading your if-else statements a bit better. In the beginning this is very hard.
A good tip would be to read about the serial communication: Serial.println() - Arduino Reference
There are some demo sketches in the IDE that show how to setup the serial communication, and how to print out data.
In your loop, and in your IF & ELSE branches, put some println statements saying for example "i've entered the if statement" , "count = 1", "ledstate is true", etc... You'll get immediate insight into what your program is doing, how it's behaving each loop :).

and btw, sounds like a fun project. very original, and very doable for a novice :).