Go Down

Topic: LED Bar using PWM outputs for fading each LED (Read 522 times) previous topic - next topic

compubob

Dec 20, 2010, 06:08 am Last Edit: Dec 20, 2010, 06:55 am by compubob Reason: 1
Hi everyone.. I am a new arduino user.. I have a Mega board and I've been messing with it for a few weeks.
I have 10 LED's setup and functioning using the PWM outputs. With the push of a button I want to start off with pin1 and fade it in over 200ms and keep it on then move to pin2 and repeat, so over the course of 2 seconds I illuminate the strip.

I have this operating fine when just turning on the LED's using     digitalWrite(pin, HIGH);  but when it comes to using a pwm output with 255 steps it wants to cycle through only one step at a time before moving to the second led.

I have tried everything I can think of. I could see this working if you could somehow use multiple loops that exit and go to the next when the 255 value is achieved, but I'm sure that's impossible. and not to mention that the led would probably turn off when you exit that loop.

here is the last code I was working on and I keep changing it constantly so this is probably not what everyone expects to see (I removed the button part for testing so ignore that)
with this code it's not even fading, just turning on the LED's it was fading at one point :(

Code: [Select]
const int buttonPin = 12;  
int value = 0;                    
int ledPin1 =  2;
int ledPin2 =  3;
int ledPin3 =  4;
int ledPin4 =  5;
int ledPin5 =  6;
int ledPin6 =  7;
int ledPin7 =  8;
int ledPin8 =  9;
int ledPin9 =  10;
int ledPin10 =  11;
int buttonState = 0;

void setup()   {                
 // initialize the digital pin as an output:
 pinMode(buttonPin, INPUT);  
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(ledPin4, OUTPUT);
 pinMode(ledPin5, OUTPUT);
 pinMode(ledPin6, OUTPUT);
 pinMode(ledPin7, OUTPUT);
 pinMode(ledPin8, OUTPUT);
 pinMode(ledPin9, OUTPUT);
 pinMode(ledPin10, OUTPUT);  
}

// Program

void loop()                    
{
 buttonState = digitalRead(buttonPin);
 // LED STRIP ON
 {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin1, value);  
  delay(100);    
}
 {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin2, value);    
  delay(100);                    
}  
 {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin3, value);      
  delay(100);              
}  
 {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin4, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin5, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin6, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin7, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin8, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin9, value);    
  delay(100);                      
 }
   {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin10, value);    
  delay(100);                      
 }
}

PaulS

Quote
I want to start off with pin1 and fade it in over 200ms and keep it on then move to pin2 and repeat, so over the course of 2 seconds I illuminate the strip.


Quote
but when it comes to using a pwm output with 255 steps it wants to cycle through only one step at a time before moving to the second led.

I fail to see how these statements do not indicate the same goal.

If the loop fades one LED all the way on, before moving to the next LED, that appears to be in keeping with your original requirement.

So, what is the problem?

Code: [Select]
  {
for(value = 0 ; value <= 255; value+=5)
  analogWrite(ledPin1, value);  
  delay(100);    
}

The body of the for loop is not inside curly braces. So, the only thing that is executed in the loop is the analogWrite statement. The time it takes to execute the whole for loop is a few hundred nanoseconds. Then, the delay occurs.

This probably not what you want. Put the curly braces in the right place, and see that happens.
Code: [Select]
for(value = 0 ; value <= 255; value+=5)
{
  analogWrite(ledPin1, value);  
  delay(100);    
}


compubob

well I can't believe it was something that simple.. ok I'm getting somewhere now.
thank you!

Go Up