Servo position hold while operating with Joystick

Hi,
I am working on a project where I want to operate two servo motors using joystick.
Joystick should give incremental position to servo motors in any specific direction.
Servo motors shouldnt return back if joystick returns to home position.

Here is the tinkercad circuit link

Here is the code I am working on, but still not successful.
Need your suggestions to correct the code.

#include <Servo.h>

Servo LHRH_servo;
Servo UPDN_servo;

int joyX = A5;
int joyY = A4;

int ServoH_Min = 0;
int ServoH_Max = 60;
int ServoV_Min = 40;
int ServoV_Max = 0;
int pan = 0;

int HorizontalJoyValue;
int HorizontalServoPosition;
int VerticalJoyValue;
int VerticalServoPosition;

void setup()
{
pinMode(joyX, INPUT);
pinMode(joyY, INPUT);
UPDN_servo.attach(12);
LHRH_servo.attach(11);
LHRH_servo.write(30);
UPDN_servo.write(20);
Serial.begin(9600);
}

void loop()
{
HorizontalJoyValue = analogRead(joyX);
VerticalJoyValue = analogRead(joyY);
Serial.print("HorizontalJoyValue = ");
Serial.println(HorizontalJoyValue);
Serial.print("VerticalJoyValue = ");
Serial.println(VerticalJoyValue);
HorizontalServoPosition = map(HorizontalJoyValue, 0, 1023, ServoH_Min, ServoH_Max);
VerticalServoPosition = map(VerticalJoyValue, 0, 1023, ServoV_Min , ServoV_Max);
Serial.print("HorizontalServoPosition = ");
Serial.println(HorizontalServoPosition);
Serial.print("VerticalServoPosition = ");
Serial.println(VerticalServoPosition);

if (HorizontalServoPosition < ServoH_Max){
if (HorizontalServoPosition > (ServoH_Max / 2)) {
pan++;
} else {
if (HorizontalServoPosition < (ServoH_Max / 2)){
pan--;
}
}
}
LHRH_servo.write(pan);
delay(2000);
Serial.println(LHRH_servo.read());

if (LHRH_servo.read() >= ServoH_Max) {
pan = ServoH_Max;
LHRH_servo.write(pan);
delay(2000);
}

if (VerticalServoPosition < ServoV_Min){
if (VerticalServoPosition > (ServoV_Min / 2)) {
pan++;
} else {
if (VerticalServoPosition < (ServoV_Min / 2)){
pan--;
}
}
}
UPDN_servo.write(pan);
delay(2000);
Serial.println(UPDN_servo.read());
if (UPDN_servo.read() >= ServoV_Min) {
pan = ServoV_Min;
UPDN_servo.write(pan);
delay(2000);
}

}

Please, use tags </>
and read this:

RV mineirin

One way to do it:

#include <Servo.h>

Servo LHRH_servo;
Servo UPDN_servo;

int XPin = A5;
int YPin = A4;

const int ServoX_Min = 0;
const int ServoX_Max = 60;
const int ServoY_Min = 40;
const int ServoY_Max = 0;

int XPosition = (ServoX_Min + ServoX_Max) / 2;
int YPosition = (ServoY_Min + ServoY_Max) / 2;

void setup()
{
  UPDN_servo.attach(12);
  LHRH_servo.attach(11);
  LHRH_servo.write(XPosition);
  UPDN_servo.write(YPosition);
  Serial.begin(9600);
}

void loop()
{
  int xVal = (analogRead(XPin) - 512) / 100; // -5 to +5
  int yVal = (analogRead(YPin) - 512) / 100; // -5 to +5
  
  Serial.print("xVal = ");
  Serial.println(xVal);
  Serial.print("yVal = ");
  Serial.println(yVal);
  
  XPosition = constrain(XPosition + xVal, ServoX_Min, ServoX_Max);
  YPosition = constrain(YPosition + yVal, ServoY_Min, ServoY_Max);
  
  Serial.print("XPosition = ");
  Serial.println(XPosition);
  Serial.print("YPosition = ");
  Serial.println(YPosition);

  LHRH_servo.write(XPosition);
  UPDN_servo.write(YPosition);
}
1 Like

If you want help here, post your schematic here, not on an outside site. I, for one, will not make an account on a site that does not interest me just to see your schematic.

Use the IDE autoformat tool to format your code so that it is more readable before posting in code tags.

That conveys no information that we can use to help you. What does the code actually do and how does that differ from what you want?

Dear Johnwasser,
Thank you so much for reply.

I have used the code and it works perfectly for one Servo.
But other servo is not moving with joystick. I have verified the connections and with different servo also.

I am getting following observation in serial monitor.

HorizontalJoyValue = 0
VerticalJoyValue = 0
HorizontalServoPosition = 44
VerticalServoPosition = 40
44
40
HorizontalJoyValue = 0
VerticalJoyValue = 0
HorizontalServoPosition = 44
VerticalServoPosition = 0
44
0

sometimes, Y position in 0 and next time it is 40. this keeps happening in loop and appearing in serial monitor.
First servo works well as needed and X position values are also as expected.

Can you please suggest if I am missing anything here.

Dear groundFungus,

Thank you for your reply.
I will keep in mind points mentioned by you.

Attached is the image of the circuit.
Also, earlier shared link is of Autodesk Sketchbook, no need to make account to access the complete circuit with code. Even it can be simulated there without account.
I was not aware of IDE tool, here, next time I will definitely use it while pasting the code.

Function expected is

  • joystick will operate two servo motors, both are perpendicular to each other
  • once joystick is operated then relevant servo motor should rotate by specific degree but should not return to home position when joystick returns, unless joystick is operated in opposite direction (like keeping last position of servo motor as it is)

I think this might be a problem with the constrain() function. You are using _Min for the maximum value and _Max for the minimum value. Try swapping them.

1 Like

If you carry on with the project you may find that powering the servos from the Uno 5V regulator will be problematic. Servos need a lot of current to start moving. Even the mini servos can pull 1A (stall or starting current) or more each time that they start. One servo, unloaded, may work, but if you try to start 2 servos that are loaded I would not be surprised that the Uno resets. Use an external supply that can handle the combined stall current of the servos. The servo data sheet should list the stall current specification. Be sure to connect the external power supply ground to the Uno ground.

Really you are genius, appreciate your support.
Your guess was exactly right.

after changing the values of Min and Max, both motors are working perfect.
Does it means that, in Constrain function minimum value should come first and maximum value later?

Thanks and Regards

Hi,

I am using MG90 servo and following are specifications

  • Operating voltage: 4.8V~ 6.6V
  • Current (idle) 10mA (typical)
  • Current (typical during movement) 120-250mA
  • Current (stall) 700mA (measured)
  • Stall torque @4.8V : 1.8kg-cm
  • Stall torque @6.6V : 2.2kg-cm

but sure, your suggestion is really good, I will plan for external power supply instead of arduino 5v pin.

Thanks

Yes.

Documented here:
https://www.arduino.cc/reference/en/language/functions/math/constrain/