Go Down

Topic: Have a loop run for X amount of time (Read 3 times) previous topic - next topic

toedip

Hi,

I am trying to make a program to control some LED's. There are two inputs. English and French. Each input also has an LED. Before anyone has pushed a button, the LED's will flash alternately. Once someone pushes the a button, that button will stay illuminated for X amount of time. Then go back to alternate flashing.

My code is a bit messy right now, but I'm looking for advice on concept. Is it possible to:

Run a loop until digitalRead detects a HIGH.
Then - digitalRead runs if statement loop. That loop will run for X amount of seconds. Then return to main loop.
I cannot use delay commands since I need it to always be open to input even during the timed loop. Ie. While running the second loop, pushing a button can restart that loop.

Biggest thing I'm looking for is how to run an if statement for X amount of time, then to go back to the main loop.

Hope that is clear.

Thank you!


Arrch


Run a loop until digitalRead detects a HIGH.

Yes.
Quote
Then - digitalRead runs if statement loop. That loop will run for X amount of seconds. Then return to main loop.
I cannot use delay commands since I need it to always be open to input even during the timed loop. Ie. While running the second loop, pushing a button can restart that loop.

So you need a counter that gets sets to 0. Every time it blinks, counter gets incremented. Your blink code will check how many times it's blinked to determine if it needs to blink again.

Sounds like you're looking for a state machine.

toedip

Hey,

Thanks for the reply.

I'm not familiar with the counter part. I'll do some research.

Is this not possible with a millis variety?

If input = high, run loop for x amount of time until time runs out or input = high again.

Seems simple, haha.

Thanks

Arrch


Is this not possible with a millis variety?

If input = high, run loop for x amount of time until time runs out or input = high again.

Seems simple, haha.

Thanks


It's entirely possible, but if you can't use delay, then you need to start getting away from calling it a loop. It's a state machine, which is If the state is this, do this, otherwise if it's that, do that, etc.. Having the LED blink as one of the states is trivial, you just need to determine the conditions in which the LED will start/stop blinking and change the state when those happens.

toedip

Hey,

It's good to clear my terminalogy up. Thanks,

I guess my trouble is how to tell one LED to stay high, and the other low for a certain time frame once the state has been requested to change.

Go Up