Pages: [1]   Go Down
Author Topic: how to toggle between two subrountines?  (Read 367 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26226
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't enable or disable subroutines, you simply call or not call them.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you give me some example codes? Thanks!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 301
Posts: 26226
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The blink without delay example provided with the IDE is a useful foundation.
Logged

"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.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Pages: [1]   Go Up
Jump to: