Moving 4 servos with 2 joysticks

Hello! im fairly new to arduino and coding, and i need help!
i want that when i release the joystick the servos doesnt go to the start position and stays put, bc im trying to make a robotic arm.
this is the code:
#include <Servo.h>

// Definizione dei pin per i joystick
const int pinJoystickX1 = A0;
const int pinJoystickY1 = A1;
const int pinJoystickX2 = A2;
const int pinJoystickY2 = A3;

// Definizione dei pin per i motori servo
const int pinServoX1 = 9;
const int pinServoY1 = 10;
const int pinServoX2 = 11;
const int pinServoY2 = 12;

// Definizione dell'intervallo per la mappatura del joystick
const int minVal = 0;
const int maxVal = 1023;

// Definizione dell'intervallo di gradi del motore servo
const int minDegree = 0;
const int maxDegree = 180;

Servo servoX1;
Servo servoY1;
Servo servoX2;
Servo servoY2;

void setup() {
// Inizializzazione dei motori servo
servoX1.attach(pinServoX1);
servoY1.attach(pinServoY1);
servoX2.attach(pinServoX2);
servoY2.attach(pinServoY2);
}

void loop() {
// Lettura dei valori sull'asse X e Y del primo joystick
int joystickValueX1 = analogRead(pinJoystickX1);
int joystickValueY1 = analogRead(pinJoystickY1);
// Lettura dei valori sull'asse X e Y del secondo joystick
int joystickValueX2 = analogRead(pinJoystickX2);
int joystickValueY2 = analogRead(pinJoystickY2);

// Mappatura dei valori letti sull'asse X e Y del primo joystick nell'intervallo dei gradi dei primi due motori servo
int servoPositionX1 = map(joystickValueX1, minVal, maxVal, minDegree, maxDegree);
int servoPositionY1 = map(joystickValueY1, minVal, maxVal, minDegree, maxDegree);
// Mappatura dei valori letti sull'asse X e Y del secondo joystick nell'intervallo dei gradi degli altri due motori servo
int servoPositionX2 = map(joystickValueX2, minVal, maxVal, minDegree, maxDegree);
int servoPositionY2 = map(joystickValueY2, minVal, maxVal, minDegree, maxDegree);

// Movimento dei primi due motori servo alle posizioni calcolate dai valori del primo joystick
servoX1.write(servoPositionX1);
servoY1.write(servoPositionY1);
// Movimento degli altri due motori servo alle posizioni calcolate dai valori del secondo joystick
servoX2.write(servoPositionX2);
servoY2.write(servoPositionY2);

// Aggiornamento della posizione dei motori servo ogni 10 millisecondi (adattabile)
delay(10);
}

Thank you for seeing this!!`

Welcome to the forum

Your topic has been moved to the Programming Questions category of the forum as it has nothing to do with the installation or operation of the IDE

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hello aceburn

Welcome to the best Arduino forum ever.

You might use potentiometers instead of a joystick.

Nice! i was thinking about using them! but which ones should i buy? 5k/10k/20k/etc..? and what type for this project should i buy linear potentiometers or rotary potentiometers?

5K or 10K.

By linear do ypu mean slide pots?

Or linear track, as opposed to audio (exponential track). In that case, certainly, linear.

With rotary pots you also have a choice of single turn or multi-turn pots. So instead of 10K in (typically) 270 degrees you can get much finer adjustment, 10K in almost 10 turns,

When using pots in place of joysticks you may need to adjust the mapping of the input as joysticks sometimes do not cover the same range as a pot, slider or rotary.

Else you could use the joysticks provided they are potentiometer type and simply remove the return springs.

Yes. Or move to a rate-orientated control method.

I can't read @aceburn's code, so forgive me if it already

just move the servo in the direction you are displacing the corresponding joypot input, at a rate proportional to how much you have displaced the stick from neutral.

When you let go of the stick and it returns to neutral, stop moving the servo.

To move, for example, the thusly repositioned servo back to where it started, you will push the joystick in the opposite direction, causing the servo to move the other way as long as you stay on the stick, only to stop when you let go and the stick returns to neutral and the servo is where you want it when it stops.

So the stick will not be an absolute indicator or input device for positioning the servos, rather it will control how fast the servo moves towards the input you are currently giving it.

In yet other words, you would be integrating the stick displacement and using that value as the absolute position of the servo.

a7

How do i remove a return spring? I saw a tutorial and it's very hard, is there a way more easier to pull out a return spring?

Ok so i tried with one 10k potentiometer and the result was like the motor servos just moving casually and bugging out so what did i do wrong?

#include <Servo.h>

// Definizione dei pin per i potenziometri
const int pinPotX1 = A0;
const int pinPotY1 = A1;
const int pinPotX2 = A2;
const int pinPotY2 = A3;

// Definizione dei pin per i motori servo
const int pinServoX1 = 9;
const int pinServoY1 = 10;
const int pinServoX2 = 11;
const int pinServoY2 = 12;

// Definizione dell'intervallo di valori dei potenziometri
const int minPotVal = 0;
const int maxPotVal = 1023;

// Definizione dell'intervallo di gradi del motore servo
const int minDegree = 0;
const int maxDegree = 180;

Servo servoX1;
Servo servoY1;
Servo servoX2;
Servo servoY2;

void setup() {
// Inizializzazione dei motori servo
servoX1.attach(pinServoX1);
servoY1.attach(pinServoY1);
servoX2.attach(pinServoX2);
servoY2.attach(pinServoY2);
}

void loop() {
// Lettura dei valori dai potenziometri
int potValueX1 = analogRead(pinPotX1);
int potValueY1 = analogRead(pinPotY1);
int potValueX2 = analogRead(pinPotX2);
int potValueY2 = analogRead(pinPotY2);

// Mappatura dei valori letti dai potenziometri nell'intervallo dei gradi dei motori servo
int servoPositionX1 = map(potValueX1, minPotVal, maxPotVal, minDegree, maxDegree);
int servoPositionY1 = map(potValueY1, minPotVal, maxPotVal, minDegree, maxDegree);
int servoPositionX2 = map(potValueX2, minPotVal, maxPotVal, minDegree, maxDegree);
int servoPositionY2 = map(potValueY2, minPotVal, maxPotVal, minDegree, maxDegree);

// Movimento dei motori servo alle posizioni calcolate dai valori dei potenziometri
servoX1.write(servoPositionX1);
servoY1.write(servoPositionY1);
servoX2.write(servoPositionX2);
servoY2.write(servoPositionY2);

// Aggiornamento della posizione dei motori servo ogni 10 millisecondi (adattabile)
delay(10);
}

Now i tried with all of them and when i try of moving the wires it moves the servos and buggs them out, i try moving a pot and it moves the servo but it buggs the other.
what should i do?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

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