I need my bot motors to stop after eight seconds. Searching past messages brings up a number of possibilities, but none that I understand completely. You would save hours of hit and miss by steering me in the right direction. Thanks.
If nothing needs to happen 'at the same time' the simples solution would be:
//run engines
delay( 8000 );
//stop engines
I tried the delay but seems there are other events happening because now there is no response to the sensor. Is a 555 timer a possibility?
I would be glad to see some code.
You can either use the '#' button in the forum 'text editor' when you reply. Or use http://arduino.pastebin.com/
As for wanting multiple things to happen at the same time, have a look at these code snippets/libraries: Arduino Playground - GeneralCodeLibrary
Thanks. The code is in the pastebin http://arduino.pastebin.com/maac2491. I have altered Brandon Sweeney's sketch just a little. My elementary gifted students are designing a game with the robot. We appreciate your help.
When do you need the 8 second pause. From startup, or everytime an event happens?
I need the loop to run only one time and stop. The bot will run in an arena. Students have created a base with colored shapes. The bot must stop after eight seconds on one of these shapes. Students take a question from an appropriate stack of the same color, answer it, and then restart the bot for the next round. So, the motors are running for eight seconds, stop, and wait for manual reset.
If you need it to also be reading sensors whilst moving for 8 seconds then you need an interrupt. The TimerOne library is probably the easiest one to use.
A solution that uses no interrupts:
#define TIME_LIMIT 8000
unsined long start=0;
void setup(){
//do all initialization
start = millis();
}
void loop(){
if( millis() < start + TIME_LIMIT ){
//stop engines
while(true){ ; } //do nothing until reset
}
//your code here
}
Your code is compiling but I probably didn't plug it in correctly because all is working well but the motors don't stop. Please take a look and see where my error is. http://arduino.pastebin.com/m16d34335
[edit]Network bug == double post[/edit]
Magnifico! Far out. Wow! I am ecstatic! The motors stop! My students determine the necessary behavior and expect me to build a robot that acts accordingly. This is the fifth model in eight months of frustration. Each worked but not to their specs. Your timer is the last real hurdle. These are brilliant children. Teaching them how to build and program Arduino will be fun. I am the obstacle; molasses pours faster than I learn electronics. Thanks a million!
New here... what is the purpose of the while(true) inside the if?
Think about that would happen if the while(true) wasn't there - the main loop() would end , and the motors would start running again. With the while(true) thay run once, then enter the while loop, which keeps running forever, and therefore the motors never start running again, at least until the program is reset.
I see, thanks. It is obvious now :-).