Pages: [1]   Go Down
Author Topic: [Solved] Snarly errors while fading an RGB-LED  (Read 1087 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino's rocking Ubuntu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello and greetings from Germany!
Me and my dad just bought an Arduino Duemilanove and now we are experimenting. Yesterday I took a RGB-LED and tried to fade its colours. First, I faded red in and out, afterwards green and then blue. No big deal indeed. But then I got carefree and faded green in and red out and so on - simultaneously. Here's my code - and yes, I copied some passages from the fading example code and I know, some passages are kinda messy:

Code:
int ledPinR =  9;     //   red LED connected to digital pin  9
int ledPinG =  10;    // green LED connected to digital pin 10
int ledPinB =  11;    //  blue LED connected to digital pin 11
int fadeValueR =  0;  // start-value for the   red LED
int fadeValueG =  0;  // start-value for the green LED
int fadeValueB =  0;  // start-value for the  blue LED

void setup()   {                
  // start with a fast fade in of the red LED
  for(int fadeValueR = 0 ; fadeValueR <= 255; fadeValueR +=1) {
    // set the red value:
    analogWrite(ledPinR, fadeValueR);        
    // wait for only 6 milliseconds to see the faster dimming effect    
    delay(6);                            
  }
}

void loop()                    
{
  // fade red out and green in:
  for(int fadeValueR = 255 ; fadeValueR >= 0; fadeValueR -=1) {
    // wait for 17 milliseconds to see the dimming effect    
    delay(17);
    // set the red value:
    analogWrite(ledPinR, fadeValueR);        
    // set the green value:
    if(fadeValueG < 255){
      fadeValueG = fadeValueG + 1;
      analogWrite(ledPinG, fadeValueG);
    }
  }

  // turn the red LED copletely off and the green on to prevent it from blinking:
  fadeValueR = 0;
  analogWrite(ledPinR, fadeValueR);
  fadeValueG = 255;
  analogWrite(ledPinG, fadeValueG);        

  // fade green out and blue in:
  for(int fadeValueG = 255 ; fadeValueG >= 0; fadeValueG -=1) {
    // wait for 17 milliseconds to see the dimming effect    
    delay(17);
    // set the green value:
    analogWrite(ledPinG, fadeValueG);        
    // set the blue value:
    if(fadeValueB < 255){
      fadeValueB = fadeValueB + 1;
      analogWrite(ledPinB, fadeValueB);
    }
  }

  // turn the green LED copletely off and the blue on to prevent it from blinking:
  fadeValueG = 0;
  analogWrite(ledPinG, fadeValueR);
  fadeValueB = 255;
  analogWrite(ledPinB, fadeValueG);        

  // fade blue out and red in:
  for(int fadeValueB = 255 ; fadeValueB >= 0; fadeValueB -=1) {
    // wait for 17 milliseconds to see the dimming effect    
    delay(17);
    // set the blue value:
    analogWrite(ledPinB, fadeValueB);        
    // set the red value:
    if(fadeValueR < 255){
      fadeValueR = fadeValueR + 1;
      analogWrite(ledPinR, fadeValueR);
    }
  }

  // turn the blue LED copletely off and the red on to prevent it from blinking:
  fadeValueB = 0;
  analogWrite(ledPinB, fadeValueR);
  fadeValueR = 255;
  analogWrite(ledPinR, fadeValueG);        
}
The first run was somehow a feast for the eyes but the second run of the loop function shocked me:
After fading in the green LED the second time, the RGB-LED blinked once and then after every color-change. While developing the code, this problem occured also in the first run, so I inserted these "completely on and off"-blocks. It helped, but as said only for the first run of the loop-function.

Since this is more and more confusing me, I ask you, dear community: Any suggestions for solving the problem?
If needed, I'll clean up the code, so that it's comprehensible for everyone. I can also film the Arduino and the error if this helps understanding the problem.
« Last Edit: December 31, 2010, 11:25:27 am by agent94 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 50243
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 // turn the green LED copletely off and the blue on to prevent it from blinking:
  fadeValue[glow]G[/glow] = 0;
  analogWrite(ledPin[glow]G[/glow], fadeValue[glow]R[/glow]);
  fadeValue[glow]B[/glow] = 255;
  analogWrite(ledPin[glow]B[/glow], fadeValue[glow]G[/glow]);        


  // turn the blue LED copletely off and the red on to prevent it from blinking:
  fadeValue[glow]B[/glow] = 0;
  analogWrite(ledPin[glow]B[/glow], fadeValue[glow]R[/glow]);
  fadeValue[glow]R[/glow] = 255;
  analogWrite(ledPin[glow]R[/glow], fadeValue[glow]G[/glow]);        
Probably ought to all match.
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino's rocking Ubuntu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Embarrassing, embarrassing...  :-[
That it was, thank you! Maybe I should write more code than I copy.. :-/

So long! Chrisch




@the moderators: please close this topic
« Last Edit: December 31, 2010, 05:11:47 am by agent94 » Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
@the moderators: please close this topic

On the world's lightest moderated board ?  Unlikely I feel.....  smiley
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13851
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@agent94

You can change the title ? ==> add a [closed] tag
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino's rocking Ubuntu!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good idea, I'll do that.

And by the way: Happy new year! smiley-wink So long
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@robtillaart:
Quote
You can change the title ? ==> add a [closed] tag
In the interests of full disclosure and truth in advertising, I would probably change it to "[Solved]" or, maybe "[Resolved]" or some such happy thing.  (And maybe even put a happy-face emoticon on it.)  I mean, it isn't really closed is it?  (As this post proves.)


Regards,

Dave
« Last Edit: December 31, 2010, 11:25:14 am by davekw7x » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it isn't really closed is it?  (As this post proves.)

No it is not. The problem and solution will live on the Internet for an eternity. The OP's great great great grandchildren will be able to read it and chuckle and laugh about the old man long after he passes.  smiley-wink

Maybe I should be more careful about what I post, seeing how it will last an eternity. Kind of like saving ones grade school report cards for the world to see, forever.  ;D

Lefty
Logged

Pages: [1]   Go Up
Jump to: