I would like to be able to turn a potentiometer and make it turn LED's on and off in sequence.
Exactly like the video link below:
Here is a video of someone who figured this out:
This picture represents my connections:
I have the code started but I need help.
// Controlling LED's using a potentiometer (variable resistor)
byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; //pins used for LED's
int potpin = 2; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
// set all pins to output
for (int x = 0; x < 10; x++) {
pinMode (ledPin[x], OUTPUT); }
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
changeLEDs();
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
for (int i = 0; i < 10; i++) {
if( (i * 102) > val) { digitalWrite(ledPin[i], HIGH);}
else
{ digitalWrite(ledPin[i], LOW);}
}
}
Most likely the analog read value is not varying enough. Probably because the POT isnt giving a full output when it is at its heighest setting. Change the value 102 to something like 80 that should work then you'll have: 0-80 LED 1 81-160 LED 2 161-240 LED 3 241-320 LED 4 321-400 LED 5 401-480 LED 6 481-560 LED 7 561-640 LED 8 641-720 LED 9 721-1023 LED 10 Of course you could also have Serial.println tell you exactly the range of analog values then you could fine tune your app so that it works just perfectly.
Woops thats what i get for not reading the code properly. See below.
I did say that code was for starters. The first LED won't light because when i = 0 then (i * 102) is always going to be zero as well. Just put a special test in front as hi lighted in yellow
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
for (int i = 0; i < 10; i++) {
[glow]if( val > 51) { digitalWrite(ledPin[0], HIGH);}
else
{ digitalWrite(ledPin[0], LOW);}[/glow]
if( (i * 102) > val) { digitalWrite(ledPin[i], HIGH);}
else
{ digitalWrite(ledPin[i], LOW);}
}
}
Thanks, Mike
I should have been more descriptive in the direction of the lights.
I flipped the carrot symbol and it works exactly how I wanted, thanks to you.
from this: ( val > 51) to this: ( val [glow]<[/glow] 51)