#include <Arduino.h>
// Define the analog input pins for the joystick
const int VERT_PIN = A2;
const int HORZ_PIN = A3;
// Define the digital output pins for the LEDs
const int RED_LED_PIN = 6;
const int BLUE_LED_PIN = 8;
const int YELLOW_LED_PIN = 3;
const int GREEN_LED_PIN = 5;
// Define the values for each joystick position
const int NEUTRAL_VERT = 0;
const int NEUTRAL_HORZ = 0;
const int LEFT_HORZ = -100;
const int RIGHT_HORZ = 100;
const int UP_VERT = 100;
const int DOWN_VERT = -100;
void setup() {
// Initialize the analog input pins as inputs
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
// Initialize the digital output pins as outputs
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
}
void loop() {
// Read the analog input values from the joystick
int vertValue = analogRead(VERT_PIN);
int horzValue = analogRead(HORZ_PIN);
// Map the analog input values to a range of -100 to 100
int vertMapped = map(vertValue, 1, 900, -200, 100);
int horzMapped = map(horzValue, 0, 1023, 100, -100);
// Use the mapped values to control the LEDs
if (horzMapped > LEFT_HORZ && horzMapped < RIGHT_HORZ) {
// When the joystick is in neutral position (horizontally), turn off all LEDs
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
} else if (horzMapped > LEFT_HORZ) {
// When the joystick is moved to the left, turn off red and yellow, turn on blue and yellow
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
} else if (horzMapped < RIGHT_HORZ) {
// When the joystick is moved to the right, turn off blue and yellow, turn on red and yellow
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
}
if (vertMapped > UP_VERT) {
// When the joystick is moved up, turn off all LEDs except green and red
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
} else if (vertMapped < DOWN_VERT) {
// When the joystick is moved down, turn off all LEDs except red and yellow
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
} else {
// When the joystick is in neutral position (vertically), turn off all LEDs
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
}