#include <Arduino.h>
// Define LED pins
const int led1 = 2; // Pin for LED 1
const int led2 = 3; // Pin for LED 2
// Define joystick pins
const int joystickX = A0; // Analog input pin for joystick X-axis (turning)
const int joystickY = A1; // Analog input pin for joystick Y-axis (forward/backward)
// Define a deadzone for the joystick (optional)
const int joystickDeadzone = 20; // Minimum joystick value to register movement
void setup() {
// Set LED pins as outputs
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
// Read the joystick values
int joystickXValue = analogRead(joystickX);
int joystickYValue = analogRead(joystickY);
// Apply deadzone (optional) - ignore small joystick movements
joystickXValue = abs(joystickXValue - 512) > joystickDeadzone ? joystickXValue - 512 : 0;
joystickYValue = abs(joystickYValue - 512) > joystickDeadzone ? joystickYValue - 512 : 0;
// Control LEDs based on joystick position
if (joystickYValue > joystickDeadzone) { // Forward (positive Y)
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
// Blink LEDs (optional)
delay(250); // Delay for half a second (adjust as needed)
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(250); // Delay for half a second (adjust as needed)
} else if (joystickYValue < -joystickDeadzone) { // Backward (negative Y)
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
// Blink LEDs (optional) - similar blinking logic as forward
} else if (joystickXValue > joystickDeadzone) { // Right (positive X)
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
} else if (joystickXValue < -joystickDeadzone) { // Left (negative X)
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
} else { // Center position (both X and Y within deadzone)
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
}