Delay Function

Can someone show me a way to write a function that can delay the robot. I would like to write a function and put it in ‘user_routines.c’ and call it anywhere in my ‘user_routines_fast.c’ file. Simply I would like to:

{
pwm13 = 167;
pwm15 = 167;
delay = 100;
}

where ‘delay =’ would pause the robot for whatever time you gave it. Your help would be appreciated. What would the function look like? Thanks…

maybe somthing like this…


void delay(unsigned int seconds)
{
   unsigned int current = 0;
   while (current < (seconds * 1000) / 26.4) // Seconds * 1000 to get milliseconds and then / 26.4 millisecond loop
   {
      getdata(); // forget what parameter getdata takes
      current++;
      putdata(); // forget what parameter putdata takes..
   }
}

I have no idea if that will work but you can try it…

“Stalling” execution is generally a bad idea. I would make a “loopcounter” variable, and increase the counter every time the function runs. After a certain number of loops, make it do the next thing.

For example, here’s our pump code:


void pump_routine(void) {

	static unsigned int loops = 0;   <--- Loop Counter

	if(BTN_PUMP_DISABLE) {

		if(DIGIN_PUMP_PRESSURE_SWITCH && loops>10) { <-- If more than ten loops have passed

			RLY_PUMP_ON = 0;

		}

		else if(DIGIN_PUMP_PRESSURE_SWITCH) {

			loops++; <-- Increase loop counter

			RLY_PUMP_ON = 1;

		}

		else {

			loops = 0;

			RLY_PUMP_ON = 1;

		}

	}

	else {

		RLY_PUMP_ON = 0;

		loops = 0;

	}

	RLY_PUMP_NUL = 0;

}

i’ve thought about it before, and it turns out something like bear24rw’s idea.

int counter=0;

void User_Autonomous_Code(void)
{
  static unsigned int i;
  const float delay = 100;
  int counter, int limit=(delay*1000)*26.4;

  for(counter=0, counter<limit, counter++)
  {
  }
}

not necessarilly a function, but the interrupt code here (the for loop) should work. all you have to do if place the for loop whereever you want it

… for autonomous no doubt.

Perhaps you will find some insight in this recent thread

I’d suggest structuring your autonomous code to run every 26ms (off the SPI data updates), tuck some timers into that, and structure you code as a state machine.

You need to be carefull with that loop, you need to call putdata and getdata every so often so the master processor doesnt shut down… i forget what the actual timeout is but its not very long…

You do realize that the range for an int is -32,768 to +32,767?