Society of Robots - Robot Forum

Software => Software => Topic started by: ioannis on February 27, 2008, 10:50:23 AM

Title: How do i time an action?
Post 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?
Title: Re: How do i time an action?
Post by: izua on February 27, 2008, 11:12:41 AM
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.
Title: Re: How do i time an action?
Post by: hazzer123 on February 27, 2008, 11:21:07 AM
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 -

Code: [Select]
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)
Title: Re: How do i time an action?
Post by: izua on February 27, 2008, 12:45:22 PM
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.
Title: Re: How do i time an action?
Post by: Admin on March 09, 2008, 08:51:39 AM
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