Controlling 2 MG995 Servos with 3 push buttons

Hi, i want to know if someone could help me in my simple school project, I need to control 2 MG995 servos with 3 push buttons and an arduino uno.

Here's how it works, the buttons is connected to D2-D4 pin with input pullup and the servos is connected to PWM pin D6 & D9, the servo is powered directly from arduino 5v pin.

If none of the buttons is pressed, both servos will be in the following position (pos = 34, pos2 = 29).

If the first button is pressed, the first servo will move to the 75 degree position, and remain in that position for 4 seconds before returning to the initial position (pos = 34).

If the second button is pressed, the second servo will move to the 75 degree position, and remain in that position for 4 seconds before returning to the initial position (pos2 = 29).

if the third button is pressed, both servos will move to the 75 degree position, and remain in that position for 4 seconds before returning to the initial position (pos = 34, pos2 = 29).

The problem is, the third button won't work and it only manage to activate the second servo. is there a problem with my program?

Here is my program:

int button = 2;
int button2 = 3;
int button3 = 4;
int pos = 34;
int pos2 = 29;
#include <Servo.h>
#define Servo_PWM 6
#define Servo_PWM2 9
Servo MG995_Servo;

void setup() {
  Serial.begin(9600);
  MG995_Servo.attach(Servo_PWM);
  MG995_Servo.attach(Servo_PWM2);
  pinMode (button, INPUT_PULLUP);
  pinMode (button2, INPUT_PULLUP);
  pinMode (button3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(button) == HIGH)
  {
    Serial.println("close");
    MG995_Servo.attach(Servo_PWM);
    MG995_Servo.write(pos);
    MG995_Servo.detach();//Stop
  } 
  else 
  {
    Serial.println("open");
    MG995_Servo.attach(Servo_PWM);
    MG995_Servo.write(75);
    delay (4000);
    MG995_Servo.write(pos);
    delay(1000);
    MG995_Servo.detach();//Stop
  }


  if (digitalRead(button2) == HIGH)
  {
    Serial.println("close2");
    MG995_Servo.attach(Servo_PWM2);
    MG995_Servo.write(pos2);
    MG995_Servo.detach();//Stop
  } 
  else 
  {
    Serial.println("open2");
    MG995_Servo.attach(Servo_PWM2);
    MG995_Servo.write(75);
    delay (4000);
    MG995_Servo.write(pos2);
    delay(1000);
    MG995_Servo.detach();//Stop
  }


  if (digitalRead(button3) == HIGH)
  {
    Serial.println("close_all");
    MG995_Servo.attach(Servo_PWM);
    MG995_Servo.attach(Servo_PWM2);
    MG995_Servo.write(pos);
    MG995_Servo.write(pos2);
    MG995_Servo.detach();//Stop
  } 
  else 
  {
    Serial.println("open_all");
    MG995_Servo.attach(Servo_PWM);
    MG995_Servo.attach(Servo_PWM2);
    MG995_Servo.write(75);
    delay (4000);
    MG995_Servo.write(pos);
    MG995_Servo.write(pos2);
    delay(1000);
    MG995_Servo.detach();//Stop
  }
}

sorry for my bad english,,,tehe;)

Classic mistake. You do not want to do this (and other such similar things), you want to do this when the button becomes high.

Look at the state change example in the IDE's examples menu.

Don’t do this…

A couple of comments

  • You do not need to use PWM pins to control servos, although it is OK if you do. Any digital pin, including the A* pins can be used
  • You should attach() the servos just once in setup() and leave them attach()ed unless you have a good reason not to. Do you have such a reason ?

In an ideal world I agree, but why bother in this simple case

because when i only once in the setup, then the servo won't move at all

Where is the second servo defined in your sketch ?

    MG995_Servo.attach(Servo_PWM);
    MG995_Servo.attach(Servo_PWM2);

The second servo needs its own name, then you can remove all of the silly attach()/detach() calls and just write() to each servo by name

its right here

#define Servo_PWM 6
#define Servo_PWM2 9

i named it

That #defines 2 pin numbers, not 2 Servo objects

Servo MG995_Servo;

This creates a single Servo object named G995_Servo

You are confusing servo pin names with servo names

Here is what I would do

  • #define the 2 servo pin numbers
  • Create 2 instances of Servo with unique names
  • attach() each servo to its associated pin in setup()
  • There is no need to attach()/detach() the servos in loop()
  • You can then write() to whichever servo or servos that you want by using their unique names

Hi, thanks a lot for your suggestion
It works rn😃
Here's the result Terlihat sederhana, namun harus ngoding seharian karena terbiasa copas #ADELEPRO #Servo #Gripper | By ADELE PROFacebook

That is good news

Please post your working sketch here for the benefit of other users

Did you note the warning about powering the servos directly from the Arduino ? They can take more current than the Arduino can provide, particularly when the servo is stalled

Yes it is, that's why i have separated power supply
I will upload the sketch asap, after coming back from school

here you go

int button = 2;
int button2 = 3;
int button3 = 4;
int pos = 34;
int pos2 = 29;
#include <Servo.h>
#define Servo_PWM 6
#define Servo_PWM2 9
Servo MG995_Servo;
Servo MG995_Servo2;

void setup() {
  Serial.begin(9600);
  MG995_Servo.attach(Servo_PWM);
  MG995_Servo2.attach(Servo_PWM2);
  pinMode (button, INPUT_PULLUP);
  pinMode (button2, INPUT_PULLUP);
  pinMode (button3, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(button) == HIGH)
  {
    Serial.println("close");
    MG995_Servo.write(pos);
  } 
  else 
  {
    Serial.println("open");
    MG995_Servo.write(75);
    delay (4000);
    MG995_Servo.write(pos);
  }

  if (digitalRead(button2) == HIGH)
  {
    Serial.println("close2");
    MG995_Servo2.write(pos2);
  } 
  else 
  {
    Serial.println("open2");
    MG995_Servo2.write(75);
    delay (4000);
    MG995_Servo2.write(pos2);
  }

  if (digitalRead(button3) == HIGH)
  {
    Serial.println("close_all");
    MG995_Servo.write(pos);
    MG995_Servo2.write(pos2);
  } 
  else 
  {
    Serial.println("open_all");
    MG995_Servo.write(75);
    MG995_Servo2.write(75);
    delay (4000);
    MG995_Servo.write(pos);
    MG995_Servo2.write(pos2);
  }
}

A couple of comments

  • The Serial monitor is going to be full of messages about closed servos for most of the time. Does that matter ?

  • What if you wanted to move the second servo within 4 seconds of pressing the button to move the first one ? Does you assignment mention this ?

the serial monitor thing is matter, it is to monitor if arduino is working properly or not, because I once got a servo that was damaged, thought the damage was in the Arduino but I was wrong after prove it using serial monitor.

My assignment doesn't mention this because it's not what the teacher need

Understood

Then I would question the teacher over what happens in this case. Having supervised many projects myself while I worked at a UK University I would be pleased and give the pupal extra credit for this.

Or if you do not want to do this, then just include the case anyway, it is easy enough to do.

Is this an AI generated reply? It certainly looks like one.