Hi All,
I am a beginner in arduino and have five LEDs in a row connected to my arduino as well as a potentiometer, I am wondering how I can use the potentiometer to select which led to light. I know i could write an if statement for each LED however I’m sure there is a simpler way to do it.
P.S
I have mapped the potentiometer to min 1 and max 5.
Change the map() to give you 0 to 4 instead of 1 to 5 then put the LED pin numbers in an array and use the number as the array index to turn on the associated LED.
Thanks for the quick reply, I’ve done what you said and it is half working, the LEDs turn on when selected with the potentiometer however when I move the potentiometer they do not turn off.
Here is my code
//Led Pin array
int ledPins[] = {2, 3, 4, 5, 6};
void setup() {
//Makes pins 2-6 OUTPUT pins
for(int thisPin = 2; thisPin <7; thisPin++){
pinMode(thisPin, 1);
}
}
void loop() {
int val = analogRead(A0); //Sets val to the potentiometer value
val = map(val, 0, 1023, 0, 4);// Maps the value of val to 0-4
digitalWrite(ledPins[val], 1);// Turns on the 'val' LED
}
I tried turning off the LED straight after I turned it on however the LED became extremely dim.
however when I move the potentiometer they do not turn off.
That should not be a surprise. One way to do it would be to turn off all LEDs before turning on a new one which you will know is a new one because the value returned will have changed. Alternatively just turn off the current LED before turning on a new one.
I would just loop all the leds an put them in the needed state.
Made some changes with comments to you code. Please read them
//Led Pin array
const byte LedPins[] = {2, 3, 4, 5, 6}; //cont => they never change, byte => is large enough
const byte PotPin = A0; //Name all your pins
void setup() {
//Makes pins 2-6 OUTPUT pins
//Use your array to select them!
for(byte i = 0; i < sizeof(LedPins); i++){
pinMode(LedPins[i], OUTPUT); //Use the OUTPUT define to make it readable!
}
}
void loop() {
byte ledSelect = map(analogRead(PotPin), 0, 1023, 0, sizeof(LedPins) - 1);// Maps the value of val to 0 - number of LEDs
//loop all the leds
for(byte i = 0; i < sizeof(LedPins); i++){
//turns on the led where it's true that ledSelect == i
//aka turn on the selected led and turns the rest off
digitalWrite(LedPins[i], ledSelect == i);
}
}
Now it’s also easy to change the number of leds. Just add more leds to the LedPin array and you’re done
It’s good design to only change output values when data changes. It’s harmless in this case, but some functions and some hardware can’t be “hammered” that way.
void loop() {
byte ledSelect = map(analogRead(PotPin), 0, 1023, 0, sizeof(LedPins) - 1);// Maps the value of val to 0 - number of LEDs
static byte previousLEDSelect; // keep track of the pin output state
if (ledSelect != previousLEDSelect) // if the state has changed
{
//loop all the leds
for (byte i = 0; i < sizeof(LedPins); i++) {
//turns on the led where it's true that ledSelect == i
//aka turn on the selected led and turns the rest off
digitalWrite(LedPins[i], ledSelect == i); // write the pin
}
ledSelect = previousLEDSelect; // remember the new pin
}
}