Pages: [1]   Go Down
Author Topic: diming LEDs  (Read 528 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, thanks for any help anyone can give in advance, it's much apprciated.

I'm trying to have some LEDs on different PWM pins cycle such that they blend. I've managed to work out the "for" command for dimming the LED, but the problem is that only one pin can be dimmed at a time. Idealy I'd like to have as one pin goes up the other to go down. Anyone any ideas?

Also this is my first foray in to programming of any kind, so take it easy on me  :-/
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Arduino can do hardware PWM on 6 output pins.  I'm not clear what you mean by only one can be dimmed at a time?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At the moment I have say LED1 coming on, then LED2 coming on, then LED1 dimming off, then LED3 coming on, then LED2 dimming off etc...

so there is a smooth transition between the colours.

However, I would like to get it a bit smoother, so that at the same time as one LED is coming on another is dimming off.

I'm just not sure how to do this with the 'for' command that i'm using at the moment...

thanks
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 854
Arduino rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
However, I would like to get it a bit smoother, so that at the same time as one LED is coming on another is dimming off.

I'm just not sure how to do this with the 'for' command that i'm using at the moment...

What does your for loop look like?  Can you post the code here?  It should be simple enough to have one fade on as another fades off.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah sure,
here is my code of diming the LEDs

Code:
 for(value = 0 ; value <= 255; value+=5)          // fade in (from min to max) led1
  {
    analogWrite(ledpin, value);                    // sets the value (range from 0 to 255)
    delay(30);                                     // waits for 30 milli seconds to see the dimming effect
  }
  
   for(value3 = 255; value3 >=0; value3-=5)        // fade out (from max to min) led3
  {
    analogWrite(ledpin3, value3);
    delay(30);
  }
  
  for(value2 = 0 ; value2 <= 255; value2+=5)       // fade in (from min to max) led2
  {
    analogWrite(ledpin2, value2);                  // sets the value (range from 0 to 255)
    delay(30);                                     // waits for 30 milli seconds to see the dimming effect
  }
  
  for(value = 255; value >=0; value-=5)            // fade out (from max to min) led1
  {
    analogWrite(ledpin, value);
    delay(30);
  }
  
  for(value3 = 0 ; value3 <= 255; value3+=5)       // fade in (from min to max) led3
  {
    analogWrite(ledpin3, value3);                  // sets the value (range from 0 to 255)
    delay(30);                                     // waits for 30 milli seconds to see the dimming effect
  }
 
  
  for(value2 = 255; value2 >=0; value2-=5)         // fade out (from max to min) led2
  {
    analogWrite(ledpin2, value2);
    delay(30);
  }
Logged

Atlanta, GA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 51
Arduino boulders
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your for-loops are all in series, that is one following another, therefore your approach is unlikely to allow control of multiple pins concurrently.

A different way to look at this problem is to phrase it as: What value does each pin have at timestep T? Thinking about the answer leads to something like the following p-code:

Code:
while looping:
  val = ValueForPin1()
  if (val != -1) SetPin1(val)
  val = ValueForPin2()
  if (val != -1) SetPin1(val)
  ...
  val = ValueForPinN()
  if (val != -1) SetPinN(val)

  # other code here
  
  delay(30)


the ValueForPinX() functions can be a single parameterised function, which would be easier to manage. Also, you can build a for loop over all pins, setting each one's value accordingly inside the loop. The important thing is to decouple the determination of a pin's value from where that value is used.


Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah ok i think I see, have a single value which ticks over in the loop and tie that value to the different pin values at different points?
thanks for the help I will give it a go
Logged

Grenoble/Lyon - France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 363
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem with your code is that you used FOR loops. You program doesn't goes further that the loop until it have finished doing it.
Another way to do it could be something like.

Code:
led1val = 0;                  // Led 1 starts off
led2val = 255;              // Led 2 starts fully ON
led1inc = 1;                 // Led 1 increments 1
led2inc = -1;                 // Led 2 increments -1 = decrements 1

void setup() {
   analogWrite(led1,led1val);
   analogWrite(led2,led2val);
}

void loop() {
   led1val += led1inc;
   led2val += led2inc;
   analogWrite(led1,led1val);
   analogWrite(led2,led2val);

   switch (led1val) {
      case 0:
         led1inc = 1;
         break;
      case 255:
         led1inc = -1;
         break;
   }
  
   switch (led2val) {
      case 0:
         led2inc = 1;
         break;
      case 255:
         led2inc = -1;
         break;
   }

   delay(100);
}

NB : this is "pseudocode", don't take it for granted, got to test it smiley-wink
Logged


Pages: [1]   Go Up
Jump to: