Go Down

Topic: how to toggle between two subrountines? (Read 412 times) previous topic - next topic

Richard905

Hi, I am very new to Arduino.
My questions: How to use "Timer" or "Sensor" to toggle between "Subroutine-A" and "Subroutine-B" easily?

1. Enable "Subroutine-A"; Disable "Subroutine-B"
2. After 30 seconds or read Sensor signal, Disable "Subroutine-A"; Enable "Subroutine-B"
3. Do above again and again for 45 min.

Could somebody give me example codes? (user "Timer" or use "sensor" solution, or both)

Thanks very much in advance.



AWOL

You don't enable or disable subroutines, you simply call or not call them.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Richard905

Could you give me some example codes? Thanks!

AWOL

The blink without delay example provided with the IDE is a useful foundation.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH


My questions: How to use "Timer" or "Sensor" to toggle between "Subroutine-A" and "Subroutine-B" easily?


It sounds like a trivial question, but you don't explain what 'enabling' a subroutine means.

If you design your sketch following the approach demonstrated in the 'blink without delay' example then your two subroutines can each be implemented as a function called repeatedly to execute whichever piece of logic you want 'enabled'. In that case you can simply have a global variable that indicates which of the functions to call, and in loop() you will simply test the value of that variable and call one or other function. To change which function gets called you simply update the global variable, and you can do that whenever you want based on whatever conditions you want - such as based on a timer, or based on the state of an input.

I only provide help via the forum - please do not contact me for private consultancy.

Go Up