// Servo and ESP controller
// Joystick will control Servo Motor 1 and 2
#include <ESP32Servo.h>
//defining all the pin number
#define JOYSTICK_VERT 2
#define JOYSTICK_HORZ 4
#define JOYSTICK_SEL 15
#define SERVO_PIN_X 12
#define SERVO_PIN_Y 13
Servo servoX;
Servo servoY;
int vertValue;
int horzValue;
void setup() {
pinMode(JOYSTICK_VERT, INPUT);
pinMode(JOYSTICK_HORZ, INPUT);
servoX.setPeriodHertz(50);
servoY.setPeriodHertz(50);
servoX.attach(SERVO_PIN_X);
servoY.attach(SERVO_PIN_Y);
centerServos();
}
void loop() {
vertValue = analogRead(JOYSTICK_VERT);
horzValue = analogRead(JOYSTICK_HORZ);
// Map joystick values to servo angles
int xPos = map(horzValue, 0, 4095, 0, 180); // ESP32 ADC range is 0-4095
int yPos = map(vertValue, 0, 4095, 0, 180);
// Control servo motors
servoX.write(xPos);
servoY.write(yPos);
// here i will check if joystick is released
if (digitalRead(JOYSTICK_SEL) == LOW) {
centerServos();
}
delay(15);
}
void centerServos() {
servoX.write(90); // Center position for servo1
servoY.write(90); // Center position for servo2
}