Multiple LED fade coding.

Hi everyone!! I recently bought an arduino uno and started having fun with it very recently. Being new to this and this progging language, i had to look around alot and compare info and methods from alot of different years and versions. Ultimately my arduino uno came with nothing but itself and a USB cable, so bought a breadboard and some LEDs, resistors and wires from a local electronics shop and started having fun from scratch and undocumented parts, and when i tried to do a 5 LED fade sequence, that's when the fun became work... So i finally figured it out and tweaked the code enough to make it work, starting from the fade example that came with the IDE and i figured this code might help out a few people that had the same problem with LED glitches and such. So here is the final code that got it working without a hitch for me. Note you can tweak the delay if you want to change the speed that the fade moves, but the rest of it kind of needs to be kept intact if you want it to work, and if you have less or more LEDs, you can always copy and change the numbers for the variables, but the UNO has only 6 PWM outputs, so if you have more than 6 LEDs you will need to create layers and go about doing it another way or getting a bigger arduino than the UNO. Hope this helps some people and that you enjoy this code. :slight_smile:

PS. if you have any upgrades, corrections, easier ways to do this, feel free to reply, as i'm just a beginner and litteraly started this project about 45 minutes ago, so i know it might need some TLC and that it's a bit rough, and i'm always open to learning and getting better, so any help/insight/improvements are always welcome! Looking forward to helping and learning from this community! Cheers!

/*
  Fade 5 LEDs

  This example shows how to fade an LED on pins 11-10-9-6-5 using the analogWrite()
  function.

*/

int led_1 = 11;
int led_2 = 10;
int led_3 = 9;
int led_4 = 6;
int led_5 = 5;
int brightness_1 = 254;    // how bright the LED is
int fadeAmount_1 = 5;    // how many points to fade the LED by
int brightness_2 = 192;    // how bright the LED is
int fadeAmount_2 = 5;    // how many points to fade the LED by
int brightness_3 = 128;    // how bright the LED is
int fadeAmount_3 = 5;    // how many points to fade the LED by
int brightness_4 = 64;    // how bright the LED is
int fadeAmount_4 = 5;    // how many points to fade the LED by
int brightness_5 = 1;    // how bright the LED is
int fadeAmount_5 = 5;    // how many points to fade the LED by



// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led_1, OUTPUT);
  pinMode(led_2, OUTPUT);
  pinMode(led_3, OUTPUT);
  pinMode(led_4, OUTPUT);
  pinMode(led_5, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
    analogWrite(led_1, brightness_1);
    if (brightness_1 <= 0) {
    OCR2A = 0;
}
    if (brightness_1 >= 255) {
    OCR2A = 255;
}
  brightness_1 = brightness_1 + fadeAmount_1;
  if (brightness_1 <= 0 || brightness_1 >= 255){
    fadeAmount_1 = -fadeAmount_1;
    }
    analogWrite(led_2, brightness_2);
    if (brightness_2 <= 0) {
    OCR1B = 0;
}
    if (brightness_2 >= 255) {
    OCR1B = 255;
}
  brightness_2 = brightness_2 + fadeAmount_2;
  if (brightness_2 <= 0 || brightness_2 >= 255){
    fadeAmount_2 = -fadeAmount_2;
    }
    analogWrite(led_3, brightness_3);
    if (brightness_3 <= 0) {
    OCR1A = 0;
}
    if (brightness_3 >= 255) {
    OCR1A = 255;
}
  brightness_3 = brightness_3 + fadeAmount_3;
  if (brightness_3 <= 0 || brightness_3 >= 255){
    fadeAmount_3 = -fadeAmount_3;
    }
    analogWrite(led_4, brightness_4);
    if (brightness_4 <= 0) {
    OCR0A = 0;
}
    if (brightness_4 >= 255) {
    OCR0A = 255;
}
  brightness_4 = brightness_4 + fadeAmount_4;
  if (brightness_4 <= 0 || brightness_4 >= 255){
    fadeAmount_4 = -fadeAmount_4;
    }  
    analogWrite(led_5, brightness_5);
    if (brightness_5 <= 0) {
    OCR0B = 0;
}
    if (brightness_5 >= 255) {
    OCR0B = 255;
}
  brightness_5 = brightness_5 + fadeAmount_5;
  if (brightness_5 <= 0 || brightness_5 >= 255){
    fadeAmount_5 = -fadeAmount_5;
    }
    
    // wait for 40 milliseconds to see the dimming effect
  delay(40);
}

Your code contains lots of sections that are repeated (almost exactly) 5 times over for each of the 5 leds. Look into using arrays to shrink your code down to almost one fifth of its current size.

Code lines like this

    OCR2A = 0;

Are difficult for beginners to understand and not portable to other types of Arduino. What do they do and why are you using them?