Can you please guide me about analog joystick ? Is it possible to control the directions and speed of two motors just like an RC Car movements with one joystick (VRx, VRy )

My example sketch only calculates speed values for the left and right motors so, yes, you do have to make changes to add code to control your particular motor drivers.

Typically you would use the Servo library for your ESC's. Something like this:

#include <Servo.h>

const byte XPotPin = A0;  // Left-Right control
const byte YPotPin = A1;  // Forward-backward throttle
const byte LeftServoPin = 2;
const byte RightServoPin = 3;

Servo LeftServo, RightServo;

const int SERVO_LIMIT_LOW = 1000;  // Used with servo.writeMilliseconds()
const int SERVO_LIMIT_HIGH = 2000;  // Used with servo.writeMilliseconds()
const int SERVO_IDLE = (SERVO_LIMIT_LOW + SERVO_LIMIT_HIGH) / 2;
const int DEAD_ZONE = 10;
const int YAW_SENSITIVITY = 100;

void setup()
{
  // Set pin modes on motor pins
  LeftServo.writeMicroseconds(SERVO_IDLE);
  LeftServo.attach(LeftServoPin);

  RightServo.writeMicroseconds(SERVO_IDLE);
  RightServo.attach(RightServoPin);
}

void loop()
{
  int speedInput = analogRead(YPotPin); // Forward/Reverse
  int yawInput = analogRead(XPotPin); // Left/Right turn

  // Map 'speedInput' to the range SERVO_LIMIT_LOW (backward), SERVO_LIMIT_HIGH (forward)
  speedInput = map(speedInput, 0, 1023, SERVO_LIMIT_LOW, SERVO_LIMIT_HIGH);

  // Map 'yawInput' to the range -YAW_SENSITIVITY (full left) to +YAW_SENSITIVITY (full right)
  yawInput = map(yawInput, 0, 1023, -YAW_SENSITIVITY, YAW_SENSITIVITY);

  // Put in dead zones
  if (speedInput > SERVO_IDLE-DEAD_ZONE && speedInput < SERVO_IDLE+DEAD_ZONE)
    speedInput = SERVO_IDLE;
  if (yawInput > -DEAD_ZONE && yawInput < DEAD_ZONE)
    yawInput = 0;

  int leftSpeed = speedInput + yawInput;
  int rightSpeed = speedInput - yawInput;

  // neither motor can go faster than maximum speed
  leftSpeed = constrain(leftSpeed, -SERVO_LIMIT_LOW, SERVO_LIMIT_HIGH);
  rightSpeed = constrain(rightSpeed, -SERVO_LIMIT_LOW, SERVO_LIMIT_HIGH);

  LeftServo.writeMicroseconds(leftSpeed);
  RightServo.writeMicroseconds(rightSpeed);
}
1 Like