Is there a max value how many times for loop can run?
I´m controlling a servomotor with transmission which ratio is 100:1.
My servomotor is controlled as it where a stepper motor. I send pulses for servodriver and motor runs as long there is pulses comming. Direction signal goes in an other line. I can adjust in the servodriver how many pulses correspond to one revolution on motors shaft. The more there is pulses per revolution the smoother the motor runs. And becouse I have such a big ration on transmission I need to take a lot of pulses.
I´M WONDERING WHY I CAN´T SEND MORE THAN ABOUT 32700 PULSES WITH MY ARDUINO. I´M USING THE FOLLOWING CODE.
long pulseNumber = 0; // Number of pulses in pulse train
double frequency = 10; // real frequency is about 17 kHz
double period = (1 / frequency) * 1000;
double dutyCycle = .5; //duty cycle
int on = (dutyCycle * period)/2;
#define fastWrite(pin, state) ( pin < 8 ? (state ? PORTD |= 1 << pin : PORTD &= ~(1 << pin )) : (state ? PORTB |= 1 << (pin -8) : PORTB &= ~(1 << (pin -8) )))
// the macro sets or clears the appropriate bit in port D if the pin is less than 8 or port B if between 8 and 13
void setup() {
pinMode(suuntaPlusOut, OUTPUT);
pinMode(suuntaMiinusOut, OUTPUT);
pinMode(pulssiPlusOut, OUTPUT);
pinMode(pulssiMiinusOut, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
void loop() {
if (Serial.available() > 0) {
tavoiteAsema = Serial.read();
pulseNumber = (absAsema-tavoiteAsema)*128;
absAsema = tavoiteAsema;
if (pulseNumber >= 0)
{
fastWrite(suuntaPlusOut, HIGH); //suuntasignaalit
fastWrite(suuntaMiinusOut, LOW);
for (long i=1; i<=abs(pulseNumber); i++) { //pulssisignaalit
Pulse(on);
}
}
else
{
fastWrite(suuntaPlusOut, LOW); //suuntasignaalit
fastWrite(suuntaMiinusOut, HIGH);
for (long i=1; i<=abs(pulseNumber); i++) { //pulssisignaalit
Pulse(on);
}
}
Serial.flush();
}
}
void Pulse(int on) {
fastWrite(pulssiPlusOut, HIGH); // set Pin high
fastWrite(pulssiMiinusOut, LOW); // set Pin high
delayMicroseconds(on);
fastWrite(pulssiPlusOut, LOW); // set Pin high
fastWrite(pulssiMiinusOut, HIGH); // set Pin high
delayMicroseconds(on);
}