Delay Function Question

Can the delay function be a floating number? I know it has to be an unsigned long variable.
Like 1.4045 mSec. Which is too big for the delayMicroseconds function.

No.

Is there a way to achieve this delay accurately?

Thank you for sharing your wisdom with me.

For sub-microsecond precisions like that, you'd need to consider hardware timers in the processor and account for the variable latency between the hardware and your code. It would help to start by using a fast processor.

  • Do you need a 1.4045 millisecond delay or 1.4045 microsecond delay ?

The maximum number for delayMicroseconds() is 16383, 1.4045 milliseconds would be:

delayMicroseconds(1405);

I am working on a stepper motor project. This delay controls the step rate. I need to swing an arm from the home position at 20 Deg to one of two positions of 90 (78 steps) and 180 (178 steps) Deg. With each step being 0.9 Deg. The rotation has 10 different speeds from 0.5 to 0.250 seconds.

The math to determine the delay is>>
delay = (time/step count)/2

Then using the following code to do the actual step>>

do{
        if( Abort_Flag  == true) goto Abort;      
        digitalWrite(Step_Pin, !digitalRead(Step_Pin));
        delayMicroseconds(PulseDelay);
 } while (( count += digitalRead(Step_Pin)) < Num_Steps);

I do not know how to code to use hardware timers in the processor.

I am using an Adafruit ItsyBitsy 32u4 which interfaces with a Rasberry PI app and controls a DRV8825 stepper motor driver

P.S.
I think I found a way to achieve the required delay >>>

void StepPulseDelay(unsigned uint16_t Req_Delay){
  do{
    delayMicroseconds(5);  
  } while((Req_Delay -= 5) >  5)
  return
}

Stepping code >>>

do{
        if( Abort_Flag  == true) goto Abort;      
        digitalWrite(Step_Pin, !digitalRead(Step_Pin));
        StepPulseDelay(PulseDelay);
 } while (( count += digitalRead(Step_Pin)) < Num_Steps);

delayMicroseconds can support times up to 0.016383 seconds per:

....but coding microseconds as per the BlinkWithoutDelay example could handle stepping with periods up to 2^32/1e6=4294 seconds with this completely untested code:

bool stepAsNeeded(unsigned long stepMicros){
   static unsigned long lastMicros =0;
   unsigned long now = micros();
   bool retVal = false;
   if(now - lastMicros > stepMicros){
      lastMicros = now;
      step();
      retVal = true;
   }
   return retVal;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.