Pages: [1] 2 3 ... 5   Go Down
Author Topic: help with RGB LED fading (noob)  (Read 1596 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: March 03, 2012, 03:42:06 pm by chris1982 » Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

• 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!

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 03, 2012, 03:47:02 pm by chris1982 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.





« Last Edit: March 03, 2012, 04:11:09 pm by chris1982 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What if brightnessRed goes from, say, 254 to 256 without ever equalling 255?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think that would be a good start.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Changing ;

Code:
if (brightnessRed == 0 || brightnessRed == 255)

to

Code:
if (brightnessRed == 0 || brightnessRed > 255)

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: March 03, 2012, 04:42:35 pm by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Doesn't seem to help at all.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My code in full;

Code:
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.
« Last Edit: March 03, 2012, 04:48:44 pm by chris1982 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24317
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// wait for 30 milliseconds to see the dimming effect   
     delay(1);   
When code is commented like this, I stop reading.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 03, 2012, 04:54:16 pm by chris1982 » Logged

"The old Europe"
Offline Offline
Edison Member
*
Karma: 1
Posts: 2005
Bootloaders suck!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

• 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!

Pages: [1] 2 3 ... 5   Go Up
Jump to: