const int LED_PINS[] = {2, 3, 4, 5, 6};
const int LED_COUNT = sizeof(LED_PINS) / sizeof(LED_PINS[0]);
const int JOY_X = A0;
const int JOY_BUTTON = 7;
const int LOW_THRESHOLD = 300; // наклон в сторону 0
const int HIGH_THRESHOLD = 700; // наклон в сторону 1023
const unsigned long BLINK_INTERVAL = 300;
const unsigned long DEBOUNCE_DELAY = 50;
int current = 0; // какой светодиод выбран
bool blinking = false; // мигает или горит ровно
bool stickCentered = true; // джойстик вернулся в центр
bool ledOn = true;
unsigned long lastBlink = 0;
int lastButtonReading = HIGH;
int buttonState = HIGH;
unsigned long lastDebounce = 0;
void setup() {
for (int i = 0; i < LED_COUNT; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
pinMode(JOY_BUTTON, INPUT_PULLUP);
}
void loop() {
readStick();
readButton();
updateLeds();
}
void readStick() {
int x = analogRead(JOY_X);
if (x > LOW_THRESHOLD && x < HIGH_THRESHOLD) {
stickCentered = true;
return;
}
if (!stickCentered) return;
stickCentered = false;
if (x <= LOW_THRESHOLD && current < LED_COUNT - 1) current++;
if (x >= HIGH_THRESHOLD && current > 0) current--;
ledOn = true;
lastBlink = millis();
}
void readButton() {
int reading = digitalRead(JOY_BUTTON);
if (reading != lastButtonReading) {
lastDebounce = millis();
}
lastButtonReading = reading;
if (millis() - lastDebounce < DEBOUNCE_DELAY) return;
if (reading == buttonState) return;
buttonState = reading;
if (buttonState == LOW) {
blinking = !blinking;
ledOn = true;
lastBlink = millis();
}
}
void updateLeds() {
if (blinking && millis() - lastBlink >= BLINK_INTERVAL) {
ledOn = !ledOn;
lastBlink = millis();
}
for (int i = 0; i < LED_COUNT; i++) {
bool on = (i == current) && (!blinking || ledOn);
digitalWrite(LED_PINS[i], on ? HIGH : LOW);
}
}