/**
Raspberry Pi Pico LED Bar Graph Binary Counter with Joystick
https://wokwi.com/arduino/projects/309828467927548481
Copyright (C) 2021 Uri Shaked
*/
const int pins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
const int joystickPin = 0; // Аналоговий вхід для джойстика
const int joystickCenter = 512; // Центральне значення джойстика
void setup() {
for (int i = 0; i < 10; i++) {
pinMode(pins[i], OUTPUT);
}
}
int counter = 0;
void loop() {
// Зчитуємо значення аналогового входу джойстика
int joystickValue = analogRead(joystickPin);
// Визначаємо напрямок руху джойстика (вгору, вниз або в центрі)
int direction = 0;
if (joystickValue < joystickCenter - 50) {
direction = -1; // Вниз
} else if (joystickValue > joystickCenter + 50) {
direction = 1; // Вгору
}
// Вмикаємо лічильник у відповідному напрямку
counter += direction;
// Обмежуємо лічильник від 0 до 1023
counter = constrain(counter, 0, 1023);
// Перетворюємо лічильник у бінарний формат та виводимо на світлодіоди
for (int i = 0; i < 10; i++) {
digitalWrite(pins[i], bitRead(counter, i) ? HIGH : LOW);
}
delay(50);
}