nested loops

Hi there
Can someone help please. I am tearing my hair out in frustration. I cannot see what I am donig wrong.
I need the While (steps) loop in following to repat "Divs" times I have tried IF For While loops to no avail. Help please.

int Divs=0;
int Dir=0;
int Steps=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(2,OUTPUT);//Steps
pinMode(3,OUTPUT);//Dir
}

void loop() {
Serial.println ("Enter Number of Divisions");
while (!Serial.available()) {//wait for data    
    }
    Divs = Serial.parseInt();//assign serial data to Divs    
    Serial.println(Divs);     
    Steps =(1600/Divs);//make steps = to 1600(number of microsteps)/Divs
   if (Divs>=0){   
       while (Steps>=0){
          Serial.println(Steps);               
          digitalWrite(2,HIGH);
          digitalWrite(2,LOW);
          Steps=Steps-1;
          Divs=Divs-1; 
    }        
        } 
      
  }

Thank you in advance
Bob

ok i have found it

if you want to repeat Divs time, have the loop test to be on Divs, not steps

you can also use a for loop

for (int i=0; i<Divs;i++) {

}

I'm unsure what the maths on Steps is for though

Hi Bob,

I ran your code before I saw:

ok i have found it

Please can you edit your title to include [Solved], or similar.

At some point your use of blocking code (while) is going to bite you as nothing else can happen.

These will help:
Using millis for timing
Demonstration for several things at the same time

I had not seen it!

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