#include <esp32-hal-ledc.h>
#include <ESP32Servo.h>
#define THROTTLE_PIN 36
#define STEERING_PIN 39
#define MOTOR_PWM_PIN 19
#define SERVO_PIN 18
Servo steeringServo;
void setup() {
Serial.begin(115200);
ledcAttach(MOTOR_PWM_PIN, 5000, 8); // attach GPIO19 to channel 0
steeringServo.attach(SERVO_PIN, 500, 2500); // (pin, min us, max us)
}
void loop() {
int throttleValue = analogRead(THROTTLE_PIN);
const int threshold = 500;
int motor_speed = 0;
if (throttleValue > threshold) {
motor_speed = map(throttleValue, threshold, 4095, 0, 255);
} else {
motor_speed = 0;
}
ledcWrite(0, motor_speed);
int steeringValue = analogRead(STEERING_PIN);
int angle = map(steeringValue, 0, 4095, -45, 45);
int servoPos = map(angle, -45, 45, 0, 180);
steeringServo.write(servoPos);
Serial.print("Throttle: "); Serial.print(throttleValue);
Serial.print(" | Motor Speed: "); Serial.print(motor_speed);
Serial.print(" | Steering angle: "); Serial.print(angle);
Serial.print(" | Servo Pos: "); Serial.println(servoPos);
delay(100);
}