Hello!
I’m searching after the delay command (or the equivalent) in C.
I’m sorry if it has been asked before but I hadn’t found what I need with the “search”).
Thanks allot,
Ben Barchechath.
Hello!
I’m searching after the delay command (or the equivalent) in C.
I’m sorry if it has been asked before but I hadn’t found what I need with the “search”).
Thanks allot,
Ben Barchechath.
EasyC has a “Wait” function, however, the MPLAB C version default code cannot accommodate a lengthly delay. Any substantial delay in that version causes the processor to be disabled.
If you have a specific need we can suggest typical alternatives.
We are using the same switch of the joystick to do two jobs.
I some need kind of delay because when i press the button once the robot doing the open and close command too fast.
And that is why i am searching for a delay of something like two seconds.
P.S.
I’m using the MPLAB.
Thanks.
What is it that you’re actually trying to do? There’s no “wait” command but if you describe what you’re trying to do, perhaps we can find an alternative way to do what you want.
A couple of possible variations come to mind.
You can program the joystick button to act one way when you hold it down and do the opposite when you let it go.
For instance, close when you hold the button and open only when you release the button.
You can program the button to toggle so you have to push it and let go each time you want it to do something.
For instance, push once and let go for close, then push once again to open.
Either of these fit what you are trying to do?
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.
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.
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.
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.
Yes, the Master processor watches our User processor closely. If we don’t talk back to it and miss a couple of communication packets (38 per second) then it shuts us down and displays the red OI “Code Error” LED of death.
A two second delay would be way too long.
If I understand your needs properly, Eric is correct: what you want is a counter. Process_Data_From_Master_uP() is executed about every 26 milliseconds. Check the state of the button in that routine (or in another routine called from it). If the button is pressed, increment the counter; if the button is not pressed, set the counter to zero. If the counter reaches 76, you know the button has been held for two seconds, and you can take some action.
If that doesn’t sound like what you need, please give some more detail about what you want the button to do.