Need to start a process if(this is true) countdown timer for that process

I’m adding a switch statement in my sketch. I have a bit of a dilemma though. Here’s what I want to do. Not code just trying to give you an idea of what I’m trying to accomplish.

case 1:(this is running)
if (val = anotherval)
start seconds timer and do something else UNTIL seconds timer = 30
set variable to select case 2 when timer runs out

My code works ok but I the problem is there is a chance that after the if statement is true val will be < anotherval(if statement is false) then the countdown will start over once its true again… I need to find a way to make the counter stay active even if while the counter is running its not true anymore. Then after countdown is finished set it to goto case 2. hope that makes sense.

I’m also wondering how you guys recommend setting up a timer like that?

unsigned long counter;

if (millis() > counter)
counter = counter + 1000

if counter / 1000 = 30//check to see if counter has reached 30 seconds
set variable to goto case 2

Whatcha think?

Have a state 3 that is the counter counting state. When in that state it counts and when the timer expires does whatever you want (move to state 2?).

What you are trying to implement is called a Finite State Machine, or FSM.

Nice, thanks! I'm pooped but will try tomorrow. I need 1-9 steps(per user input) all with different counters(user can input how many seconds) and if statements for each step just like I was describing above.

I try to search but come up short when I'm not sure what to call what I need. Not trying to clog up the forum. That's cool there's actually a name for what I'm trying to do hehe.

Have a good night/day.