Not declared error only on third instance

I'm trying out some concepts for a stepper motor control. I use the first part of this sketch successfully in the setup portion of the sketch. I added then next two parts and put into the loop. I get this error flagged only in the third WHILE loop, not in the first two.
"counter' was not declared in this scope"
I don't see why only that one is a porblem.

Here is the sketch:

#define motorPin1  8   //Blue      |
#define motorPin2  9   //Pink      |  to plug
#define motorPin3  10  //Yellow     > connector
#define motorPin4  11  //Orange    |  on ULN2003
//                       Red (VCC) |

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  int counter = 0;
}
void loop() {
  while (counter < 512) {
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    delay(7);
    counter = counter + 1;
    if (counter = 512) {
      Serial.print("count is ");
      Serial.println(counter);
      Serial.println("Aspect is Approach")
      delay(2000);
    }
  }
  while (counter < 1024) {
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    counter = counter + 1;
    if (counter = 1024) {
      Serial.print("count is ");
      Serial.println(counter);
      Serial.println("Aspect is Clear")
      delay(2000);
    }
  }
  while (counter > 0) { //**<------THIS IS WHAT GETS FLAGGED**
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    delay(7);
    digitalWrite(motorPin1, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
    counter = counter - 1;
    if (counter = 0) {
      Serial.print("count is ");
      Serial.println(counter);
      Serial.println("Aspect is Stop")
    }
  }
}

This is the wrong place for the definition

It’s time to read about scope .

The compiler might complain about one occurrence of the variable which might not be the first one in the code .

Make this variable global and you’ll be fine.

PS: not totally fine though - check lines like this

== is for testing equality whereas = is for assigning a value.

PS2: why test in the while loop? Let the while loop finish and then you can print the message before starting the next while loop.

Moving the counter initialization before the setup took care of that error. After adding a couple of semicolons, it compiles. I'll get that single = fixed and see if it works.

You have it 3x and you can just get rid of that test anyway if you exit the while loop it means the condition went false …

For example if after incrementing the counter you go to the top

And the condition is false, then you exit the while loop and that’s where you can print the message before launching the next one.

PS. Also no need to repeat the digitalWrite() that do not change the previous value.

There's a different value in each test. It's there for the pause and print.

You are always testing for the condition that happens when you exit the loop

So I mean instead of doing

Keep it simple :

while (counter < 512) {
  digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH); delay(7); // LOW LOW LOW HIGH
  digitalWrite(motorPin3, HIGH); delay(7); // LOW LOW HIGH HIGH
  digitalWrite(motorPin4, LOW);  delay(7); // LOW LOW HIGH LOW
  digitalWrite(motorPin2, HIGH); delay(7); // LOW HIGH HIGH LOW
  digitalWrite(motorPin3, LOW);  delay(7); // LOW HIGH LOW LOW
  digitalWrite(motorPin1, HIGH); delay(7); // HIGH HIGH LOW LOW
  digitalWrite(motorPin2, LOW);  delay(7); // HIGH LOW LOW LOW
  digitalWrite(motorPin4, HIGH); delay(7); // HIGH LOW LOW HIGH
  counter++;
}

//-----------------------------------------------
// WHEN YOU ARRIVE HERE counter HAS REACHED 512
//  SO THAT'S WHEN YOU WANT TO PRINT AND DELAY
//-----------------------------------------------

Serial.print("count is ");
Serial.println(counter);
Serial.println("Aspect is Approach");
delay(2000);

Same applies for the others ➜ there is no need to have the test inside the while loop since this is something you want to do once the while loop is over, just put it after the while loop.

Here's version 3:
And J-M-L Jackson, there are three different things to be printed.

#define motorPin1  8   //Blue      |
#define motorPin2  9   //Pink      |  to plug
#define motorPin3  10  //Yellow     > connector
#define motorPin4  11  //Orange    |  on ULN2003
//                       Red (VCC) |
int counter = 0;
int pause = 5000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  //Initialize pins
  digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
}
void loop() {
  while (counter <= 64) {
    digitalWrite(motorPin3, HIGH);  // 4 still HIGH, 3 HIGH (STEP 0)
    delay(6);
    digitalWrite(motorPin4, LOW); // 3 still HIGH, 4 LOW (STEP 1)
    delay(6);
    digitalWrite(motorPin2, HIGH);  // 3 still HIGH 2 HIGH, (STEP 2)
    delay(6);
    digitalWrite(motorPin3, LOW); // 2 still HIGH, 3 LOW (STEP 3)
    delay(6);
    digitalWrite(motorPin1, HIGH);  // 2 still HIGH, 1 HIGH (STEP 4)
    delay(6);
    digitalWrite(motorPin2, LOW);   // 1 still HIGH, 2 LOW (STEP 5)
    delay(6);
    digitalWrite(motorPin4, HIGH);  //  1 still HIGH, 4 HIGH (STEP 6)
    delay(6);
    digitalWrite(motorPin1, LOW);  // 4 still HIGH, 1 LOW (STEP 7)
    delay(6);
    counter = counter + 1;
    if (counter == 64) {
      Serial.println("Approach");
      delay(pause);
    }
  }
  while (counter <= 128) {
    digitalWrite(motorPin3, HIGH);  // 4 still HIGH, 3 HIGH (STEP 0)
    delay(6);
    digitalWrite(motorPin4, LOW); // 3 still HIGH, 4 LOW (STEP 1)
    delay(6);
    digitalWrite(motorPin2, HIGH);  // 3 still HIGH 2 HIGH, (STEP 2)
    delay(6);
    digitalWrite(motorPin3, LOW); // 2 still HIGH, 3 LOW (STEP 3)
    delay(6);
    digitalWrite(motorPin1, HIGH);  // 2 still HIGH, 1 HIGH (STEP 4)
    delay(6);
    digitalWrite(motorPin2, LOW);   // 1 still HIGH, 2 LOW (STEP 5)
    delay(6);
    digitalWrite(motorPin4, HIGH);  //  1 still HIGH, 4 HIGH (STEP 6)
    delay(6);
    digitalWrite(motorPin1, LOW);  // 4 still HIGH, 1 LOW (STEP 7)
    delay(6);
    counter = counter + 1;
    if (counter == 128) {
      Serial.println("Clear");
      delay(pause);
    }
  }
  while (counter >= 0) {
    digitalWrite (motorPin1, HIGH);  // 4 still HIGH, 1 HIGH (STEP 0)
    delay(6);
    digitalWrite (motorPin4, LOW);  // 1 still HIGH, 4 LOW (STEP 1)
    delay(6);
    digitalWrite (motorPin2, HIGH); // 1 still HIGH, 2 HIGH (STEP 2)
    delay(6);
    digitalWrite (motorPin1, LOW);  // 2 still HIGH, 1 LOW (STEP 3)
    delay(6);
    digitalWrite (motorPin3, HIGH); // 2 still HIGH, 3 HIGH (STEP 4)
    delay(6);
    digitalWrite (motorPin2, LOW);  // 3 still HIGH, 2 LOW (STEP 5)
    delay(6);
    digitalWrite (motorPin4, HIGH); // 3 still HIGH, 4 HIGH (STEP 6)
    delay(6);
    digitalWrite (motorPin3, LOW);  // 4 still HIGH, 3 LOW (STEP 7)
    delay(6);
    counter = counter - 1;
    if (counter == 0) {
      Serial.println("Stop");
      delay(pause);
    }
  }
}

Okay J-M-L, it dawned on me what you were saying about the print outside the loop.

Here is stepperTest3a, the final version before moving on to a different control concept.

#define motorPin1  8   //Blue      |
#define motorPin2  9   //Pink      |  to plug
#define motorPin3  10  //Yellow     > connector
#define motorPin4  11  //Orange    |  on ULN2003
//                       Red (VCC) |
int counter = 0;
int pause = 5000;
int timing = 5;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  //Initialize pins
  digitalWrite(motorPin1, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH);
}
void loop() {
  while (counter <= 64) {
    digitalWrite(motorPin3, HIGH);  // 4 still HIGH, 3 HIGH (STEP 0)
    delay(timing);
    digitalWrite(motorPin4, LOW); // 3 still HIGH, 4 LOW (STEP 1)
    delay(timing);
    digitalWrite(motorPin2, HIGH);  // 3 still HIGH 2 HIGH, (STEP 2)
    delay(timing);
    digitalWrite(motorPin3, LOW); // 2 still HIGH, 3 LOW (STEP 3)
    delay(timing);
    digitalWrite(motorPin1, HIGH);  // 2 still HIGH, 1 HIGH (STEP 4)
    delay(timing);
    digitalWrite(motorPin2, LOW);   // 1 still HIGH, 2 LOW (STEP 5)
    delay(timing);
    digitalWrite(motorPin4, HIGH);  //  1 still HIGH, 4 HIGH (STEP 6)
    delay(timing);
    digitalWrite(motorPin1, LOW);  // 4 still HIGH, 1 LOW (STEP 7)
    delay(timing);
    counter++;
  }
  Serial.println("Approach");
  delay(pause);
  while (counter <= 128) {
    digitalWrite(motorPin3, HIGH);  // 4 still HIGH, 3 HIGH (STEP 0)
    delay(timing);
    digitalWrite(motorPin4, LOW); // 3 still HIGH, 4 LOW (STEP 1)
    delay(timing);
    digitalWrite(motorPin2, HIGH);  // 3 still HIGH 2 HIGH, (STEP 2)
    delay(timing);
    digitalWrite(motorPin3, LOW); // 2 still HIGH, 3 LOW (STEP 3)
    delay(timing);
    digitalWrite(motorPin1, HIGH);  // 2 still HIGH, 1 HIGH (STEP 4)
    delay(timing);
    digitalWrite(motorPin2, LOW);   // 1 still HIGH, 2 LOW (STEP 5)
    delay(timing);
    digitalWrite(motorPin4, HIGH);  //  1 still HIGH, 4 HIGH (STEP 6)
    delay(timing);
    digitalWrite(motorPin1, LOW);  // 4 still HIGH, 1 LOW (STEP 7)
    delay(timing);
    counter++;
  }
  Serial.println("Clear");
  delay(pause);
  while (counter >= 0) {
    digitalWrite (motorPin1, HIGH);  // 4 still HIGH, 1 HIGH (STEP 0)
    delay(timing);
    digitalWrite (motorPin4, LOW);  // 1 still HIGH, 4 LOW (STEP 1)
    delay(timing);
    digitalWrite (motorPin2, HIGH); // 1 still HIGH, 2 HIGH (STEP 2)
    delay(timing);
    digitalWrite (motorPin1, LOW);  // 2 still HIGH, 1 LOW (STEP 3)
    delay(timing);
    digitalWrite (motorPin3, HIGH); // 2 still HIGH, 3 HIGH (STEP 4)
    delay(timing);
    digitalWrite (motorPin2, LOW);  // 3 still HIGH, 2 LOW (STEP 5)
    delay(timing);
    digitalWrite (motorPin4, HIGH); // 3 still HIGH, 4 HIGH (STEP 6)
    delay(timing);
    digitalWrite (motorPin3, LOW);  // 4 still HIGH, 3 LOW (STEP 7)
    delay(timing);
    counter--;
  }
  Serial.println("Stop");
  delay(pause);
}

Glad you finally got it :wink: