#include <Servo.h>
Servo motor1;
Servo motor2;
int joyPinX = A0; // Joystick X-axis pin
int joyPinY = A1; // Joystick Y-axis pin
int joyThreshold = 20; // Joystick deadzone threshold
void setup() {
motor1.attach(3); // Attach motor1 to pin 3
motor2.attach(5); // Attach motor2 to pin 5
}
void loop() {
int joyX = analogRead(joyPinX); // Read joystick X-axis
int joyY = analogRead(joyPinY); // Read joystick Y-axis
// Apply deadzone to joystick readings
if (abs(joyX - 512) < joyThreshold) {
joyX = 512;
}
if (abs(joyY - 512) < joyThreshold) {
joyY = 512;
}
// Map joystick readings to motor speeds
int speed1 = map(joyY, 0, 1023, -255, 255);
int speed2 = map(joyY, 0, 1023, -255, 255);
// Calculate motor speed differentials for turning
int turnDiff = map(joyX, 0, 1023, -255, 255);
speed1 += turnDiff;
speed2 -= turnDiff;
// Set motor speeds
motor1.writeMicroseconds(1500 + speed1);
motor2.writeMicroseconds(1500 - speed2);
}