Created Sun, 29 Jun 2014 23:55:37 +0000 by Sanduino
Sun, 29 Jun 2014 23:55:37 +0000
Hello: I'm building a program for controlling a stepper motor driver using a Pololu A4988. I need to generate a microsecond delay dependent engine speed (speedRPS), but do not understand why a variable overflowCount = holdTime_us / 65535 used in the following thread [url]http://forum.arduino.cc/index.php?topic=221017.0[/url]
// A custom delay function used in the run()-method
void holdHalfCylce(double speedRPS) {
long holdTime_us = (long)(1.0 / (double) stepsInFullRound / speedRPS / 2.0 * 1E6);
int overflowCount = holdTime_us / 65535;
for (int i = 0; i < overflowCount; i++) {
delayMicroseconds(65535);
}
delayMicroseconds((unsigned int) holdTime_us);
}
Please, I would like any advice or observations about :) Best Regards Daniel
Mon, 30 Jun 2014 09:36:38 +0000
That stems from the Arduino only having 16-bit ints, and they are used by default by people who don't know better.
A 16 bit int (when unsigned) can hold the values 0 - 65535, so if you want to delay longer than that you need to do it in chunks of 65535, hence the loop of delays of 65535.
The chipKIT boards are 32-bit boards, so they use 32-bit integers. That means that the code you posted is not needed, as you can delay up to 2³²-1 microseconds in one go (that's 4294967295 microseconds).
Mon, 30 Jun 2014 17:51:08 +0000
thanks majenko , I imagined something regards