// Include necessary libraries
#define POT_PIN 34 // Potentiometer connected to analog pin 34
#define BUTTON_PIN 25 // Push button connected to GPIO 25
const int ledCount = 10;
int ledPins[] = {
2, 4, 5, 12, 13, 14, 18, 19, 21,22
};
void setup() {
// Initialize LED pins as output
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
digitalWrite(ledPins[thisLed], LOW);
}
// Initialize button as input with pull-up resistor
pinMode(BUTTON_PIN, INPUT);
// Initialize Serial for debugging
Serial.begin(115200);
}
void loop() {
// Check if button is pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
// Read potentiometer value
int potValue = analogRead(POT_PIN);
// Map potentiometer value to LED count
int ledLevel = map(potValue, 0, 4095, 0, ledCount);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
else{
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
digitalWrite(ledPins[thisLed], LOW);
}
}
delay(100); // Small delay for debouncing and smooth updates
}