Finally cracked Joystick program with Arduino

Опубликовано: 05 Июль 2026
на канале: let's Rock
76
5

code:-
// Define pins for joystick and LEDs
const int xPin = A0;
const int yPin = A1;
const int buttonPin = 2;
const int northLED = 3;
const int southLED = 5;
const int eastLED = 6;
const int westLED = 9;

// Define variables for joystick values
int xValue = 0;
int yValue = 0;
int buttonValue = 0;

void setup() {
// Set pins as input or output
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(northLED, OUTPUT);
pinMode(southLED, OUTPUT);
pinMode(eastLED, OUTPUT);
pinMode(westLED, OUTPUT);
}

void loop() {
// Read joystick values
xValue = analogRead(xPin);
yValue = analogRead(yPin);
buttonValue = digitalRead(buttonPin);

// Map joystick values to LED brightness values
int northBrightness = map(yValue, 0, 1023, 0, 255);
int southBrightness = map(yValue, 0, 1023, 255, 0);
int eastBrightness = map(xValue, 0, 1023, 0, 255);
int westBrightness = map(xValue, 0, 1023, 255, 0);

// Turn on LEDs based on joystick position
analogWrite(northLED, northBrightness);
analogWrite(southLED, southBrightness);
analogWrite(eastLED, eastBrightness);
analogWrite(westLED, westBrightness);
}