Hey guys, really new to Arduino (1 week) and trying an LED project!
I would like to have a series of LED’s (on PWM outputs), use a potentiometer to fade up the series from LED 1-5. LED 1 would fade from 0-255, then when it hits max value, LED 2 would begin its fade from 0-255 and so on throughout each LED. I’ve accomplished this on another project using digitalWrite for HIGH/LOW values, so they are either on or off and can produce a series effect, however I would like to see if it’s possible to make each LED fade from min-max value before the next one turns on, then reverse the effect as the pot goes back down. (Essentially this will be a precursor to using a microphone as input for a PFL, and the LED’s will show the input volume level visually)
Currently, when I run this program below, the first LED1 will fade up perfectly, however when it reaches max value, the next LED2 will turn on FULL, drop to minimum, and both LED’s will fade up with the pot. Once it reaches max, again the next LED will turn on FULL, then drop to minimum then all LED’s fade up, etc. I assume I’m missing some lines of code to tell the LED’s their min and maximum values, or do I need thresholds? I’m trying to make my code as short and simple as possible without having to write endless unnecessary lines. Hoping the awesome arduino community can give me some tips. Let me know if this is in the wrong forum area. Cheers guys.
P.S I have attached a schematic of my circuitry and wiring as well
int timer = 2; // delay in milliseconds
int ledPins [] { 5, 6, 9, 10, 11}; // led array output to PWM pins
int pinCount = 5; // amount of pins
const int potInput = A0; // Pot input on pin A0
int outputValue = 0;
void setup() {
for (int pin = 0; pin < pinCount; pin++) {
pinMode (ledPins[pin], OUTPUT); // set Led array as Output
}
Serial.begin(9600);
}
void loop() {
int sensorInput = analogRead(potInput);
outputValue = map(sensorInput, 0, 1023, 0, 255); // Read input and map to led output values
for (int pin = 0; pin < pinCount; pin++)
{
if ((pin * 51) < outputValue) // Used 255 divided by number of outputs (5)
{
analogWrite(pin[ledPins], sensorInput);
}
else
{
digitalWrite(pin[ledPins], LOW); // pins above value don't turn on yet
}
}
delay(timer);
Serial.println(sensorInput);
}