#include <Arduino.h>
#include <ESP32Servo.h> // Make sure you have this library installed
// Joystick pins
const int vertPin = 34; // Vertical
const int horzPin = 35; // Horizontal
const int selPin = 32; // Button (select)
// Servo pins
const int servoVertPin = 14;
const int servoHorzPin = 12;
// Servo objects
Servo servoVert;
Servo servoHorz;
// For mapping joystick range to servo angle (0–180)
int mapJoystickToServo(int value) {
// ESP32 analogRead default: 0–4095
return map(value, 0, 4095, 0, 180);
}
void setup() {
Serial.begin(115200);
// Attach servos
servoVert.attach(servoVertPin);
servoHorz.attach(servoHorzPin);
// Joystick button input
pinMode(selPin, INPUT_PULLUP);
}
void loop() {
// Read joystick values
int vertValue = analogRead(vertPin);
int horzValue = analogRead(horzPin);
int selValue = digitalRead(selPin); // LOW when pressed if using INPUT_PULLUP
// Map joystick to servo
int vertAngle = mapJoystickToServo(vertValue);
int horzAngle = mapJoystickToServo(horzValue);
// Move servos
servoVert.write(vertAngle);
servoHorz.write(horzAngle);
// Debug info
Serial.print("Vert: "); Serial.print(vertValue);
Serial.print(" -> "); Serial.print(vertAngle);
Serial.print("\tHorz: "); Serial.print(horzValue);
Serial.print(" -> "); Serial.print(horzAngle);
Serial.print("\tSelect: "); Serial.println(selValue);
delay(50); // Smooth control
}