Using potentiometer to control speed of LED "rail"

Hello, I have an LED "rail" that lights each LED from left to right, then right to left, continuously. I'm trying to control the speed of the delay with a potentiometer. With the code I currently have, it seems like it "should" just count the potentiometer value, then plug that number into delay. But it doesn't seem to work. It just switches LED state about every half a second or so, which seems like an arbitrary delay value which I can't determine where it's coming from.

Here is my code:

int ledPins[]={2,3,4,5,6,7,8,9,10};

int potSpeed = 0;

int potPin = A0;

void setup(){
  //Serial.begin(9600);
  for (int i =0; i <9; i++)
  {
    pinMode(ledPins[i],OUTPUT);
  }
  pinMode(potPin, INPUT);
}

void loop(){
  int potValue = analogRead(potPin);
  /*
  Serial.print("Pot Value: ");
  Serial.print(potValue);
  Serial.println("");
  */

  potSpeed = potValue;
  
  int i = 0;
  while (i<8)
  {
    {
      digitalWrite(ledPins[i],HIGH);
      delay(potSpeed);
      digitalWrite(ledPins[i],LOW);
      i++;
    }
  }

  i = 8;
  while (i >0)
  {
    digitalWrite(ledPins[i],HIGH);
    delay(potSpeed);
    digitalWrite(ledPins[i],LOW);
    i--;
  }
}

If you uncomment your debug output, what does it say?

pert:
If you uncomment your debug output, what does it say?

All of the led's just barely light up.

I think it draws too many mA when un-commenting, since there is delay, and it can't do multiple things at once, so it turns all LED's on? I'm not sure if that's accurate, though.

The point is to find out whether your pot is wired up correctly. You might want to comment the LED code out while you establish what the analogRead is getting you.

While you're at it, increase the serial speed a bit lot. It's not the 1980s :slight_smile:

If you want the pot to be more responsive you can read it more often. The way your sketch is written it will take about 16 seconds before looking at the pot again if the pot is set to a high value.

const byte LEDCount = 9;
const byte LEDPins[LEDCount] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

const byte PotPin = A0;

void setup()
{
  //Serial.begin(9600);
  for (int i = 0; i < LEDCount; i++)
  {
    pinMode(LEDPins[i], OUTPUT);
  }
  // pinMode(potPin, INPUT);  // Not needed for analogRead()
}

void loop()
{
  for (int i = 0; i < LEDCount; i++)
  {
    digitalWrite(LEDPins[i], HIGH);
    delay(analogRead(PotPin));
    digitalWrite(LEDPins[i], LOW);
  }

  for (int i = LEDCount-1; i >= 0; i--)
  {
    digitalWrite(LEDPins[i], HIGH);
    delay(analogRead(PotPin));
    digitalWrite(LEDPins[i], LOW);
  }
}
1 Like

wildbill:
The point is to find out whether your pot is wired up correctly. You might want to comment the LED code out while you establish what the analogRead is getting you.

While you're at it, increase the serial speed a bit lot. It's not the 1980s :slight_smile:

It is wired okay, if I un-comment the debug section, it reads from 0 - 1023.

How do I allow higher serial speed rate? Anything other than 9200 doesn't seem to work with IDE, and gives me errors.

johnwasser:
If you want the pot to be more responsive you can read it more often. The way your sketch is written it will take about 16 seconds before looking at the pot again if the pot is set to a high value.

const byte LEDCount = 9;

const byte LEDPins[LEDCount] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

const byte PotPin = A0;

void setup()
{
  //Serial.begin(9600);
  for (int i = 0; i < LEDCount; i++)
  {
    pinMode(LEDPins[i], OUTPUT);
  }
  // pinMode(potPin, INPUT);  // Not needed for analogRead()
}

void loop()
{
  for (int i = 0; i < LEDCount; i++)
  {
    digitalWrite(LEDPins[i], HIGH);
    delay(analogRead(PotPin));
    digitalWrite(LEDPins[i], LOW);
  }

for (int i = LEDCount-1; i >= 0; i--)
  {
    digitalWrite(LEDPins[i], HIGH);
    delay(analogRead(PotPin));
    digitalWrite(LEDPins[i], LOW);
  }
}

Thank you very much, kind sir.

   int i = 0;
  while (i<8)
  {
    {
      digitalWrite(ledPins[i],HIGH);
      delay(potSpeed);
      digitalWrite(ledPins[i],LOW);
      i++;
    }
  }

Turn on the LED
Wait a bit
Turn off the LED then immediately go back to turn it on again without any wait

Don't forget that if you increase the serial speed in the sketch, you have to change it to match in the serial monitor of the IDE too.

evanmars:

   int i = 0;

while (i<8)
 {
   {
     digitalWrite(ledPins[i],HIGH);
     delay(potSpeed);
     digitalWrite(ledPins[i],LOW);
     i++;
   }
 }




Turn on the LED
Wait a bit
Turn off the LED then immediately go back to turn it on again without any wait

You missed the "i++".

Turn on the LED
Wait a bit
Turn off the LED then immediately go back to TURN ON THE NEXT LED without any wait

johnwasser:
You missed the "i++".

That line must've gotten stuck in my bifocals.. :-[