for loop question!?

Hi all,

Having a strange problem with my loop. Any value I use for a total number on the loop works perfect up to ~32,000. If I enter 33,000+ it blows right past the 33,000 and just continues “forever”. Where as 32,000 or less will reach the number, then reverse (motor signal) and continue back to start position. I’m just trying to move 4 axis on a robot their full range of motion at once, then move back. Each axis has a different degree of movement though, hence the high number values to create resolution to use the modulo operator for precise timing of the motor increments.

This has hung me up for a while now. Any help would be much appreciated.

``````byte wN14Direction = 13;
byte wN14Step = 12;
byte lN23Direction = 11;
byte lN23Step = 10;
byte rN23Direction = 9;
byte rN23Step = 8;
byte rN17Direction = 7;
byte rN17Step = 6;
byte pN17Direction = 5;
byte pN17Step = 4;
byte fN17Direction = 3;
byte fN17Step = 2;

int millisbetweenSteps = 0.1;

void setup() {

Serial.begin(9600);
Serial.println("Start");
Serial.print('\n');
Serial.print('\n');

pinMode(wN14Direction, OUTPUT);
pinMode(wN14Step, OUTPUT);
pinMode(rN17Direction, OUTPUT);
pinMode(rN17Step, OUTPUT);
pinMode(lN23Direction, OUTPUT);
pinMode(lN23Step, OUTPUT);
pinMode(rN23Direction, OUTPUT);
pinMode(rN23Step, OUTPUT);
pinMode(fN17Direction, OUTPUT);
pinMode(fN17Step, OUTPUT);
pinMode(pN17Direction, OUTPUT);
pinMode(pN17Step, OUTPUT);

digitalWrite(rN17Direction, LOW);
digitalWrite(rN17Step, LOW);
digitalWrite(lN23Direction, LOW);
digitalWrite(lN23Step, LOW);
digitalWrite(rN23Direction, LOW);
digitalWrite(rN23Step, LOW);
digitalWrite(pN17Direction, LOW);
digitalWrite(pN17Step, LOW);
digitalWrite(wN14Direction, LOW);
digitalWrite(wN14Step, LOW);
digitalWrite(fN17Direction, LOW);
digitalWrite(fN17Step, LOW);

delay(500);

int currentPos = 0.0;
int currentInc = 1.0;

digitalWrite(fN17Direction, HIGH);
digitalWrite(pN17Direction, HIGH);
digitalWrite(rN17Direction, LOW);
digitalWrite(rN23Direction, HIGH);
digitalWrite(lN23Direction, LOW);

///*

////////////
// Start  //
////////////

for(int i=0; i<32000; i++){ //               Adjust this value > 33000
currentPos = currentPos += currentInc;
//    Serial.println(i);
//    delay(100);
if (currentPos % 10 == 0) {
digitalWrite(rN23Step, HIGH);
digitalWrite(lN23Step, HIGH);
digitalWrite(rN23Step, LOW);
digitalWrite(lN23Step, LOW);
//    Serial.println("l/rN23");
//    delay(100);
}
//    if (currentPos % 25 == 0) {
//      digitalWrite(rN17Step, HIGH);
//      digitalWrite(rN17Step, LOW);
//    }
if (currentPos % 25 == 0) {
digitalWrite(pN17Step, HIGH);
digitalWrite(pN17Step, LOW);
//      Serial.println("pN17");
//      delay(100);
}
if (currentPos % 50 == 0) {
digitalWrite(fN17Step, HIGH);
digitalWrite(fN17Step, LOW);
//      Serial.println("fN17");
//      delay(100);
}
delay(millisbetweenSteps);
}
delay(1000);

/////////////
// REVERSE //
/////////////

Serial.println("Reverse");
delay(300);

digitalWrite(fN17Direction, LOW);
digitalWrite(pN17Direction, LOW);
digitalWrite(rN17Direction, HIGH);
digitalWrite(rN23Direction, LOW);
digitalWrite(lN23Direction, HIGH);

///*

for(int i=0; i<32000; i++){ //
currentPos = currentPos += currentInc;
if (currentPos % 10 == 0) {
digitalWrite(rN23Step, HIGH);
digitalWrite(lN23Step, HIGH);
digitalWrite(rN23Step, LOW);
digitalWrite(lN23Step, LOW);
}
//    if (currentPos % 25 == 0) {
//      digitalWrite(rN17Step, HIGH);
//      digitalWrite(rN17Step, LOW);
//    }
if (currentPos % 25 == 0) { ::)
digitalWrite(pN17Step, HIGH);
digitalWrite(pN17Step, LOW);
}
if (currentPos % 50 == 0) {
digitalWrite(fN17Step, HIGH);
digitalWrite(fN17Step, LOW);
}
delay(millisbetweenSteps);
}
delay(1000);
Serial.print('\n');
Serial.println("Done");

//*/

}

void loop()
{

}
``````

You've declared 'i' to be a 16-bit integer. It can only hold positive numbers up to 32767. If you want bigger limits, declare 'i' as unsigned long (or uint32_t).

Pete

``````int millisbetweenSteps = 0.1;
``````
``````    delay(millisbetweenSteps);
``````

The delay() function takes an unsigned long (an integer) as its parameter so this won't do what the name of the variable indicates, neither can an integer variable hold a float value.

el_supremo:
You’ve declared ‘i’ to be a 16-bit integer. It can only hold positive numbers up to 32767.
If you want bigger limits, declare ‘i’ as unsigned long (or uint32_t).

Pete

Is it literally `for(unsigned long i=0; i<32000; i++){`?

UKHeliBob:

``````int millisbetweenSteps = 0.1;
``````
``````    delay(millisbetweenSteps);
``````

The delay() function takes an unsigned long (an integer) as its parameter so this won’t do what the name of the variable indicates, neither can an integer variable hold a float value.

You’re right, thank you. I didn’t catch that when I was messing around.

Yes, either

``````for(unsigned long i=0; i<32000; i++){
``````

or

``````for(uint32_t i=0; i<32000; i++){
``````

will work and allow you a range up to 4294967295 which is usually sufficient for most purposes

Pete

You need to increase i<32000 to get to 4294967295 (0xFFFFFFFF vs 0xFFFF) as well.

el_supremo:
Yes, either

``````for(unsigned long i=0; i<32000; i++){
``````

or

``````for(uint32_t i=0; i<32000; i++){
``````

will work and allow you a range up to 4294967295 which is usually sufficient for most purposes

Pete

That worked great, thank you for your time!