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