Pages: [1]   Go Down
Author Topic: Using timers to stay in a loop.  (Read 715 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was wondering how to keep my code running in a subroutine for a certain period of time.  And in that time if switch1 is not pressed then I need it to go to another part of the code?  Please help.  I tried to use the blink without delay.  Not using it right
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not using it right
Not posting any code, either.
Logged

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

I would put something in here.  but I don't have anything.  I have a code started but I have been trying to figure out how to keep it running in a loop for a certain amount of time until something else happens.  I will post what I have but it won't help much. Its simple.  Just looking at sensors and two switches.  but in the first sub routine.  I want it to stay there for a few seconds while it waits to see if S1 is tripped hi.  If not then move on to what I need it to do.

void setup() {
Switch1.attach(13);
Switch2.attach(12);
pinMode(iled1,OUTPUT);

pinMode(s1,INPUT);
pinMode(s2,INPUT);
pinMode(s3,INPUT);
pinMode(s4,INPUT);
pinMode(s5,INPUT);
 

}

void loop() {
  if(digitalRead(s1)==HIGH && digitalRead(s2)==LOW && digitalRead(s4)==LOW){
 fgo();
 }

}
//-------------------------------------------------------
void fgo(){

digitalWrite(iled1,HIGH);
Switch1.write(pos2);
if(digitalRead(s1)==LOW && digitalRead(s2)==HIGH && digitalRead(s4)==HIGH){
 loop();
 }

}
//---------------------------------------------------------
void fgo1(){


}
//-----------------------------------------------------------
void fgo2(){


//-----------------------------------------------------------
void fgo3(){
}
Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2221
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The trick is not to say in the loop! You (almost) never need to "say in a loop".

Mark
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Switch1.attach(13);
Switch2.attach(12);
What are Switch1 and Switch2? The only class I know of with an attach() method is the Servo class. Naming a Servo instance Switch1 doesn't make sense.
Logged

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

They are servos.  They are servos running mechanical switches on a train track
Logged

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

With that.  What I have is sensors that are below a train track that let me know when trains are getting closer to a switch.  Then depending on what sensors are high determins what happens with the switch and lights.  Right now I would like it so that the train enters a track and waits to see if another train approaches if not then it moves onto the main track and continues on its merry way.  If I can get the timer right then I could use it in other junctions of the train tracks.

Also can you make it so that it reads all the digital inputs easier without writing a paragraph.
« Last Edit: January 30, 2013, 03:19:15 pm by Jason2548 » Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop() {
  ...
 fgo();
}
//-------------------------------------------------------
void fgo(){
...
 loop();
}

What you have here is recursion and I'm certain that's not what you are looking to do. I'm guessing that you see these function calls as more of a "go to this part of the code", which simply isn't the case.

If you want to stay within a certain portion of code until a condition is met, you need a while loop:

Code:
while (condition)
{
  // Execute code
}
Logged

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

I understand, I thought about that.  How can I stay in that condition under a certain amount of time.  If that limit is reached then the code needs to move on to the next part.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/blink
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make your functions do actions. When the function finishes, execution flow returns on the next line where the function was originally called from. Then in loop() you just call one of the functions based on your if conditions. You can add a delay() call in there to slow down the process, or you need to study the blinking without delay example better. If you have any kind of user interface with buttons, you are bound to end up with the "without delay" example and maybe even "debouncing".
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand, I thought about that.  How can I stay in that condition under a certain amount of time.  If that limit is reached then the code needs to move on to the next part.

The Blink Without Delay uses an if statement to check if it has been a certain time. Using a while instead will cause the code to get "stuck" in that part of the code until the condition is no longer met.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of trying to "stay in the code" for a certain time, you need to redesign. Read the link that I posted above.

When you are cooking dinner, you don't get fixated in waiting for the roast to cook, and do absolutely nothing else, do you? You might set the table while it is cooking, and pour yourself a wine. Same idea.
Logged

Pages: [1]   Go Up
Jump to: