Using timers to stay in a loop.

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

Not using it right

Not posting any code, either.

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(){ }

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



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.

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

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.

void loop() {
void fgo(){

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:

while (condition)
  // Execute code

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.

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

Jason2548: 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.

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.