A simple 2 button functionality

Hi All,

I have a fairly simple code which has 2 buttons - For button 1 , when pressed it runs a 2x servo and 2x relay routine - when button 2 is pressed I need it to run a second different routine on the same servo/relays.

At the moment when I press 1 it just runs the 1 routine which is fine -
But when I press 2 it runs both the 1 and 2 routine (presumably its running routine 1 on its way to routine 2?)

Any help on how I can make it just run routine 2 when button 2 is pressed?

Thanks all..

CODE:

#include <VarSpeedServo.h> 
 
VarSpeedServo myservo1;    // create servo object to control a servo 
VarSpeedServo myservo2;    // create servo object to control a servo
VarSpeedServo myservo3;    // create servo object to control a servo
VarSpeedServo myservo4;    // create servo object to control a servo


const int BUTTON_PIN = 13; // Arduino pin connected to button's pin
int relay1 = 2; //Amp CH1 relay
int relay2 = 3; //Amp CH2 relay
int relay3 = 4; //Amp CH3 relay
int relay4 = 5; //Amp CH4 relay
int button1 = 8; //CH1 Button
int button2 = 9; //CH2 Button
int button3 = 10; //CH3 Button
int button4 = 11; //CH4 Button
int buttonState = digitalRead(BUTTON_PIN); //  MAIN KILL SWITCH
 
void setup() {

     // Initialize digital output pins for relays
   pinMode(relay1, OUTPUT);
   pinMode(relay2, OUTPUT);
   pinMode(relay3, OUTPUT);
   pinMode(relay4, OUTPUT);
   pinMode(button1, INPUT_PULLUP);
   pinMode(button2, INPUT_PULLUP);
   pinMode(button3, INPUT_PULLUP);
   pinMode(button4, INPUT_PULLUP);
   pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mo
  myservo1.attach(6);  // attaches the servo on pin 6 to the servo object
  myservo2.attach(7); 
  myservo3.attach(22);
  myservo4.attach(23);
} 
 
void loop() {
  // put your main code here, to run repeatedly:
     bool button1State ;                 //////////////////////////BUTTON 1  RESET ALL ROUTINE///////////
   button1State = digitalRead(button1);
  
   if (button1State == LOW)
   

   
   {
          while(digitalRead(BUTTON_PIN)==HIGH)
{ 
; //do nothing 
}
  digitalWrite(relay1, HIGH);     ////ALL RELAYS OFF///      
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  delay(2000);                      /////2 second delay 

 digitalWrite(relay1, LOW);       /////Relay L and R ON to stop ROT position
 digitalWrite(relay3, LOW);
 delay(2000);

  myservo4.write(0, 30, false);         /// Closes grabs 
  myservo1.write(170, 30, false);
 

 myservo3.write(170, 30, false);        ///ANTI CLOCK SPIN RH
 myservo2.write(170, 30, false);        /// CLOCKWISE SPIN LH

 delay(9000);                            ////Spin for 5 secs to reach STOP point

 myservo3.write(90, 30, false);          ///ANTI CLOCK SPIN RH ramp down
 myservo2.write(90, 30, false);        /// CLOCKWISE SPIN LH ramp down

 delay(4000);

 digitalWrite(relay3, HIGH);             // RIGHT SPIN STOP RELAY OFF 
 digitalWrite(relay1, HIGH);             // RIGHT SPIN STOP RELAY OFF 

 myservo4.write(90, 30, false);          /// resets LH Grab to default
myservo1.write(90, 30, false);          /// resets RH Grab to default



 /////////////////////////////////////////////////END OF BUTTON 1//////////////////////////////////////////////////////////////

     bool button2State ;                 //////////////////////////BUTTON 2//////////////ADDS GRAB ACTIVITY////
   button2State = digitalRead(button2);
   if (button2State == LOW) 
  

   {
          while(digitalRead(BUTTON_PIN)==HIGH);
{ 
 //do nothing 
}


////enter code here//
  myservo4.write(170, 20, false);         /// Slowly opens Both Grabs for 5 secs
  myservo1.write(0, 20, false); 
  
  delay(5000);

  myservo4.write(0, 20, false);         /// Slowly Closes grabs
  myservo1.write(170, 20, false);

  delay(5000);

   myservo4.write(90, 30, false);          /// resets LH Grab to default
   myservo1.write(90, 30, false);          /// resets RH Grab to default

  digitalWrite(relay1, HIGH);     ////ALL RELAYS OFF///      
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);


   }}}

Separate each routine into a different function, f1 & f2.

Press button1 -- call f1. Press button2, call f2.

1 Like
if (button1State == LOW) {
  // button1 code
}
else if (button2State == LOW) {
  // button2 code
}
else {
  // no button's pushed
}

Your code has five buttons.

This needs to be button1State

Your button2() routine is INSIDE your button1() routine.

Your code, in simulation, ...
https://wokwi.com/projects/378898479896868865

This doesn't allow for the case when both buttons are pressed!

In that case, button 1 will take precedence.

@marcopirro - is that the behaviour you want?

Hi,
What is the application.

Sounds ominous.
It is usual practice for a kill switch or E-STOP to disconnect power to the device directly and not through software.
In your code, it may take 5 seconds to instigate the "kill".

const int BUTTON_PIN = 13; // Arduino pin connected to button's pin
int relay1 = 2; //Amp CH1 relay
int relay2 = 3; //Amp CH2 relay
int relay3 = 4; //Amp CH3 relay
int relay4 = 5; //Amp CH4 relay
int button1 = 8; //CH1 Button
int button2 = 9; //CH2 Button
int button3 = 10; //CH3 Button
int button4 = 11; //CH4 Button

I know its a bit picky but better you started your varible name off so well.

const int BUTTON_PIN = 13; // Arduino pin connected to button's pin
int relay1Pin = 2; //Amp CH1 relay
int relay2Pin = 3; //Amp CH2 relay
int relay3Pin = 4; //Amp CH3 relay
int relay4Pin = 5; //Amp CH4 relay
int button1Pin = 8; //CH1 Button
int button2Pin = 9; //CH2 Button
int button3Pin = 10; //CH3 Button
int button4Pin = 11; //CH4 Button

That way they cannot be mistaken in your code as program variables.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

1 Like

the code for the 2nd button is inside the condition for the first

look at the code when properly formatted

#include <VarSpeedServo.h>

VarSpeedServo myservo1;    // create servo object to control a servo
VarSpeedServo myservo2;    // create servo object to control a servo
VarSpeedServo myservo3;    // create servo object to control a servo
VarSpeedServo myservo4;    // create servo object to control a servo

const int BUTTON_PIN = 13; // Arduino pin connected to button's pin
int relay1 = 2; //Amp CH1 relay
int relay2 = 3; //Amp CH2 relay
int relay3 = 4; //Amp CH3 relay
int relay4 = 5; //Amp CH4 relay
int button1 = 8; //CH1 Button
int button2 = 9; //CH2 Button
int button3 = 10; //CH3 Button
int button4 = 11; //CH4 Button
int buttonState = digitalRead(BUTTON_PIN); //  MAIN KILL SWITCH

void setup() {

    // Initialize digital output pins for relays
    pinMode(relay1, OUTPUT);
    pinMode(relay2, OUTPUT);
    pinMode(relay3, OUTPUT);
    pinMode(relay4, OUTPUT);
    pinMode(button1, INPUT_PULLUP);
    pinMode(button2, INPUT_PULLUP);
    pinMode(button3, INPUT_PULLUP);
    pinMode(button4, INPUT_PULLUP);
    pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mo
    myservo1.attach(6);  // attaches the servo on pin 6 to the servo object
    myservo2.attach(7);
    myservo3.attach(22);
    myservo4.attach(23);
}


void loop() {
    // put your main code here, to run repeatedly:
    bool button1State ;                 //////////////////////////BUTTON 1  RESET ALL ROUTINE///////////
    button1State = digitalRead(button1);

    if (button1State == LOW)

    {
        while(digitalRead(BUTTON_PIN)==HIGH)
        {
            ; //do nothing
        }
        digitalWrite(relay1, HIGH);     ////ALL RELAYS OFF///
        digitalWrite(relay2, HIGH);
        digitalWrite(relay3, HIGH);
        digitalWrite(relay4, HIGH);
        delay(2000);                      /////2 second delay

        digitalWrite(relay1, LOW);       /////Relay L and R ON to stop ROT position
        digitalWrite(relay3, LOW);
        delay(2000);

        myservo4.write(0, 30, false);         /// Closes grabs
        myservo1.write(170, 30, false);

        myservo3.write(170, 30, false);        ///ANTI CLOCK SPIN RH
        myservo2.write(170, 30, false);        /// CLOCKWISE SPIN LH

        delay(9000);                            ////Spin for 5 secs to reach STOP point

        myservo3.write(90, 30, false);          ///ANTI CLOCK SPIN RH ramp down
        myservo2.write(90, 30, false);        /// CLOCKWISE SPIN LH ramp down

        delay(4000);

        digitalWrite(relay3, HIGH);             // RIGHT SPIN STOP RELAY OFF
        digitalWrite(relay1, HIGH);             // RIGHT SPIN STOP RELAY OFF

        myservo4.write(90, 30, false);          /// resets LH Grab to default
        myservo1.write(90, 30, false);          /// resets RH Grab to default

        //  END OF BUTTON 1

        bool button2State ;                 // BUTTON 2 ADDS GRAB ACTIVITY////
        button2State = digitalRead(button2);
        if (button2State == LOW)

        {
            while(digitalRead(BUTTON_PIN)==HIGH);
            {
                //do nothing
            }

            ////enter code here//
            myservo4.write(170, 20, false);         /// Slowly opens Both Grabs for 5 secs
            myservo1.write(0, 20, false);

            delay(5000);

            myservo4.write(0, 20, false);         /// Slowly Closes grabs
            myservo1.write(170, 20, false);

            delay(5000);

            myservo4.write(90, 30, false);          /// resets LH Grab to default
            myservo1.write(90, 30, false);          /// resets RH Grab to default

            digitalWrite(relay1, HIGH);     ////ALL RELAYS OFF///
            digitalWrite(relay2, HIGH);
            digitalWrite(relay3, HIGH);
            digitalWrite(relay4, HIGH);

        }
    }
}
1 Like

Many thanks all for your invaluable feedback.....

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