#include <Servo.h>
// Pin definitions
const int x_key = A1; // Joystick X-axis
const int leftLED = 7; // Left indicator LED
const int rightLED = 8; // Right indicator LED
const int centerLED = 13; // Center (neutral) indicator LED
Servo leftServo; // Left wheel servo motor
Servo rightServo; // Right wheel servo motor
int initialPositionLeft = 90; // Neutral position for left wheel
int initialPositionRight = 90; // Neutral position for right wheel
// Thresholds for joystick movement
const int thresholdLeft = 300; // Joystick left movement threshold
const int thresholdRight = 700; // Joystick right movement threshold
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the LEDs
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
pinMode(centerLED, OUTPUT);
// Attach servos to pins
leftServo.attach(9);
rightServo.attach(10);
// Set both servos to a straight position
leftServo.write(initialPositionLeft);
rightServo.write(initialPositionRight);
// Set center LED (neutral indicator)
digitalWrite(centerLED, HIGH); // Center light ON by default
}
void loop() {
// Read joystick position
int x_pos = analogRead(x_key);
// If joystick is pushed left (turn left)
if (x_pos < thresholdLeft) {
// Turn on left LED (indicate left turn)
digitalWrite(leftLED, HIGH);
digitalWrite(centerLED, LOW); // Center LED off during turn
digitalWrite(rightLED, LOW); // Ensure right LED is off
// Move left servo (simulate left wheel turning)
leftServo.write(initialPositionLeft - 30); // Adjust value for left turn
rightServo.write(initialPositionRight + 30); // Adjust value for left turn
// Delay to simulate blinking of the indicator
delay(500);
digitalWrite(leftLED, LOW);
delay(500);
// If joystick is pushed right (turn right)
} else if (x_pos > thresholdRight) {
// Turn on right LED (indicate right turn)
digitalWrite(rightLED, HIGH);
digitalWrite(centerLED, LOW); // Center LED off during turn
digitalWrite(leftLED, LOW); // Ensure left LED is off
// Move right servo (simulate right wheel turning)
leftServo.write(initialPositionLeft + 30); // Adjust value for right turn
rightServo.write(initialPositionRight - 30); // Adjust value for right turn
// Delay to simulate blinking of the indicator
delay(500);
digitalWrite(rightLED, LOW);
delay(500);
// If joystick is in neutral position
} else {
// Turn off the left and right LEDs, turn on the center LED
digitalWrite(leftLED, LOW);
digitalWrite(rightLED, LOW);
digitalWrite(centerLED, HIGH);
// Reset servo motors to the neutral position
leftServo.write(initialPositionLeft);
rightServo.write(initialPositionRight);
}
}