const int ledPins[] = {3, 5, 6, 9, 10, 11};
const int numLeds = sizeof(ledPins) / sizeof(int);
const int xPin = A0;
const int yPin = A1;
const int buttonPin = 2;
int activeLed = 0;
int brightness = 125;
bool buttonPressed = false;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
static unsigned long lastReadTime = 0;
if (millis() - lastReadTime >= 100) {
lastReadTime = millis();
readJoystick();
updateLEDs();
}
}
void readJoystick() {
int xValue = analogRead(xPin);
int yValue = analogRead(yPin);
bool buttonState = digitalRead(buttonPin);
if (xValue > 600 || xValue < 400) {
if (xValue > 600 && activeLed < numLeds - 1) {
activeLed++;
} else if (xValue < 400 && activeLed > 0) {
activeLed--;
}
}
if (yValue > 600) {
if (brightness < 250) {
brightness += 10;
}
} else if (yValue < 400) {
if (brightness > 0) {
brightness -= 10;
}
}
if (!buttonState && !buttonPressed) {
activeLed = 0;
buttonPressed = true;
} else if (buttonState && buttonPressed) {
buttonPressed = false;
}
}
void updateLEDs() {
for (int i = 0; i < numLeds; i++) {
analogWrite(ledPins[i], 0);
}
analogWrite(ledPins[activeLed], brightness);
}