help with RGB LED fading (noob)

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.

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.

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.

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.

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

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.

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;

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.

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

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

I think that would be a good start.

Changing ;

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

to

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

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

If it can go greater than 255, it can also, presumably, go less than 0. Have you catered for that?

the other 2 colours are commented out

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
if (brightnessRed < 0 || brightnessRed > 255) {
    fadeAmountRed = -fadeAmountRed ;
  }

Doesn’t seem to help at all.

My code in full;

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.

// wait for 30 milliseconds to see the dimming effect    
     delay(1);

When code is commented like this, I stop reading.

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.

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.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup()  
{
  Serial.begin(57600);
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

Is not all that easy for me to understand, but i’ll go away and try and work out how to make that output the fadeAmountRed value (i presume that’s what you are getting at?)

As madworm says, there should be one statement ONLY in your setup () Serial.begin (bitRateOfyourChoice);

Get rid of the green and blue code, it's just noise.

Let it output whatever you need.

Not just the fade-amount, but also the counting variable itself. Only with the latter you will more easily see if it gets stuck somewhere. And of course it is the variable that gets checked by your if-statement.

And I recommend to have a look at the example code: "Files-->Examples-->Basics-->DigitalReadSerial".

And I recommend to have a look at the example code: “Files–>Examples–>Basics–>DigitalReadSerial”.

I don’t seem to have those. I have files>examples> but no basics within that subsection. I guess i’ll try reinstalling. Have they been added recently?

I’ve minimised the code slightly, i still don’t seem to be getting any joy;

int sensorValueRed = 0; //store the value from the potentiometer
int brightnessRed = 0;    // how bright the LED is
int fadeAmountRed = 0;    // how many points to fade the LED by
int ledPinRed = 3;      //pin LED is attached to
int sensorPinRed = 0;   //pin for the potentiometer
#include <SoftwareSerial.h>

void setup()  {
  // declare pin 9 to be an output:
  Serial.begin(57600);
  pinMode(ledPinRed, OUTPUT);
 }

void loop()  {
  sensorValueRed = analogRead(sensorPinRed); //read the value from the sensor as sensorValue
  analogWrite(ledPinRed, brightnessRed);    //set ledPinRed to brightnessRed
  fadeAmountRed = sensorValueRed/300;       //set the fadeamountred from potentiometer value

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

  
  if (brightnessRed < 0 || brightnessRed > 255)   // reverse the direction of the fading at the ends of the fade:
  { 
    fadeAmountRed = -fadeAmountRed ;
  }   
   Serial.write(sensorValueRed);
     delay(1);                                     //delay 1 ms
}

The seriel monitor outputs gobbledygook even when i set the bitrate correctly. Maybe i’ve picked too difficult a project to start with after doing the basic examples.

O well, i’ll get it working eventually. So far i can make it do this;

Compiled but obviously untested.

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

void setup()  
{
  Serial.begin(57600);
}

void loop()  
{
  int sensorValueRed = analogRead(sensorPinRed); 
  analogWrite(ledPinRed, brightnessRed);  
  fadeAmountRed = sensorValueRed/300;
 
  brightnessRed = brightnessRed + fadeAmountRed; 

  
  if (brightnessRed < 0 || brightnessRed > 255)  
  { 
    fadeAmountRed = -fadeAmountRed ;
  }   
   Serial.print ("Value ");
   Serial.println(sensorValueRed);
   Serial.print ("Fade ");
   Serial.println(fadeAmountRed);
   Serial.print ("Brightness ");
   Serial.println(brightnessRed);
}