Pages: 1 2 3 [4] 5   Go Down
Author Topic: help with RGB LED fading (noob)  (Read 1942 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That code just causes the LED to start at maximum brightness and reduce down at a rate set by the potentiometer, then it starts over (i presume i can add the other part of the code in easily enough to make it ramp back up again).
Yes, you can.

Quote
However, if i reduce the potentiometer down to minimum and then up again the LED halts it's cycle and does not restart. Exactly the same problem as with my code.
Aha, now we get to the real problem!

Look at what the fade amount is when you get the potentiometer to the lowest value.
Code:
   int sensorValueRed = analogRead(sensorPinRed);
   int fadeAmount = sensorValueRed/300;
When sensorValueRed is 0, fadeAmount is also 0.

If you start with b=255, and sutractt 0 on each pass, how many executions of the loop will be required to get b down to 0?

Clearly, you need to constrain fadeAmount to not less than 1.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1328
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When sensorValueRed is 0, fadeAmount is also 0.

If you start with b=255, and sutractt 0 on each pass, how many executions of the loop will be required to get b down to 0?

Clearly, you need to constrain fadeAmount to not less than 1.

 One other option would be to not enter the "fade  for loop" unless fadeAmount is >=1. In case you might want to have a steady state LED.
Logged


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

I just tried altering;

Code:
fadeAmountRed =sensorValueRed/100

to

Code:
fadeAmountRed =sensorValueRed/100+1

I'm not sure if the syntax is right as i didn't look it up first but it seems to have fixed the problem, thanks.
Logged

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

Quote
I'm not sure if the syntax is right as i didn't look it up first but it seems to have fixed the problem, thanks.
If it fixed the problem, that means that you were able to compile and link and upload the code, to the syntax at least was correct.

You are, though, relying on the operations being performed in a particular order. You can, and usually, they will be executed in the correct order. I prefer, though, to use parentheses and white space.
Code:
fadeAmountRed = (sensorValueRed/100) + 1;
Logged

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

I've found a slightly better method of achieving the same result. int sensorValueRed = analogRead(sensorPinRed)+100; (so that when /100 latter it's always at least 1). I'm thinking that by adding the +1 in right at the start i can then carry on using the variable later on without having to worry each time;

Code:
int sensorValueRed;   //reading from the red pot
int brightnessRed;    // how bright the  red LED is
int fadeAmountRed;    // how many points to fade the red LED by
const int ledPinRed = 3;      //pin red LED is attached to
const int sensorPinRed = 0;   //pin for the red potentiometer

void setup()  
{
Serial.begin(57600);  //begin serial communication
}

void loop()  

{
  int sensorValueRed = analogRead(sensorPinRed)+100;   //read from the pin the red pot is plugged in to and store as sensorValueRed +100 so that when /100 = at last 1

  // fade in from min to max in increments of sensorValueRed/300 points:
  for(int brightnessRed = 10 ; brightnessRed <= 240; fadeAmountRed =sensorValueRed/100) {  
    // sets the value (range from 10 to 240):
 brightnessRed = brightnessRed + fadeAmountRed;   //adds the fadeAmountRed value to the brightnessRed value to make a new brighter brightnessRed
  analogWrite(ledPinRed, brightnessRed);           //writes the new value of brightnessRed
   Serial.print ("SensorValueRed ");                        //debugging code
   Serial.println(sensorValueRed);                          //debugging code
   Serial.print ("FadeAmountRed ");                           //debugging code
   Serial.println(fadeAmountRed);                            //debugging code
   Serial.print ("BrightnessRed ");                          //debugging code
   Serial.println(brightnessRed);                              //debugging code
   delay(3);                
  }

  // fade out from max to min in increments of sensorValueRed/300 points:
  for(int brightnessRed = 240 ; brightnessRed >= 10; fadeAmountRed =-sensorValueRed/100) {
    // sets the value (range from 10 to 240):
  brightnessRed = brightnessRed + fadeAmountRed;   //subtracts the fadeAmountRed value from the brightnessRed value to make a new dimmer brightnessRed
  analogWrite(ledPinRed, brightnessRed);           //writes the new value of brightnessRed
   Serial.print ("SensorValueRed ");                   //debugging code
   Serial.println(sensorValueRed);                   //debugging code
   Serial.print ("FadeAmountRed ");                  //debugging code
   Serial.println(fadeAmountRed);                            //debugging code
   Serial.print ("BrightnessRed ");                          //debugging code
   Serial.println(brightnessRed);                            //debugging code
  delay(3);        
  
  }



  
}

There's probably a whole bunch of stuff i can improve and i still need to add the other 2 colours back in but i should be able to manage. Thanks very much for your help.
« Last Edit: March 04, 2012, 11:42:14 am by chris1982 » Logged

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

Hm. Well i added the other 2 colours back in but now i have an entirely new problem. With my original code;

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

sensorValueGreen = analogRead(sensorPinGreen); //read the value from the sensor as sensorValue
  // set the brightness of pin 9:
  analogWrite(ledPinGreen, brightnessGreen);    
  fadeAmountGreen = sensorValueGreen/300;

  // change the brightness for next time through the loop:
  brightnessGreen = brightnessGreen + fadeAmountGreen;

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

sensorValueBlue = analogRead(sensorPinBlue); //read the value from the sensor as sensorValue
  // set the brightness of pin 9:
  analogWrite(ledPinBlue, brightnessBlue);    
  fadeAmountBlue = sensorValueBlue/300;

  // change the brightness for next time through the loop:
  brightnessBlue = brightnessBlue + fadeAmountBlue;

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

Everything worked the way i wanted except that the LEDs were ramping up in brightness and then cuttign back to minimum.

The new code solves that problem but now each of the 3 colours ramps up and down in brightness in sequence rather than simultaneously so that i get nice colour mixing effects. I'm not sure why this is.
Logged

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

Quote
There's probably a whole bunch of stuff i can improve and i still need to add the other 2 colours back in but i should be able to manage.
Before you go too much further, might I suggest that you print the code out, and get a highlighter.

Highlight all the parts that are going to need to change for another color. Decide whether that really needs to change, or not.

For instance, this statement might get highlighted in several places:
  for(int brightnessRed = 10 ; brightnessRed <= 240; fadeAmountRed =sensorValueRed/100) { 

Now, think about whether the loop index really should depend on the color. Could the index be just a generic name like i? The answer is that of course it could. Could the fade amount be a generic value that was assigned a specific value, as I did in my example.

When you've revised the code to remove as much color uniqueness as possible, create a function that contains the code, and pass it parameters for the unique stuff.

Code:
void fade(int ledPin, int fadeAmount, unsigned long delayTime)
{
   for(int i=0; i<=255; i+=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
   for(int i=255; i>=0; i-=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
}

Then, you can call the function:
Code:
fade(ledRedPin, fadeAmountRed, 30);
fade(ledGrnPin, fadeAmountGrn, 30);
fade(ledBluPin, fadeAmountBlu, 30);

(As you can see, I like patterns to names, including making them all the same length. Sometimes this requires a little mis-spelling, but I don't mind intentional spelling errors).
Logged

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

The reason why i thought to keep everything seperate the way i have is that i want to be able to have the 3 colours varying at different rates depending on the positions of 3 seperate potentiometers. Will this still be possible if i combine them code in the way you suggest?
Logged

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

Quote
Will this still be possible if i combine them code in the way you suggest?
Certainly. Notice that I assume that the fade amount will be different.

Now, keep in mind that the code you are developing is going to fade one color at a time. The time taken to fade the LED up and down (or down and up) will vary, but a completely different approach is needed to fade the three colors at the same time.
Logged

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

I think i need to brush up on my understanding of functions. I've never used them before and i must be doing something wrong;

Code:
int sensorValueRed;   //reading from the red pot
int sensorValueGrn;   //reading from the Green pot
int sensorValueBlu;   //reading from the Blue pot
int brightnessRed;    // how bright the  red LED is
int brightnessGrn;    // how bright the  Green LED is
int brightnessBlu;    // how bright the  Blue LED is
int fadeAmountRed;    // how many points to fade the red LED by
int fadeAmountGrn;    // how many points to fade the Grn LED by
int fadeAmountBlu;    // how many points to fade the Blu LED by
const int ledRedPin = 3;      //pin red LED is attached to
const int ledGrnPin = 5;      //pin red LED is attached to
const int ledBluPin = 6;      //pin red LED is attached to
const int sensorPinRed = 0;   //pin for the red potentiometer
const int sensorPinGrn = 1;   //pin for the red potentiometer
const int sensorPinBlu = 2;   //pin for the red potentiometer

void setup() 
{
Serial.begin(57600);  //begin serial communication
}

void fade(int ledPin, int fadeAmount, unsigned long delayTime)
{
   for(int i=0; i<=255; i+=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
   for(int i=255; i>=0; i-=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
}

void loop() 


{
fade(ledRedPin, fadeAmountRed, 30);
fade(ledGrnPin, fadeAmountGrn, 30);
fade(ledBluPin, fadeAmountBlu, 30);
 
}

I am sure i'm not using this right.
Logged

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

Quote
I am sure i'm not using this right.
The only thing I can see that you are doing wrong is omitting a description of the problem.
Logged

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

Well i don't really understand how the code is supposed to work that i've copied from your example because i'm not very familiar with functions so i'm not sure exactly what it is supposed to do but i'm guessing i should be seeing the LEDs light up because of the section that calls the function and they remain unlit. Should i be seeing light or would i need to add more code before that would happen?
Logged

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

fadeAmountRed is not getting assigned a value, so it defaults to 0. Since the value starts at 0, and increments by 0, it might take a while before the light actually gets bright enough to see.
Logged

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

Aha, now i'm getting somewhere (thanks).

Now it ramps up and down nicely (with time 30 or whatever), but only once.

Code:
int sensorValueRed;   //reading from the red pot
int sensorValueGrn;   //reading from the Green pot
int sensorValueBlu;   //reading from the Blue pot
int brightnessRed;    // how bright the  red LED is
int brightnessGrn;    // how bright the  Green LED is
int brightnessBlu;    // how bright the  Blue LED is
int fadeAmountRed;    // how many points to fade the red LED by
int fadeAmountGrn;    // how many points to fade the Grn LED by
int fadeAmountBlu;    // how many points to fade the Blu LED by
const int ledRedPin = 3;      //pin red LED is attached to
const int ledGrnPin = 5;      //pin red LED is attached to
const int ledBluPin = 6;      //pin red LED is attached to
const int sensorPinRed = 0;   //pin for the red potentiometer
const int sensorPinGrn = 1;   //pin for the red potentiometer
const int sensorPinBlu = 2;   //pin for the red potentiometer

void setup()  
{
}

void fade(int ledPin, int fadeAmount, unsigned long delayTime)
{
   for(int i=0; i<=255; i+=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
   for(int i=255; i>=0; i-=fadeAmount)
   {
      analogWrite(ledPin, i);
      delay(delayTime);
   }
}

void loop()  


{
fadeAmountRed=1;
fade(ledRedPin, fadeAmountRed, 8);
fade(ledGrnPin, fadeAmountGrn, 30);
fade(ledBluPin, fadeAmountBlu, 30);
  
}

I've got
Code:
fadeAmountRed=1;
in the loop and
Code:
fade(ledRedPin, fadeAmountRed, 8)
; so i don't see why it isn't repeating.
« Last Edit: March 04, 2012, 01:33:13 pm by chris1982 » Logged

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

Well, it would, except that it gets hung up the green loop, for the same reason. Fix the blue value, too.
Logged

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