Thread: Commands
View Single Post
  #7   Spotlight this post!  
Unread 05-02-2007, 15:35
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
Re: Commands

There are two different types of delays: blocking and non blocking. A blocking delay halts execution of EVERYTHING for that amount of time. A non-blocking delay only affects a small portion of the code.

I believe you are looking for a non-blocking delay. I would suggest adding a counter to your code that increments at the top of the control loop.

Code:
time_counter+=1;
if(button_pressed && time_counter>desired_delay)
    do stuff;
    time_counter=0;

rest of code
desired_delay would be equal to the amount of time you want to delay in seconds divided by the frequency of your control loop. I think that frequency is 40Hz, but please someone correct me. Therefore, desired_delay = 2seconds / 40 Hz = 80.

If you are looking for a blocking delay, you have several options, two of which I have outlined below.


The simple (read "simple"" as "bad") way is to simply spin in a loop.

Code:
for(i=0;i<DELAY_TIME;i++);
This is bad for many reasons, but is useable as a quick and dirty solution for small delay times, as would be seen for hacking out a bad bit banger for serial comm.

Quote:
Originally Posted by Mark McLeod View Post
Any substantial delay in that version causes the processor to be disabled.
I am guessing that this means that there is a watchdog that is timing out. If you really want to, you could delay, pet the watchdog, delay some more, pet him again, delay more, etc. When you get to this point, you are probably doing something wrong.