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