Question on controling LED brightness through PWM with pot

Okay, first of all I want to say hello since this is my first post here. So… uh, hello. :slight_smile:

I just got my arduino a couple weeks ago and am finally starting to play around with it. I’ve never done any kind of programming so I’m just sort of learning as I go.

Today I made a simple 5 LED chaser thingy that utilises a potentiometer as an analog input to control the speed of the LEDs. Now the next thing I want to do is to use a second potentiometer to control the brightness of the LEDs via PWM but I just can’t seem to figure out how to accomplish this.

The code I used was just a barely modified (just changed the # of LEDs) version of Earthshine’s “Interactive LED chaser”.

byte ledPin[] = {4, 5, 6, 7, 8,}; 
int ledDelay; // delay between changes 
int direction = 1; 
int currentLED = 0; 
unsigned long changeTime; 
int potPin = 2; 
// select the input pin for the potentiometer 

void setup() { 
// set all pins to output 
for (int x=0; x<5; x++) { 
pinMode(ledPin[x], OUTPUT); } 
changeTime = millis(); 
} 

void loop() {
// read the value from the pot
ledDelay = analogRead(potPin);
// if it has been ledDelay ms since last change
if ((millis() - changeTime) > ledDelay) {
changeLED();
changeTime = millis();
}
}

void changeLED() {
// turn off all LED's
for (int x=0; x<5; x++) {
digitalWrite(ledPin[x], LOW);
}
// turn on the current LED
digitalWrite(ledPin[currentLED], HIGH);
// increment by the direction value
currentLED += direction;
// change direction if we reach the end
if (currentLED == 4) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}

I figured I could add the second pot to A1 but beyond that, I’m lost. Any help that can be offered would be greatly appreciated.

~Rev

P.S. The original circuit called for a resistor between each LED and it’s corresponding pin, but instead I just ran a single resistor between the shared LED ground and the ground on the board. Is there any reason I shouldn’t do this? It just seemed simpler and less cluttered.

First, you need to make sure that the LEDs are connected to PWM pins; some of the ones you specified are not (unless you're using a Mega). Have a look at the documentation for analogWrite.

AWOL: First, you need to make sure that the LEDs are connected to PWM pins; some of the ones you specified are not (unless you're using a Mega). Have a look at the documentation for analogWrite.

Yeah, I figured on using 5,6,9,10,11 for the LEDs (I'm using an Uno). I'll do some reading on analogWrite. Big thanks for the tip. :D

~Rev

P.S. The original circuit called for a resistor between each LED and it's corresponding pin, but instead I just ran a single resistor between the shared LED ground and the ground on the board. Is there any reason I shouldn't do this? It just seemed simpler and less cluttered.

That's ok for your current application but if you wanted to turn on more than one LED at a time you would need to use separate resistors.

Success!

/*5 LED sequential flash w/adjustable speed and brightness. */

byte ledPin[] = {5, 6, 9, 10, 11}; //Define output pins.
int ledDelay; //Delay value.
int val = 0; //Brightness value.
int direction = 1; 
int currentLED = 0; 
unsigned long changeTime; 
int potPin1 = 1; //Brightness pot.
int potPin2 = 2; //Delay pot.



void setup() 
{ 
  for (int x=0; x<5; x++) 
  { 
  pinMode(ledPin[x], OUTPUT); //Set ledPins to output.
  } 
  changeTime = millis(); 
} 

void loop() 
{
  val = analogRead(potPin1); //Read birghtness value.
  ledDelay = analogRead(potPin2); //Read delay value.
  if ((millis() - changeTime) > ledDelay) 
  {
  changeLED();
  changeTime = millis();
  }
}

void changeLED() 
{
  for (int x=0; x<5; x++) 
  {
  analogWrite(ledPin[x], 0); //Turn LED off.
  }
  analogWrite(ledPin[currentLED], val / 4); //Turn LED on @ potPin1 val.
  currentLED += direction;
  if (currentLED == 4) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
}

I totally didn’t think I would be able to figure it out.

Thanks again for pointing me in the right direction AWOL. And @ mem; that’s what I thought, thanks for the confirmation.

~Rev

I totally didn't think I would be able to figure it out.

Feels good, doesn't it? Spread it around!

Congratulations.