All submissions

Solving Approach:

How do you plan to solve it?

 

Code

const int ledPins[] = {11, 10, 9, 6, 3};
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int potPin = A0;

void setup() {
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  int potValue = analogRead(potPin);

  int activeLedIndex = potValue / (1024 / numLeds);

  if (activeLedIndex >= numLeds) {
    activeLedIndex = numLeds - 1;
  }

  for (int i = 0; i < numLeds; i++) {
    if (i < activeLedIndex) {
      analogWrite(ledPins[i], 255);
    } else if (i == activeLedIndex) {
      int segmentMinVal = activeLedIndex * (1024 / numLeds);
      int segmentMaxVal = (activeLedIndex + 1) * (1024 / numLeds) - 1;
      
      if (activeLedIndex == numLeds - 1) {
          segmentMaxVal = 1023;
      }
      
      int brightness = map(potValue, segmentMinVal, segmentMaxVal, 0, 255);
      analogWrite(ledPins[i], brightness);
    } else {
      analogWrite(ledPins[i], 0);
    }
  }
}



 

Output

Video

Add video of output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

 

 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!