Move servos independent by pressing joystick button

Hi all,
I have two servos that i plant to implement on a prototype excavator. one servo will move the arm up and down and the other will move the bucket to and fro. I want the bucket and arm to be at a particular position. I am using one joystick, when i move t he joystick up it will control one motor and i will like that when i press the onboard joystick button that i can move the joystick same up and down to control the other motor without moving the other motor. I am not sure what pin on the arduino to attach the onboard select button. Below is my code, it doesnt allow the select button to do anything, i.e the two servos move simultaneously when i move the joystick

int Joy_button = 2; // button from joystick input
//const int Push_button_1 = 3;
//const int Push_button_2 = 4;

const int led = 13; // onboard led 
int x = 512; // set initial value for x
int y = 512; // set initial value for y
int z = 0; // set initial value for z


#include <Servo.h> // include servo library
Servo first_servo, second_servo; // create server object to control the first servo

void setup() {
  
  first_servo.attach(3); // attach the servo on pin 9
second_servo.attach(4);
//Joy_button.attach(2) ;
}

void loop(){
  
  digitalRead(Joy_button);
  
  if (Joy_button,1) {


  x = analogRead(A0); // read the value from the joystick 0-1023
  
  x = map(x,0,1023,0,180); //scale the analog input values form the pot to a suitable digital signal
  
  first_servo.write(x); // sets the servo position
  
  delay(15); // waits
}

else if (Joy_button,0);
{

 x = analogRead(A0); // read the value from the joystick 0-1023
  
  x = map(x,0,1023,0,180); //scale the analog input values form the pot to a suitable digital signal
  
  second_servo.write(x); // sets the servo position
  
  delay(15);


}




}

I am not sure what pin on the arduino to attach the onboard select button.

Why not? You defined a variable, Joy_button, assigned it a value, and provided a comment that implies that is where the joystick button is (to be) attached.

Servo first_servo, second_servo; // create server object to control the first servo

Useless names. How are we supposed to know which servo does what? Suppose, instead, that you had used names like bucketServo and armServo. Then, we could assume that bucketServo moved the arm, and that armServo moved the bucket. Or that other way around.

  digitalRead(Joy_button);

But throw the result away. Why?

  if (Joy_button,1) {

You are going to have to explain this misuse of the comma operator. What are you trying to do here?

else if (Joy_button,0);

and here. And explain why there is a semicolon on the end.

In any case, Joy_button is always 2, so (syntax aside) doing an "if" on it is meaningless. You mean to look at the value of the pin as read, I think, but as PaulS just said, you threw that away.

You might get some value from this Thread about an excavator.

...R

@ PaulS , thanks for the pointers man . Ill fix it .
@Robin2 , I checked that out before i posted this, didnt really help me :expressionless: . thanks for your input though bro/sis lol

Below is my code, it doesnt allow the select button to do anything, i.e the two servos move simultaneously when i move the joystick

You might try making the servo commands a variable that is sent to the servo in the main loop. Make an if statement in the main loop that updates the servo command variable if the button is pushed. The variable update is skipped when the button is not pressed.

Izzy92:
@Robin2 , I checked that out before i posted this, didnt really help me :expressionless: . thanks for your input though bro/sis lol

I had already dicovered this.

If you had kept all the discussion in your earlier Thread I would not have wasted my time posting superfluous advice.

Don't split a topic over multiple Threads. It just wastes everyone's time.

Please ask the moderator to merge the Threads.

...R

@Robin2 Sorry i am new to posting in forums. this is my second day at posting in forums in my life ! :sweat_smile:
More importantly bro, i saw your post from this thread Re: Controls for a project - #40 by system - Project Guidance - Arduino Forum on how to control the servos the way i wanted. thanks for that. Also I was wondering if there was a way to be able to push a button and have the motors go to a particular position.... lets say 90 degrees or the orginal position. below is what i tried <note that the push button i placed is connected to dig pin 2> In my code that is derived from yours I try to set the potValue to 512 so that it can make that 90 degree angle.

#include <Servo.h>

#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3

Servo servo[4];      //code used for attaching upto 4 servos

byte angle[4] = {90, 90, 90, 90};       // middle point for servo angle

byte potPin[4] = {A0, A1, A2, A3};  // input pins to attach your potentiometers
byte servoPin[4] = {7, 6, 10, 9};    // input pins to attach servos
const int buttonPin = 2 ; //              push-button attached to 2
int buttonState = 0 ;       //   State of the push-button
 int potVal ; 
void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");
  pinMode(buttonPin, INPUT) ;          // set push button as an input

  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH);
   {
    potVal == 512;}
}

void readPotentiometers() {
  int potVal;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    
    if (potVal < 200) {         // dead zone for the joystick I used is 200 to 550.
      angle[n] += 1;
      if (angle[n] > 170) {
        angle[n] = 170;
      }
    }
    
    if (potVal > 550) {         // deadzone upper value
      angle[n] -= 1;
      if (angle[n] < 10) {
        angle[n] = 10;
      }
    }

  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].write(angle[n]);
  }
}

Izzy92:
Also I was wondering if there was a way to be able to push a button and have the motors go to a particular position...

You are doing it AGAIN.

I have already replied to this in your newest Thread.

Ask the moderator to merge all three Threads. Click Report to Moderator in this (my) post and give him the links to the other two Threads.

...R

Sorry it won't happen again.