Society of Robots - Robot Forum
Software => Software => Topic started by: ioannis on February 27, 2008, 10:50:23 AM
-
How do i go about timing an action? for example i want the robot to go back for 2 seconds and then carry on its usual actions?
I am using the Atmega8
Can anyone help?
-
Figure out how many clock cycles are wasted in 2 seconds, than write a program to waste them (piclist.org had a generator for pic and atmel).
Then, order your motors to go backwards, and run the code to waste 2 seconds.
-
Use hardware timers.
A timer counts up every cycles of the MCU, and then when it loops round from the highest number to 0, it will interrupt the current processes of the MCU, to carryout an interrupt routine. This can be used to create a 'clock' variable.
Eg you could get the timer to interrupt every millisecond, and when it interrupts, increment the clock variable.
Then you just do something like -
pseudo code
resetClock();
while (clock < 2000 milliseconds) {
go backwards
}
Heres a page that might help - http://dlangenberg.googlepages.com/atmega8timers (http://dlangenberg.googlepages.com/atmega8timers)
-
of course, using timers - as hazzer123 said - is a better method, since no cycles are wasted - you can do obstacle avoidance, terrain map or any other computation as needed, but you will need a better understanding of your mcu.
-
or if you are using the $50 Robot and do not care about wasted cycles, do this:
delay_cycles(65500);//add a few of these till you get the delay you want