Go Down

Topic: [Solved] Snarly errors while fading an RGB-LED (Read 1 time) previous topic - next topic

agent94

Dec 30, 2010, 10:22 pm Last Edit: Dec 31, 2010, 05:25 pm by agent94 Reason: 1
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: [Select]
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.

PaulS

Code: [Select]
 // 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.

agent94

#2
Dec 31, 2010, 10:52 am Last Edit: Dec 31, 2010, 11:11 am by agent94 Reason: 1
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

pluggy

Quote
@the moderators: please close this topic


On the world's lightest moderated board ?  Unlikely I feel.....  :)
http://pluggy.is-a-geek.com/index.html

robtillaart

@agent94

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

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

agent94

Good idea, I'll do that.

And by the way: Happy new year! ;) So long

davekw7x

#6
Dec 31, 2010, 05:23 pm Last Edit: Dec 31, 2010, 05:25 pm by davekw7x Reason: 1
@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

retrolefty

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.  ;)

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

Go Up