Need to delay the start of my program

Hi Guys,
I am working on a Sumo bot, and if you have looked at them before, most rules require a 5 second pause before the battle begins.

I have all systems go with my bot, until I try to add in my delay for the start.

If I write it

{delay(5000);
{goFor();
   
 randNumber = random(400, 600);
 valfl = analogRead(irfl);  
 valfr = analogRead(irfr);
 irrval = analogRead(irr);
 irlval = analogRead(irl);

 goFor();
 if (valfl > 400)
 left2();
 if (valfr > 400)
 right2();
 if (irlval >750)
 left();
 if (irrval >750)
 right();
}
}

The pause works properly, but my program doesn't work. The bot locks into goFor(); and never comes out.

If I write it

{delay(5000);
 goFor();
   
 randNumber = random(400, 600);
 valfl = analogRead(irfl);  
 valfr = analogRead(irfr);
 irrval = analogRead(irr);
 irlval = analogRead(irl);

 goFor();
 if (valfl > 400)
 left2();
 if (valfr > 400)
 right2();
 if (irlval >750)
 left();
 if (irrval >750)
 right();
}

This way loops the pause in every time.

If I write it

{del();
 goFor();
   
 randNumber = random(400, 600);
 valfl = analogRead(irfl);  
 valfr = analogRead(irfr);
 irrval = analogRead(irr);
 irlval = analogRead(irl);

 goFor();
 if (valfl > 400)
 left2();
 if (valfr > 400)
 right2();
 if (irlval >750)
 left();
 if (irrval >750)
 right();
}
void del()
{delay(5000)}

This way locks into the del void, and doesn't come out.

I don't know what else to do. Any ideas on what I should do? I have been banging my head against the wall on this. Is there a different type of delay that I can try? Or possibly some sort of interrupt? Thanks for any ideas. :smiley:

How big is the program?

Not very big. It's not a size issue. 117 lines

I don't know if I mentioned, the power switch is a button, so I would like to just have the delay run anytime the power is turned on. But only delay once per power cycle.

Thanks!

Try like this:

void setup(){
  delay(5000);
  //other setup code
}

void loop(){
  //loop code
}

:slight_smile:

Not a stellar programmer, but normally when I want a delay at the beginning I'll start by making a var of something like "Should_I_wait" with a value of 1, then
if Should_I_Wait then
delay(5000)
should_i_wait = should_i_wait - 1

delay() in the void setup was the trick!!! Thanks so much! ;D I knew it was something right in front of me. Just couldn't stick a pin on it.

I thought I knew that name. AlphaBeta you always seem to know the right things to code. You helped me a while back fix my code for a Xbox 360 Steering wheel shift indicator project. Thank you for being such a great help!