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