Joystick Servo Position HELP!!!!

Hello,

I built a 3D printed humaniod robot and I want to control it with 2 joysticks. The problem is I need the servos to return to their home/rest positions when the joystick return to its home position. I was wondering how to do that?

Thanks.

Home/Rest Positions: Torso to rest at 90 degrees, the L-Arm to rest at 180 Degrees, and the R-Arm & Bicep to rest at 0 Degrees.

//add the servo library
#include <Servo.h>

//define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

//define joystick pins (Analog)
int joyX = 0;
int joyY = 1;

int joyX2 = 2;
int joyY2 = 3;

//variable to read the values from the analog pins 
int joyVal;
void setup ()
{
  //attaches our servos on pins PWM 3-5
  servo1.attach(3); //L-Arm
  servo2.attach(5); //Torso
  servo3.attach(6); //R-Arm
  servo4.attach(9); //R-Bicep
}

void loop ()
{
  //read the value of joystick (between 0-1023)
  joyVal = analogRead(joyX);
  joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0
  servo1.write(joyVal); //set the servo position acording to the joystick value

  joyVal = analogRead(joyY);
  joyVal = map (joyVal, 0, 1023, 0, 180);
  servo2.write(joyVal);
  delay(15);

  //read the value of joystick (between 0-1023)
  joyVal = analogRead(joyX2);
  joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 180-0
  servo3.write(joyVal); //set the servo position acording to the joystick value

  joyVal = analogRead(joyY2);
  joyVal = map (joyVal, 0, 1023, 0, 180);
  servo4.write(joyVal);
  delay(15);
  
}

This mapping look backwards???

  joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0

What are the analog values for home positions on your joysticks?

joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0
Correct this mapping is backwards beacuse when the joystick is not in use the servo goes to 180 degrees which i its home/rest position.

Also, i don't know what the analog values are for the home positions on my joystick?

So basically, what I want to do is when the joystick returns to the middle all the servos go to their rest/home positions. Even if that means when you move the joystick (let's say backwards in the x-direction) a servo won't move because it can't go further because its home position is already at 180 degrees
(like I did here: joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0)

I want to know how to do it for 90 degrees and 0 degrees because the 0 degrees home position it doesn't seem to work for the Right side.

notapro101:
So basically, what I want to do is when the joystick returns to the middle all the servos go to their rest/home positions. Even if that means when you move the joystick (let's say backwards in the x-direction) a servo won't move because it can't go further because its home position is already at 180 degrees
(like I did here: joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0)

I want to know how to do it for 90 degrees and 0 degrees because the 0 degrees home position it doesn't seem to work for the Right side.

That line of code will produce values between 0 and 1023 based on an analog input between 0 and 180. Then you will try to write that to the servo. I don't think that will be correct.

I already have it wired up and programmed like that and it works for the left arm.
I have attached a link to a video of my robot operating.

In the video, you will see the left arm go up and come back down. The left arm returns all the way down (to 180 degrees) when I release the joystick. But you will see the right arm (the one with the shield) stays in the up position. I need the right arm to be in the position shown at the end of the video (pointing down/0 degrees). (The position at the end of the video should be the home/rest positions for the servos)

Link:

I still don't understand why on Servo1 you are mapping an analog input of 0 to 180 to a servo output of 0 to 1023 when the servo write() function will only take 0 to 180 as a valid angle! Whereas, everywhere else you are mapping an analog input of 0 to 1023 to a servo output of 0 to 180.

Also, you are free to name your servos leftarm, rightarm, rightbicep, and torso. It would make your code much more readable.

I don't use analog joysticks but if you need a custom mapping you can write your own mapping functions based on the analog value. Write a simple program to display the analog values at neutral positions for the joysticks. Then based on the neutral position values you can map them to the angles any way you want.

to make it very clear

 joyVal = map(joyVal, 0, 180, 0, 1023); //servo value between 180-0
  servo1.write(joyVal); //set the servo position acording to the joystick value

  joyVal = analogRead(joyY);
  joyVal = map (joyVal, 0, 1023, 0, 180);
  servo2.write(joyVal);
joyVal = map(joyVal, 0,   180, 0, 1023); 
joyVal = map(joyVal, 0, 1023, 0, 180);

joyVal = map(joyVal, 0, 180, 0, 1023);
joyVal = map(joyVal, 0, 1023, 0, 180);

can you see the difference?

best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.

ToddL1962:
I still don't understand why on Servo1 you are mapping an analog input of 0 to 180 to a servo output of 0 to 1023 when the servo write() function will only take 0 to 180 as a valid angle!

It's been a while since I looked at the source (hint), but that didn't used to be true - above about 540, write used to act just like writeMicroseconds.
Caveat: I do not know if this is still true, but I haven't heard anyone squeal that old code is now broken.

Yes, I understand that it should be

joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 0-180

but for the left arm

joyVal = map (joyVal, 0, 180, 0, 1023); //180-0 (Left Arm)

its home/origin is 180 because I can't go further back or else the arm will snap off, so to prevent my self from accidentally pushing the wrong way on the joystick I revered the values so when you push the joystick up the Left Arm moves up, when you release the joystick the Left Arm returns to 180 degrees (origin), and if you pull the joystick down nothing will happen because the arm cannot go further back. This part of the code WORKS.

The problem I'm having is with the Right Side. I can't do what I did with the Left Side because the Right Side servo's origin/Home positions are 0 degrees.

I want to know what should the values for the right side be, such that when I release the joystick the servos go to 0 degrees.

I also got this tutorial from this link:

Here is my tweaked code (I just Switched some pin numbers around)

//add the servo libary
#include <Servo.h>

//define our servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

//define joystick pins (Analog)
int joyX = 0;
int joyY = 1;

int joyX2 = 2;
int joyY2 = 3;

//variable to read the values from the analog pins 
int joyVal;
void setup ()
{
  //attaches our servos on pins PWM 3-5
  servo1.attach(5); //L-Arm
  servo2.attach(3); //Torso
  servo3.attach(9); //R-Arm
  servo4.attach(6); //R-Bicep
}

void loop ()
{
  //read the value of joystick (between 0-1023)
  joyVal = analogRead(joyX);
  joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 0-180 
  servo1.write(joyVal); //set the servo position acording to the joystick value

  joyVal = analogRead(joyY);
  joyVal = map (joyVal, 0, 180, 0, 1023); //180-0 
  servo2.write(joyVal); // Left Arm
  delay(15);

  //read the value of joystick (between 0-1023)
  joyVal = analogRead(joyX2);
  joyVal = map(joyVal, 0, 1023, 0, 180); //servo value between 0-180
  servo3.write(joyVal); //R-Arm

  joyVal = analogRead(joyY2);
  joyVal = map (joyVal, 0, 1023, 0, 180);
  servo4.write(joyVal); //R-Bicep
  delay(15);
  
}

So basically when my joystick is at 90 degrees (it's center/origin on the x and y plane) I need the Right Sided servos to be at 0 degrees.

(Right now when my joystick is at 90 degrees the Right side servos are also at 90 degrees. I need them to be at 0 degrees).

(The left side is fine because when that joystick is at 90 degrees the servo is at 180 degrees, which is what I want).