How to control four servos with two buttons

Hello, I need your help with this Arduino code that I do not know what went wrong. My goal for this project is control two servo motors with the first button and the other two servo motors with the second button. I connected it to my portable power bank and successfully uploaded the code to the Arduino Uno board. I see one-button control two servo motors work but the second button that controls the other two servo motors didn't work. I might miss something in the code. Can you please help me out? Thank you.

here is the circuit diagram:

Here is the code I created:

#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;


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 button 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 button 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;



    servo3.write(angle1);
    delay(15);
    servo2.write(angle2);
    delay(15);
    }
 }
}

Sorry, but no help is available until you either show a schematic drawing of how you have all this actually connected together. Or at least a block diagram showing the same.
Paul

…and use CODE TAGS

are you sure both buttons are wired to the correct pin and presumably ground.

the print for both buttons is the same. shouldn't then be, for example, "button 2 is pressed"

both angle1 and angle2 are used for both buttons. so it's conceivable that the "other" button toggles the angle to what the servo is already set to. presumably each servo should have a separate angle variable

and of course you and google know that "code tags" means to use the </> button (?)

@gcjr

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

@Paul_KD7HB
I hope this diagram I made help

I sure does! Thanks. But the little triangles indicate a non-inverting buffer to most of us. What is it really?
Paul

Do you see the individual messages for each button when they are pressed?
Paul

@Paul_KD7HB

That little triangles represent the ground. Sorry for the confusion. Each servo come with three wires: positive, negative, and data so that triangles are "negative"

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;

} //<--------this right here is what missing the bracket! so I added it

Guys, I finally figured it out!! I found out why that second button that controls two servo motors is not functioning. It's the missing bracket all along! I added the last bracket and it finally works!
Thank you guys for all your efforts to help me out. I appreciated it!

????

1 Like

@gcjr

yes, both buttons are wired to the correct and grounded correctly. Right now, all servos functioning well.

so you posted non-compilable code ?!

@gcjr

I'm sorry, I'm a little lost... I don't understand what you mean by that? I wired both buttons to correct pins and negative ground pin

sorry, my mistake
the brace isn't missing, it's on the wrong line

    else {
      angle1 = 0;
      angle2 = 90;

    servo3.write(angle1);
    delay(15);
    servo2.write(angle2);
    delay(15);
    }

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