Is there a max value how many times for loop can r

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);
}

I´M USING THE FOLLOWING CODE

No, you aren't. I can tell from a quick scan there are (potentially important) pieces missing.

PLEASE...

  • Use the CODE attribute when posting code. You can insert the markers using the # button in the toolbar above the edit window.

  • Post the ACTUAL code you are trying to get working including the #include lines.

  • Brian

You'll probably find the number is around 32768 - you're probably using a signed int as a loop counter or limit somewhere.
Need more?
Use a long as your loop counter.

Thank you!
Now I got it working. Below is the code I´m using. I had to change data types for "absAsema" and "tavoiteAsema". I thought that only pulsenumber must be datatype -long- , when I´m calculating the pulsenumber. But also "absAsema" and "tavoiteAsema" must be -long- ,though they have values only between 0...255.
Do you know why?

// I/O Linjojen määrittely

//SERVON OHJAUS SIGNAALIT
int suuntaPlusOut = 11;
int suuntaMiinusOut = 12;
int pulssiPlusOut = 9;
int pulssiMiinusOut = 10;


//PULSSIEN MUUTTUJAT
long pulseNumber = 0;                    // Number of pulses in pulse train                
double frequency = 50;                          
double period = (1 / frequency) * 1000;                      
double dutyCycle = .5;                  //duty cycle                                        
int on = (dutyCycle * period)/2;
            
//PAIKOITUKSEN LASKEMINEN
long absAsema=127;
long tavoiteAsema = 0;


#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);          // set outPin pin as output
  pinMode(suuntaMiinusOut, OUTPUT);          // set outPin pin as output
  pinMode(pulssiPlusOut, OUTPUT);          // set outPin pin as output  
  pinMode(pulssiMiinusOut, OUTPUT);          // set outPin pin as output  
  Serial.begin(9600);      // opens serial port, sets data rate to 9600 bps
}

void loop() {

   if (Serial.available() > 0) {

     
 //CALCULATING THE PULSENUMBER AND POSITION
    tavoiteAsema = Serial.read();
    pulseNumber = (absAsema-tavoiteAsema)*785;  //AbsAsema is Position where servo is rihgt now
    // and tavoiteAsema is the position where servo is next.
    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);  
}

::slight_smile: