Expected initializer before 'while'

I am having trouble. It keeps saying missing initializer before 'while' and when I add { before while it says you cannot add something before { so I add loop after { and it still doesn't work. Here is the code.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int servoPin2 = 10;
int servoPin3 = 11; 
int servoPin4 = 3;
int servoPin5 = 6;
int pushButtonPin =2;// the pin where push button is connected
int pushButtonPin2 =4;
int pushButtonPin3 =13;
int pushButtonPin4 =8;
int pushButtonPin5 =7;
  
int angle =25;    // initial angle  for servo
int angleStep =5;

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
  myservo2.attach(servoPin2);
  myservo3.attach(servoPin3);
  myservo4.attach(servoPin4);
  myservo5.attach(servoPin5);  
  pinMode(pushButtonPin,INPUT_PULLUP);
  pinMode(pushButtonPin2,INPUT_PULLUP);
  pinMode(pushButtonPin3,INPUT_PULLUP);
  pinMode(pushButtonPin4,INPUT_PULLUP);
  pinMode(pushButtonPin5,INPUT_PULLUP);
  Serial.println("Robojax Servo Button ");
   //Full video details: https://youtu.be/qJebLpzLE24
}

void loop() {
  while(digitalRead(pushButtonPin) == LOW){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }
    myservo.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there
  }
  
  void loop() {
    while(digitalRead(pushButtonPin2) == LOW){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }
    myservo2.write(angle); // move the servo to desired angle
      Serial.print("Moved to: ");
      Serial.print(angle);   // print the angle
      Serial.println(" degree");    
  delay(100); // waits for the servo to get there
  }// while
}

Hi guys its been 5 minutes and I found the answer!! No need to answer anymore, I don't know how to close the question. I just forgot to add ; basic syntax everyone needs to know lol. Im so dumb, but ty

Mark the topic as solved.

Also please take care where you post questions in future. Each forum section has a sticky post at the top describing what kinds of topics the section is intended for.

You have got two loop()s.
That can't be right.

For the benefit of others, please post the correct sketch in a new reply and explain what was wrong

Hi how do I mark the post solved?

Below each reply there is a checkbox that allows you to mark that reply as having solved your problem

okay thank you

What was wrong is the syntax. I forgot to put ';' at the end of void loop.

how so? I am supposed to have 5 buttons and 5 servo each button making a servo move so there must be 5 void loops in total. How is it wrong? (I am still new so please explain)

Sorry, but I don't understand what you mean by that. Please post the full corrected sketch

There was no ; after void loop like this.

void loop() ; {
    while(digitalRead(pushButtonPin2) == LOW){
  // change the angle for next time through the loop:
  angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }

No, that's wrong. I don't know what you think you have solved, but that isn't it. Putting a semicolon there would cause compilation errors.

void loop();

would be interpreted as a forward declaration. The code inside the { } following it would not be allowed outside a function.

As @PaulRB has pointed out that is wrong so you have not fixed anything I am afraid

As far as I know, an Arduino sketch must have:

  • ONE setup() function
  • ONE loop() function
1 Like

can you help me figure it out please then as you are correct I am having difficulties closing off the code.

Start here. @JohnLincoln is right. You may have other functions called whatever you name them, but you will always only have ONE each void setup() and void loop().

Here's a start to correcting your code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int servoPin2 = 10;
int servoPin3 = 11;
int servoPin4 = 3;
int servoPin5 = 6;
int pushButtonPin = 2; // the pin where push button is connected
int pushButtonPin2 = 4;
int pushButtonPin3 = 13;
int pushButtonPin4 = 8;
int pushButtonPin5 = 7;

int angle = 25;   // initial angle  for servo
int angleStep = 5;

void setup() {
  // Servo button demo by Robojax.com
  Serial.begin(9600);          //  setup serial
  myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
  myservo2.attach(servoPin2);
  myservo3.attach(servoPin3);
  myservo4.attach(servoPin4);
  myservo5.attach(servoPin5);
  pinMode(pushButtonPin, INPUT_PULLUP);
  pinMode(pushButtonPin2, INPUT_PULLUP);
  pinMode(pushButtonPin3, INPUT_PULLUP);
  pinMode(pushButtonPin4, INPUT_PULLUP);
  pinMode(pushButtonPin5, INPUT_PULLUP);
  Serial.println("Robojax Servo Button ");
  //Full video details: https://youtu.be/qJebLpzLE24
}

void loop() {
  int pushButtonState = digitalRead(pushButtonPin);
  while (pushButtonState == LOW) {
    // change the angle for next time through the loop:
    angle = angle + angleStep;

    // reverse the direction of the moving at the ends of the angle:
    if (angle <= 0 || angle >= 180) {
      angleStep = -angleStep;
    }
    myservo.write(angle); // move the servo to desired angle
    Serial.print("Moved to: ");
    Serial.print(angle);   // print the angle
    Serial.println(" degree");
    delay(100); // waits for the servo to get there
  }
}

Note the way we read pushbuttons:

int pushButtonState = digitalRead(pushButtonPin);

Why? The way you had it will read (and print) only the Arduino pin number you assigned to button to.

Thank you

Thank you so much! Sorry I am still new I am learning

So after

delay(100); //waits for the servo to get there
  }

// new button and servo?