potentiometer to turn LED's on and off in sequence

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, OUTPUT); }
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
changeLEDs();
}


Any help would be great
Thanks

Try this for a start:-

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);}
  }
}

always use the # key when posting code

Mike, you are awesome!
This is why you are a god member, no doubt.

This worked perfectly for all the LED’s except the one connected to pin 4.
I tried fiddling with some numbers but had no luck.

This is a picture of what I have so far:

My potentiometer details, if this helps are:
Resistance: 5K ohms
+20% tolerance

Rated power: .5 watts
Sliding Noise: <47mV
Total rotation: 300 degrees + 5 degrees

Mounting hole:5/16 (8.2mm)

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.

Changing 102 to some other number won’t help. Grumpy Mike’s code has this:

if( (i * 102) > val)

When i is 0, i * 102 is 0. If val is 0, i*102 (0) is not greater than val (0), so LED[0] is never turned on.

You need to add a special case to handle turning LED[0] on.

val = analogRead(potpin);      // reads the value of the potentiometer
  for (int i = 0; i < 10; i++)
  {
    if( (i * 102) > val)
    {
      digitalWrite(ledPin[i], HIGH);
    }
    else
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
  if(val < 102)
  {
    digitalWrite(ledPin[i], HIGH);
  }
  else
  {
    digitalWrite(ledPin[i], LOW);
  }

Thanks Paul
but there seems to be problems, this is my full void loop:

void loop()
{
 val = analogRead(potpin);      // reads the value of the potentiometer
  for (int i = 0; i < 10; i++)
  {
    if( (i * 102) > val)
    {
      digitalWrite(ledPin[i], HIGH);
    }
    else
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
  if(val < 102)
  {
    digitalWrite(ledPin[i], HIGH);
  }
  else
  {
    digitalWrite(ledPin[i], LOW);
  }
}

I can’t compile and get this error:
In function ‘void loop()’:
error: name lookup of ‘i’ changed for new ISO ‘for’ scoping

void loop()
{
 val = analogRead(potpin);      // reads the value of the potentiometer
  for (int i = 0; i < 10; i++)
  {
    if( (i * 102) > val)
    {
      digitalWrite(ledPin[i], HIGH);
    }
    else
    {
      digitalWrite(ledPin[i], LOW);
    }
  [glow]}[/glow]
  if(val < 102)
  {
    digitalWrite(ledPin[i], HIGH);  // <- "i" out of scope here.
  }
  else
  {
    digitalWrite(ledPin[i], LOW);  // <- and here.
  }
}

The highlighted brace is in the wrong place, and “i” has gone out of scope.

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);}
  }
}

In the code I posted, the ledPin should have been ledPin[0] in both places. Sorry about that.

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)

Your the best,
emersonb