RGB LED code help

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
jpoglod
 
Posts: 4
Joined: Thu Jul 03, 2014 3:03 am

RGB LED code help

Post by jpoglod »

I am trying to fix a weird problem with my cycling of colors in my code. I am trying to make the colors cycle forward and backwards like this: RED> GREEN> BLUE> YELLOW> CYAN> MAGENTA> WHITE> White> magenta> cyan> yellow> blue> green> red (and repeat). The only problem is that when it gets to white it turns green like this: YELLOW> CYAN> MAGENTA> WHITE> GREEN> White> magenta> cyan.
The code that I wrote is posted here:

Code: Select all

void cycle(){
  int delayTime = 1000; 
 
  for(int i = 0; i <= sizeof(COLORS); i++){
    setColor(ledDigitalOne, COLORS[i]);  
    delay(delayTime);                 
  }                                  
 
  for(int i = 7; i >= sizeof(COLORS); i--){  
    setColor(ledDigitalOne,COLORS[i]);
    delay(delayTime);                 
  }
}
The rest of the code can be viewed here:

Code: Select all

/*     ---------------------------------------------------------
 *     |  Experimentation Kit for Arduino Example Code         |
 *     |  CIRC-RGB .: Colourful Light :. (RGB LED)             |
 *     ---------------------------------------------------------
 * 
 * We've blinked an LED and controlled eight in sequence now it's time to 
 * control colour. Using an RGB LED (actual 3 LEDs in a single housing)  
 * we can generate any colour our heart desires.
 *
 * (we'll also use a few programming shortcuts to make the code 
 * more portable/readable)
 */


//RGB LED pins
int ledDigitalOne[] = {9, 10, 11}; //the three digital pins of the digital LED 
                                   //9 = redPin, 10 = greenPin, 11 = bluePin

const boolean ON = LOW;     //Define on as LOW (this is because we use a common 
                            //Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF}; 
const boolean BLUE[] = {OFF, OFF, ON}; 
const boolean YELLOW[] = {ON, ON, OFF}; 
const boolean CYAN[] = {OFF, ON, ON}; 
const boolean MAGENTA[] = {ON, OFF, ON}; 
const boolean WHITE[] = {ON, ON, ON}; 
//const boolean BLACK[] = {OFF, OFF, OFF}; 

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE};

void setup(){
  for(int i = 0; i < 3; i++){
   pinMode(ledDigitalOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
}

void loop(){

/* Example - 1 Set a color
   Set the three LEDs to any predefined color
*/
   //setColor(ledDigitalOne, RED);    //Set the color of LED one

/* Example - 2 Go through Random Colors
  Set the LEDs to a random color
*/
   //randomColor();
   cycle();
}
void cycle(){
  int delayTime = 1000; 
 
  for(int i = 0; i <= sizeof(COLORS); i++){
    setColor(ledDigitalOne, COLORS[i]);  
    delay(delayTime);                 
  }                                  
 
  for(int i = 7; i >= sizeof(COLORS); i--){  
    setColor(ledDigitalOne,COLORS[i]);
    delay(delayTime);                 
  }
}
void randomColor(){
  int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
  setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
  delay(1000);
}

/* Sets an led to any color
   led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)
   color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)
*/
void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led[i], color[i]);
 }
}

/* A version of setColor that allows for using const boolean colors
*/
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}
I am still pretty new to this language so if I miss something please let me know.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: RGB LED code help

Post by adafruit_support_mike »

You have a couple of problems with your loop controls.

In your first loop:

Code: Select all

  for(int i = 0; i <= sizeof(COLORS); i++){
    setColor(ledDigitalOne, COLORS[i]);  
    delay(delayTime);                 
  }
you have what's called a 'fencepost error', which is a common mistake when dealing with array limits. The general problem is that if you have a four-item array, the range of indices you need are:

array[0], array[1], array[2], array[3]

but sizeof(array) will return '4'. If you loop with the '<=' comparator, the loop will try to find items:

array[0], array[1], array[2], array[3], array[4]

because 4 is less-than-or-Equal-To the value returned from sizeof(). You need to drop the equal-to part from the test to keep the array index in the correct range.

Your second loop:

Code: Select all

  for(int i = 7; i >= sizeof(COLORS); i--){  
    setColor(ledDigitalOne,COLORS[i]);
    delay(delayTime);                 
  }
uses the wrong test. The setup (i=7) and next-step (i--) operations give you a count-down variable, but you're testing that variable to see if it's bigger than something.

What you need is:

Code: Select all

  for(int i = sizeof(COLORS) ; i ; i--){  
    setColor(ledDigitalOne,COLORS[ i-1 ]);
    delay(delayTime);                 
  }
That will iterate for the correct number of steps, but will give you numbers from 7 to 1. Your array wants indices in the range 0 to 6, so you have to subtract 1 when using the variable in the loop.

You could also do it this way:

Code: Select all

  for(int i = sizeof(COLORS) - 1 ; i >= 0 ; i--){  
    setColor(ledDigitalOne,COLORS[ i ]);
    delay(delayTime);                 
  }
which shifts the values to the correct range of indices, but is a bit messier in the loop setup.

jpoglod
 
Posts: 4
Joined: Thu Jul 03, 2014 3:03 am

Re: RGB LED code help

Post by jpoglod »

I did the changes you posted but it does not seem to cycle the way how I originally said. The gif below shows just how it is cycling through the colors. The gif starts later but it should be RED>rest of the gif.
http://gfycat.com/AntiqueGloriousCooter (dont mind the url)

It really seems to behave in a totally different way.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: RGB LED code help

Post by Franklin97355 »

Could you post your modified code as it exists now?

jpoglod
 
Posts: 4
Joined: Thu Jul 03, 2014 3:03 am

Re: RGB LED code help

Post by jpoglod »

Code: Select all

void cycle(){
    int delayTime = 1000; 
 
    for(int i = 0; i < sizeof(COLORS)-1; i++){
    setColor(ledDigitalOne, COLORS[i]);  
    delay(delayTime);
    }                                  
 
    for(int x = sizeof(COLORS) - 1 ; x >= 0 ; x--){  
    setColor(ledDigitalOne,COLORS[ x ]);
    delay(delayTime);                 
  }
}
Full:

Code: Select all

/*     ---------------------------------------------------------
 *     |  Experimentation Kit for Arduino Example Code         |
 *     |  CIRC-RGB .: Colourful Light :. (RGB LED)             |
 *     ---------------------------------------------------------
 * 
 * We've blinked an LED and controlled eight in sequence now it's time to 
 * control colour. Using an RGB LED (actual 3 LEDs in a single housing)  
 * we can generate any colour our heart desires.
 *
 * (we'll also use a few programming shortcuts to make the code 
 * more portable/readable)
 */


//RGB LED pins
int ledDigitalOne[] = {9, 10, 11}; //the three digital pins of the digital LED 
                                   //9 = redPin, 10 = greenPin, 11 = bluePin

const boolean ON = LOW;     //Define on as LOW (this is because we use a common 
                            //Anode RGB LED (common pin is connected to +5 volts)
const boolean OFF = HIGH;   //Define off as HIGH

//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};    
const boolean GREEN[] = {OFF, ON, OFF}; 
const boolean BLUE[] = {OFF, OFF, ON}; 
const boolean YELLOW[] = {ON, ON, OFF}; 
const boolean CYAN[] = {OFF, ON, ON}; 
const boolean MAGENTA[] = {ON, OFF, ON}; 
const boolean WHITE[] = {ON, ON, ON}; 
//const boolean BLACK[] = {OFF, OFF, OFF}; 

//An Array that stores the predefined colors (allows us to later randomly display a color)
const boolean* COLORS[] = {RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE};

void setup(){
  for(int i = 0; i < 3; i++){
   pinMode(ledDigitalOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
}

void loop(){

/* Example - 1 Set a color
   Set the three LEDs to any predefined color
*/
   //setColor(ledDigitalOne, RED);    //Set the color of LED one

/* Example - 2 Go through Random Colors
  Set the LEDs to a random color
*/
   //randomColor();
   cycle();
}
void cycle(){
    int delayTime = 1000; 
 
    for(int i = 0; i < sizeof(COLORS)-1; i++){
    setColor(ledDigitalOne, COLORS[i]);  
    delay(delayTime);
    }                                  
 
    for(int x = sizeof(COLORS) - 1 ; x >= 0 ; x--){  
    setColor(ledDigitalOne,COLORS[ x ]);
    delay(delayTime);                 
  }
}
void randomColor(){
  int rand = random(0, sizeof(COLORS) / 2);  //get a random number within the range of colors
  setColor(ledDigitalOne, COLORS[rand]);  //Set the color of led one to a random color
  delay(1000);
}

/* Sets an led to any color
   led - a three element array defining the three color pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin)
   color - a three element boolean array (color[0] = red value (LOW = on, HIGH = off), color[1] = green value, color[2] =blue value)
*/
void setColor(int* led, boolean* color){
 for(int i = 0; i < 3; i++){
   digitalWrite(led[i], color[i]);
 }
}

/* A version of setColor that allows for using const boolean colors
*/
void setColor(int* led, const boolean* color){
  boolean tempColor[] = {color[0], color[1], color[2]};
  setColor(led, tempColor);
}

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: RGB LED code help

Post by Franklin97355 »

I changed your cycle code to print out the values of i and x and they do cycle but the cycle from 1 to 13 and back. check what sizeof(COLORS) is returning.

jpoglod
 
Posts: 4
Joined: Thu Jul 03, 2014 3:03 am

Re: RGB LED code help

Post by jpoglod »

The simple fix was just dividing the array size.

Code: Select all

for(int i = 0; i < (sizeof(COLORS)-1)/2; i++)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”