I tried adding separate angle variables. The result is still the same. I changed the print for buttons. Nothing change. It showed that one button that controls two servos is functioning and the second button for the other two servos didn't.
#include <Servo.h>
#include <ezButton.h>
//constants won't change
const int BUTTON_PIN = 7;
const int BUTTON_PIN2 = 6;
const int SERVO_PIN = 9;
const int SERVO_PIN2 = 8;
const int SERVO_PIN3 = 5;
const int SERVO_PIN4 = 4;
ezButton button1(7);
ezButton button2(6);
Servo servo;
Servo servo2;
Servo servo3;
Servo servo4;
// variables will change:
int angle1 = 0;
int angle2 = 0;
int angle3 = 0;
int angle4 = 0;
void setup() {
Serial.begin(9600);
button1.setDebounceTime(15);
button2.setDebounceTime(15);
servo.attach(SERVO_PIN);
servo2.attach(SERVO_PIN2);
servo3.attach(SERVO_PIN3);
servo4.attach(SERVO_PIN4);
//servo.write(angle);
//servo2.write(angle);
}
void loop() {
button1.loop();
button2.loop();
if(button1.isPressed() ) {
Serial.println("The button1 is pressed");
// change angle of servo motor
if(angle1 == 0 ) {
angle1 = 90;
angle2 = 0;
}
else {
angle1 = 0;
angle2 = 90;
}
if(angle2 ==0) {
angle1 = 90;
angle2 = 0;
}
else {
angle1 = 0;
angle2 = 90;
}
//control servo motor according to the angle
servo.write(angle1);
delay(50);
servo4.write(angle2);
delay(50);
}
if(button2.isPressed() ) {
Serial.println("The button2 is pressed");
// change angle of servo motor
if(angle3 == 0 ) {
angle3 = 90;
angle4 = 0;
}
else {
angle3 = 0;
angle4 = 90;
}
if(angle4 ==0) {
angle3 = 90;
angle4 = 0;
}
else {
angle3 = 0;
angle4 = 90;
servo3.write(angle3);
delay(15);
servo2.write(angle4);
delay(15);
}
}
}