const int potPin = A0; // Analog input pin for the potentiometer
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10}; // Array of output pins for LEDs
const int numLeds = 9; // Number of LEDs
void setup() {
pinMode(ledPins[0], OUTPUT); // Set the first LED pin as output
digitalWrite(ledPins[0], HIGH); // Initially, turn on the first LED
for (int i = 1; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT); // Set the remaining LED pins as outputs
digitalWrite(ledPins[i], LOW); // Initially, turn off the other LEDs
}
}
void loop() {
int potValue = analogRead(potPin); // Read potentiometer value (0-1023)
// Map the potentiometer value to the range 0-7000
int mappedValue = map(potValue, 0, 1023, 0, 7000);
// Calculate how many LEDs to turn on based on the mapped value
int ledCount = map(mappedValue, 0, 7000, 0, numLeds - 1);
// Turn on the LEDs based on the calculated count, starting from the second LED
for (int i = 1; i <= ledCount; i++) {
digitalWrite(ledPins[i], HIGH);
}
// Turn off any LEDs that are not turned on
for (int i = ledCount + 1; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}