Help with my first program (LED Fade)

Hello everyone! I just bought my Arduino a few weeks ago and did a few LED tutorials. Last night I tried to tackle my first simple sketch based off the Beginner>Fade sketch. It seemed pretty straight forward, but unfortunately I’m having some issues and wondering if anyone could give me some assistance.

The program takes 3 RGB LEDs each starting with a different color. It slowly brightens then dims to off, rotates to the next color then repeats. It’s hard to describe what the output is doing, but for the most part, each LED is white and sometimes one will turn a single color fade up to bright then turn back to white or another color.

Thanks in advance for any help! I’m really looking forward to learning more about this.

  • For some reason, the array brackets with an “i” disappeared below, so I’ve replaced them with (i)
//Defines the 3 output pins of each LED
int ledOne[] = {14, 15, 16};  //(Red, Green, Blue)
int ledTwo[] = {10, 11, 9};  //(Green, Blue, Red)
int ledThree[] = {6, 3, 5};  //Blue, Red, Green)

int COLOR = 0;  //Used to reference the current location in the LED arrays

int brightness = 0; //Initial brightness
int fadeAmount = 5; //Incrimental amount

void setup()  
{  
  /* Sets all pins to output*/
  for (int i = 0; i < 3; i++) {
    pinMode(ledOne(i), OUTPUT);
    pinMode(ledTwo(i), OUTPUT);
    pinMode(ledThree(i), OUTPUT);
  }
} 

void loop()  
{
 for (int i = 0; i < 3; i++) {  //Loops through each color in the LED array
   if (i == COLOR) {   //Sets the brightness of current output color
     analogWrite(ledOne(i), brightness); 
     analogWrite(ledTwo(i), brightness); 
     analogWrite(ledThree(i), brightness); 
   }
   else {  //Sets all other colors to 0 brightness
     analogWrite(ledOne(i), 0); 
     analogWrite(ledTwo(i), 0); 
     analogWrite(ledThree(i), 0); 
   }
 }   
    
  brightness = brightness + fadeAmount; //Increments fade amount
  
  if (brightness == 0 || brightness == 255) {  //Reverses fadeamount
    if (brightness == 0) { //Increments COLOR to rotate LED colors
      COLOR++;
      if (COLOR == 3) { //Resets COLOR to 0
        COLOR = 0;
      }
    }
  }
    
  delay(100);   
  
}

Moderator edit: PRE tags replaced with CODE tags.

For some reason, the array brackets with an "i" disappeared below, so I've replaced them with (i)

If you posted code properly, using the # button, that wouldn't happen.

int ledOne[] = {14, 15, 16};  //(Red, Green, Blue)

Which Arduino do you have? The UNO does not do PWM on these pins.

If you posted code properly, using the # button, that wouldn't happen.

My apologies, rookie mistake (it's my first time posting), I used the preformatted text. I went through the buttons above and missed it.

Which Arduino do you have? The UNO does not do PWM on these pins.

The Uno...you're right. Should it still be working correctly though for LEDs 1 and 2?

Should it still be working correctly though for LEDs 1 and 2?

The code you upload to the Arduino always works right. "Right" and "the way I want" are not always the same thing, though.

It's not clear exactly what you want to have happen. If each led is to fade in a different color, you need to apply the brightness value to one of the three pins in the array on each pass through loop. Storing the red pins in one array, the green pins in another array, and the blue pins in a third array would be easier to manage then trying to store all three pins for one LED in one array.

With red[], grn[], and blu[], write brightness to red[n], grn[n], and blu[n] when n is COUNT. Otherwise, write 0.

PaulS:

With red[], grn[], and blu[], write brightness to red[n], grn[n], and blu[n] when n is COUNT. Otherwise, write 0.

I originally started that way, but found it less confusing for me (I'm sure the more I get used to the language it won't be) to store pins of each LED in an array. Each pin in the LED array is offset by 1 so that LED1[n] is the Red pin, LED2[n] is the Green pin and LED3[n] is the Blue pin. I would have to do the same using the colors as an array, offset each array by 1, or I'd end up with all red, then all blue, etc.

But otherwise, that's what my I thought my code "should" do, write the brightness when n is COUNT, otherwise it writes 0.

So, use Serial.print() to see what your program IS doing.