Adding timer in a case statement


What I'm trying to do is when the sketch goes to CASE1 I want to basically start a timer and for every second passed the variable 'counter' will have 1 added to it.

To finish it off I want to compare 'counter' variable to another number then proceed after that.

case CASE1:

if(currentMillis - previousMillis > 1000) {
     previousMillis = currentMillis;
     counter = counter + 1;
if (counter = 30){
state = CASE2

Its not working like that though. Any ideas?

if (counter = 30){ state = CASE2 } break;

Try if (counter == 30)

Thanks for reply. Same problem though. I'm outputting counter to lcd and it adds 1 to it then goes goes onto next case.

It is working thanks! My bad haha!

You probably need to reset the counter variable to zero, after it has reached 30, if you want this behaviour to repeat after another 30 seconds.

You might also want to check if one of the statements is missing a semi-colon.


I actually did reset the counter to 0 and all that. I didn't copy all the code. Probably would've helped.

It was just the two == that did it. Why is that? I need to start from ground up....

Anyone wanna see a video of what I'm doing here?

Look here

Thanks. :)