Two servo with two push button

Hi,

I am working on a project for which I need two servo and there push button, i need simple function i.e when i press button 1 then servo 1 will run once and when i press button 2 then servo 2, this can be a random click,

i have written a code but this is not working properly, looking for help please suggest

#include <Servo.h>

Servo myservo;
Servo myservo1;

// create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 90;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 myservo1.attach(8);

  }

void loop() {

  const byte buttonPin = 2;
  
    
    pinMode(buttonPin, INPUT_PULLUP);
    if(digitalRead(buttonPin) == HIGH)
 
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);   
    
  const byte buttonPin1 = 3;
    pinMode(buttonPin1, INPUT_PULLUP);
    
    
    if (digitalRead(buttonPin1) == HIGH)
  
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);   
  }}
  
}

“but this is not working properly”

Describe what is not working.

How ‘exactly’ is it supposed to work.

Suggest you look at change in state rather than operating on the level read.

How are the switches wired?

I want to run each servo only when there button click.

As long as the switch is pressed
or
one complete cycle when the switch is first pressed?

How are the switches wired?

if(digitalRead(buttonPin) == HIGH)

Did you want some { } with the above?

I have attached wire diagram.

I need
one complete cycle when the switch is first pressed and when i press switch 2 then motor 1 will stop and motor 2 will start run.

if(digitalRead(buttonPin) == HIGH)
{

//Did you want some { } with the above?

}

one complete cycle when the switch is first pressed and when i press switch 2 then motor 1 will stop and motor 2 will start run.

If motor 1 is 'in mid cycle', then you press switch 2 what happens with motor 1 if it is no yet finished?

You should not power servos with your Arduino as it can damage the Arduino.

Pushing your switch produces a LOW on the pin, not a HIGH.

yes

if(digitalRead(buttonPin) == HIGH)
{

i want { servo to run 180 degree }

}

If motor 1 is 'in mid cycle', then i press switch 2 then motor 1 will stop

only one servo will run at a time so it will not damage Arduino

yes

if(digitalRead(buttonPin) == HIGH)
{

i want { servo to run 180 degree }

}

If motor 1 is 'in mid cycle', then i press switch 2 then motor 1 will stop

only one servo will run at a time so it will not will damage Arduino

Okay, be forewarned bad things can happen.

You do know 'servos always draw power'.

You should 'never' power a motor from your Arduino!

If motor 1 is 'in mid cycle', then i press switch 2 then motor 1 will stop

Stop where it currently is or go to 0 degrees first?

And
If motor 2 is 'in mid cycle', then you press switch 1 then motor 1 will stop?

What is this project for?

#include <Servo.h>

// create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservo1;
Servo myservo2;

const byte switch1      = 2;    //switch press = LOW
const byte switch2      = 3;    //switch press = LOW
const byte heartBeatLED = 13;

byte cycleServo1Flag    = 0;    //= 0 stopped, = 1 going to 180, = 2 going to 0
byte cycleServo2Flag    = 0;    //= 0 stopped, = 1 going to 180, = 2 going to 0
byte lastSwitch1;               //current switch state
byte lastSwitch2;               //current switch state
byte servoDelay         = 15;   //15ms

int servo1Pos;
int servo2Pos;

//timing variables
unsigned long MillisHeartBeat;
unsigned long testSwitchMillis;
unsigned long servoMillis;

//**********************************************************************************************
void setup()
{
  myservo1.attach(8);
  myservo1.write(servo1Pos);

  myservo2.attach(9);
  myservo2.write(servo2Pos);

  pinMode(heartBeatLED, OUTPUT);

  pinMode(switch1, INPUT_PULLUP);         //push = LOW
  lastSwitch1 = digitalRead(switch1);

  pinMode(switch2, INPUT_PULLUP);         //push = LOW
  lastSwitch2 = digitalRead(switch2);

} //END of setup()

//**********************************************************************************************
void loop()
{
  //************************
  //code to see if the sketch is blocking, LED will toggle every 500ms
  if (millis() - MillisHeartBeat >= 500)
  {
    MillisHeartBeat = millis();

    //toggle LED state
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //************************
  //time to check the switches?
  if (millis() - testSwitchMillis >= 50)
  {
    testSwitchMillis = millis();
    checkSwitches();
  }

  //************************
  //run at loop speed
  checkServos();


  //************************
  //none blocking code here
  //************************


}  //END of loop()

//                                c h e c k S e r v o s ( )
//**********************************************************************************************
void checkServos()
{
  //************************
  //servo #1
  //cycleServo1Flag = 0 stopped, = 1 going to 180, = 2 going to 0
  //if enabled, has servoDelay ms gone by?
  if (cycleServo1Flag == 1 && millis() - servoMillis >= servoDelay)
  {
    //reset timer
    servoMillis = millis();

    myservo1.write(servo1Pos);
    servo1Pos++;

    //only go to 180
    if (servo1Pos > 180)
    {
      //at 180 now get ready to go lower
      servo1Pos = 179;

      //change to other direction
      cycleServo1Flag = 2;
    } //finished going to 180

  } //if

  //if enabled, has servoDelay ms gone by?
  else if (cycleServo1Flag == 2 && millis() - servoMillis >= servoDelay)
  {
    //reset timer
    servoMillis = millis();

    myservo1.write(servo1Pos);
    servo1Pos--;
    if (servo1Pos < 0)
    {
      //stop servo movement
      cycleServo1Flag = 0;
    } //finished going to 0

  } //else if

  //************************
  //servo #2
  //cycleServo2Flag = 0 stopped, = 1 going to 180, = 2 going to 0
  //add your second servo code here

  

} //END of checkServos()


//                              c h e c k S w i t c h e s ( )
//**********************************************************************************************
void checkSwitches()
{
  //************************
  //switch #1
  byte currentState = digitalRead(switch1);

  //has this switch position changed?
  if (lastSwitch1 != currentState)
  {
    //update to the new state
    lastSwitch1 = currentState;

    //was the switch just pushed?
    if (lastSwitch1 == LOW)
    {
      //allow servo 1 movment
      cycleServo1Flag = 1;

      //start at 0 degrees
      servo1Pos = 0;
    }
    
  } //if

  //************************
  //switch #2
  currentState = digitalRead(switch2);

  //has this switch position changed?
  if (lastSwitch2 != currentState)
  {
    //update to the new state
    lastSwitch2 = currentState;

    //add your second switch code here

  } //if

} //END checkSwitches()

//**********************************************************************************************

EDIT
Updated code

Thanks for code sir, I will check this soon and will let you know this is as per my requirement or not.

"If motor 2 is 'in mid cycle', then you press switch 1 then motor 1 will stop?"

Yes when i press button 2 then motor 1 will stop and when i press button 1 then motor 2 will stop

Note: at start both motor will at stop stage.

This project for two steering car, steering for front wheels and steering for back wheels,

Servo motors never stop.

  • You do know servos always draw power?

When the other switch is pressed:

  • Servos stop ‘where they currently are’? OR ‘go to 0 degrees’ first?

You should be able to add the 2nd switch/servo code by looking at how the 1st is written.

  • Ask if you need help.

EDIT