Pot controlled LED's problem

:slight_smile: Hello, I have a piece of code modified from emersonb.
For my purposes, the pot allows me to scroll back and fourth along a line of LED’s, lighting up one at a time.
As each LED lights up, the previous one turns off.
My problem is this: When the second LED in the array lights,the first one is completely off, which is perfect but when the third LED lights, the second one stays half lit. This continues for the fourth, fifth and sixth LED’s. However, as I return back towards the first LED, all the LED’s turn off perfectly.
How do I solve this problem? Any help much appreciated :slight_smile: :slight_smile:

byte ledPin[] = { 2, 4, 6, 7, 8, 9,  }; //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 < 6; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
 val = analogRead(potpin);	// reads the value of the potentiometer
  for (int i = 0; i < 6; i++)
  {
    if (val < 170) 
    {
	digitalWrite(ledPin[0], HIGH);
    }
    else
    {
	digitalWrite(ledPin[0], LOW);
    }
  
  if(val > 170)
  {
    digitalWrite(ledPin[1], HIGH);  // <- "i" out of scope here.
    digitalWrite(ledPin[0], LOW);
  }
  else
  {
    digitalWrite(ledPin[1], LOW);  // <- and here.
    
  }
  
   if(val > 340)
  {
    digitalWrite(ledPin[2], HIGH);  // <- "i" out of scope here.
    digitalWrite(ledPin[1], LOW);
  }
  else
  {
    digitalWrite(ledPin[2], LOW);  // <- and here.
  }
  
   if(val > 510)
  {
    digitalWrite(ledPin[3], HIGH);  // <- "i" out of scope here.
    digitalWrite(ledPin[2], LOW);
  }
  else
  {
    digitalWrite(ledPin[3], LOW);  // <- and here.
  }
  
   if(val > 680)
  {
    digitalWrite(ledPin[4], HIGH);  // <- "i" out of scope here.
    digitalWrite(ledPin[3], LOW);
  }
  else
  {
    digitalWrite(ledPin[4], LOW);  // <- and here.
  }
  
   if(val > 850)
  {
    digitalWrite(ledPin[5], HIGH);  // <- "i" out of scope here.
    digitalWrite(ledPin[4], LOW);
  }
  else
  {
    digitalWrite(ledPin[5], LOW);  // <- and here.
  }
  }
}

Look at your if statements

If val> 340 val is also > 170 so both LEDs will be turned on

You need something like:

Pseudocode:
if val > 170 and val <340 then …

I would try another approach

byte ledPin[] = { 2, 4, 6, 7, 8, 9,  }; //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 < 6; x++) {
   pinMode (ledPin[x], OUTPUT); }
}

void loop()
{
 val = analogRead(potpin);	// reads the value of the potentiometer
 
 val = map (val, 0, 1023, 0, 5); // map value to a pin
 
 for (int i = 0; i < 6; i++)  // clear all pins
	digitalWrite (ledPin[i], LOW);

  digitalWrite (val, HIGH);  // set selected pin

}

With either approach though you may get jitter on the boundary conditions (eg 169,170,171) .


Rob

:)Thanks MikMo & Thanks Graynomad!!! I tried this and it works…if((val > 170) && (val < 340)) :slight_smile: I will try the mapping as well though! :slight_smile: