#include <ESP32Servo.h>
const int joystickX = 2;
const int joystickX1 = 35;
const int servoPin = 32;
const int servoPin1 = 33;
const int sel = 0;
const int sel1 = 34;
const int led = 17;
const int led1 = 16;
Servo myServo;
Servo myServo1;
const int deadZone = 100;
const int servoCenter = 90;
unsigned long previousMillis = 0;
const long interval = 50;
bool ledState = LOW;
void setup()
{
Serial.begin(115200);
myServo.attach(servoPin);
myServo1.attach(servoPin1);
myServo.write(servoCenter);
myServo1.write(servoCenter);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(sel, INPUT_PULLUP);
pinMode(sel1, INPUT_PULLUP);
}
void loop()
{
int xValue = analogRead(joystickX);
int xValue1 = analogRead(joystickX1);
// Map joystick values to servo angles
int servoAngle = map(xValue, 0, 4095, 0, 180);
int servoAngle1 = map(xValue1, 0, 4095, 0, 180);
if (abs(xValue - 2048) > deadZone)
{
myServo.write(servoAngle);
blinker(led);
}
if (abs(xValue1 - 2048) > deadZone)
{
myServo1.write(servoAngle1);
blinker(led1);
}
if (digitalRead(sel) == LOW)
{
myServo.write(servoCenter);
delay(50);
}
if (digitalRead(sel1) == LOW)
{
myServo1.write(servoCenter);
delay(50);
}
delay(100);
}
void blinker(int ledPin)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}