Go Down

Topic: help with RGB LED fading (noob) (Read 2331 times) previous topic - next topic

chris1982

Mar 03, 2012, 09:29 pm Last Edit: Mar 03, 2012, 09:42 pm by chris1982 Reason: 1
I am trying to program an arduino to control a RGB LED to cycle through the various colours based on the readings from 3 potentiometers. Eventually i aim to get it responding to music but this is the first step. I have very little programming experience in general or with the arduino in particular.

I have put together some code based on other peoples examples designed to fade up and down through each of the 3 colours based on reading from 3 potentiometers. The problem i have is that the colours fade in as they should but then cut to zero and fade in again, they do not fade in and then fade out. There is a section of code designed to fade the brightness back down from maximum but it doesn't appear to be working properly - any advice would be greatly appreciated, thanks.

Code: [Select]
int sensorValueRed = 0; //store the value from the potentiometer
int brightnessRed = 0;    // how bright the LED is
int fadeAmountRed = 5;    // how many points to fade the LED by
int ledPinRed = 3;      //pin LED is attached to
int sensorPinRed = 0;   //pin for the potentiometer

int sensorValueGreen = 0; //store the value from the potentiometer
int brightnessGreen = 0;    // how bright the LED is
int fadeAmountGreen = 5;    // how many points to fade the LED by
int ledPinGreen = 5;      //pin LED is attached to
int sensorPinGreen = 1;   //pin for the potentiometer

int sensorValueBlue = 0; //store the value from the potentiometer
int brightnessBlue = 0;    // how bright the LED is
int fadeAmountBlue = 6;    // how many points to fade the LED by
int ledPinBlue = 6;      //pin LED is attached to
int sensorPinBlue = 2;   //pin for the potentiometer

void setup()  {
 // declare pin 9 to be an output:
 pinMode(ledPinRed, OUTPUT);
 pinMode(ledPinGreen, OUTPUT);
 pinMode(ledPinBlue, OUTPUT);
}

void loop()  {
 sensorValueRed = analogRead(sensorPinRed); //read the value from the pot as sensorValueRed
  analogWrite(ledPinRed, brightnessRed);    
 fadeAmountRed = sensorValueRed/300;

 // change the brightness for next time through the loop:
 brightnessRed = brightnessRed + fadeAmountRed;

 // reverse the direction of the fading at the ends of the fade this section of code doesn't appear to work and the LED looks the same when i comment it out?
 if (brightnessRed == 0 || brightnessRed == 255) {
   fadeAmountRed = -fadeAmountRed ;
 }    
          delay(1);  
/code continues in the same way for Green and Blue which have the same issue.


I have tried my best to include all the relevant info but haven't used this forum much.

madworm

First you should use the CODE button, not QUOTE.

You only check for "==0" and "==255". Are you 100% certain that your code ever reaches these values? You should print the values 'brightnessRed' etc. to the serial port for debugging.
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

chris1982

#2
Mar 03, 2012, 09:43 pm Last Edit: Mar 03, 2012, 09:47 pm by chris1982 Reason: 1
Thanks for your suggestions - sorted the first, looking into the second...

I am definitely not sure my code reached those values. Now i just need to work out how to print the values 'brightnessRed' etc. to the serial port for debugging.

Edit - just changed the value check down to 5 from 255 - the LED brightness cycles in exactly the same way.

AWOL

Quote
/code continues in the same way for Green and Blue which have the same issue.

So, scrap all the excess colours, concentrate on one colour, use some arrays and write some functions.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

#4
Mar 03, 2012, 09:51 pm Last Edit: Mar 03, 2012, 10:11 pm by chris1982 Reason: 1
That's exactly what i have done - the other 2 colours are commented out and i didn't bother copying and pasting them. Red cycles from minimum to maximum and then cuts out and repeats.

Quote
void loop()  {
  sensorValueRed = analogRead(sensorPinRed); //read the value from the sensor as sensorValue
  // set the brightness of pin 9:
  analogWrite(ledPinRed, brightnessRed);   
  fadeAmountRed = sensorValueRed/300;

  // change the brightness for next time through the loop:
  brightnessRed = brightnessRed + fadeAmountRed;

  // reverse the direction of the fading at the ends of the fade:
// if (brightnessRed == 0 || brightnessRed == 255) {
  //  fadeAmountRed = -fadeAmountRed ;
// }   
  // wait for 30 milliseconds to see the dimming effect   
          delay(1); 


When i comment out the bottom section of the code the LED behaves in the same way but;

Quote
void loop()  {
  sensorValueRed = analogRead(sensorPinRed); //read the value from the sensor as sensorValue
  // set the brightness of pin 9:
  analogWrite(ledPinRed, brightnessRed);   
  fadeAmountRed = sensorValueRed;

  // change the brightness for next time through the loop:
  brightnessRed = brightnessRed + fadeAmountRed;

  // reverse the direction of the fading at the ends of the fade:
if (brightnessRed == 0 || brightnessRed == 255) {
  fadeAmountRed = -fadeAmountRed ;
}   
  // wait for 30 milliseconds to see the dimming effect   
          delay(1); 


If i leave the code in that is supposed to reverse the direction of fade when it reaches maximum brightness but also remove the /300 from sensorvalueRed the LEDs behaviour is reverse - it starts as a maximum and fades to minimum before snapping back to maximum brightness. I'm a bit confused.






AWOL

What if brightnessRed goes from, say, 254 to 256 without ever equalling 255?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

Can i replace equal to with greater than to solve that problem?

AWOL

I think that would be a good start.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

Changing ;

Code: [Select]
if (brightnessRed == 0 || brightnessRed == 255)

to

Code: [Select]
if (brightnessRed == 0 || brightnessRed > 255)

Made no difference. Am i on the right path with this?

AWOL

#9
Mar 03, 2012, 10:39 pm Last Edit: Mar 03, 2012, 10:42 pm by AWOL Reason: 1
If it can go greater than 255, it can also, presumably, go less than 0.
Have you catered for that?

Quote
the other 2 colours are commented out

Code: [Select]
int sensorValueGreen = 0; //store the value from the potentiometer
int brightnessGreen = 0;    // how bright the LED is
int fadeAmountGreen = 5;    // how many points to fade the LED by
int ledPinGreen = 5;      //pin LED is attached to
int sensorPinGreen = 1;   //pin for the potentiometer
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

Code: [Select]
if (brightnessRed < 0 || brightnessRed > 255) {
    fadeAmountRed = -fadeAmountRed ;
  }   


Doesn't seem to help at all.

chris1982

#11
Mar 03, 2012, 10:46 pm Last Edit: Mar 03, 2012, 10:48 pm by chris1982 Reason: 1
My code in full;

Code: [Select]
int sensorValueRed = 0; //store the value from the potentiometer
int brightnessRed = 0;    // how bright the LED is
int fadeAmountRed = 5;    // how many points to fade the LED by
int ledPinRed = 3;      //pin LED is attached to
int sensorPinRed = 0;   //pin for the potentiometer

int sensorValueGreen = 0; //store the value from the potentiometer
int brightnessGreen = 0;    // how bright the LED is
int fadeAmountGreen = 5;    // how many points to fade the LED by
int ledPinGreen = 5;      //pin LED is attached to
int sensorPinGreen = 1;   //pin for the potentiometer

int sensorValueBlue = 0; //store the value from the potentiometer
int brightnessBlue = 0;    // how bright the LED is
int fadeAmountBlue = 6;    // how many points to fade the LED by
int ledPinBlue = 6;      //pin LED is attached to
int sensorPinBlue = 2;   //pin for the potentiometer

void setup()  {
 // declare pin 9 to be an output:
 pinMode(ledPinRed, OUTPUT);
 pinMode(ledPinGreen, OUTPUT);
 pinMode(ledPinBlue, OUTPUT);
}

void loop()  {
 sensorValueRed = analogRead(sensorPinRed); //read the value from the sensor as sensorValue
 // set the brightness of pin 9:
 analogWrite(ledPinRed, brightnessRed);    
 fadeAmountRed = sensorValueRed/300;

 // change the brightness for next time through the loop:
 brightnessRed = brightnessRed + fadeAmountRed;

 // reverse the direction of the fading at the ends of the fade:
 if (brightnessRed < 0 || brightnessRed > 255) {
   fadeAmountRed = -fadeAmountRed ;
 }    
 // wait for 30 milliseconds to see the dimming effect    
    delay(1);  
}


I commented out the code in the loop for the other 2 colours but did not bother commenting out the variables as i didn't think they would have an effect as they are not referenced anywhere futher down.

AWOL

Code: [Select]
// wait for 30 milliseconds to see the dimming effect   
     delay(1);   

When code is commented like this, I stop reading.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

#13
Mar 03, 2012, 10:49 pm Last Edit: Mar 03, 2012, 10:54 pm by chris1982 Reason: 1
Well you are obviously much more experienced and knowledgable when it comes to programming and the arduino than myself. If this forum isn't the place for total newbies to come looking for advice then can you recomend somewhere better?

I did try altering the delay to 30 and it made no difference. I should probably have edited the comments. I am not sure what you are implying with your previous comment though - it would probably be obvious to me if i knew more about programming and stuff.

madworm

You should really use the serial port to print your variables. The example code in the IDE is easy to understand!

If one of your brightness values is negative you invert the counting direction. If for some reason one counting step is not enough to get into the positive region you invert direction again and count back and forth, back and forth...

If you used the serial port, you would see something like this:

-10
-7
-10
-7
-10
-7
...

And you would probably understand what your code is really doing.
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Go Up