How to program a fade into a string of LED's

Hi All.

I have 7 Leds in a string, Each LED is connected to an output pin on my board. I want the LED's to look like a bubble moving upwards from the bottom of the string. I have code that makes this effect, but I would like it so the LED's fade in and out of brightness, instead of just turning on and off. Any help is appreciated!

int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int LED7 = 7;
int LED8 = 6;


void setup() {

   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
 
}

void loop() {

  digitalWrite(LED2, HIGH);    // turn on LED2
  delay(100);                  // wait for 200ms      
  digitalWrite(LED3, HIGH);    // turn on LED3
  delay(100);                  // wait for 200ms
  digitalWrite(LED2, LOW);    // turn on LED2
  delay(100);     
  digitalWrite(LED3, LOW);     // turn off LED3
  delay(100);                  // wait for 200ms  
  digitalWrite(LED4, HIGH);    // turn on LED2
  delay(100);                  // wait for 200ms      
  digitalWrite(LED5, HIGH);    // turn on LED3
  delay(100);                  // wait for 200ms
  digitalWrite(LED4, LOW);    // turn on LED2
  delay(100);     
  digitalWrite(LED5, LOW);     // turn off LED3
  delay(100);                  // wait for 200ms   
  digitalWrite(LED6, HIGH);    // turn on LED2
  delay(100);                  // wait for 200ms      
  digitalWrite(LED7, HIGH);    // turn on LED3
  delay(100);                  // wait for 200ms
  digitalWrite(LED6, LOW);    // turn on LED2
  delay(100);     
  digitalWrite(LED7, LOW);     // turn off LED3
  delay(100);                  // wait for 200ms  
  digitalWrite(LED8, HIGH);    // turn on LED2
  delay(100);                  // wait for 200ms      
  digitalWrite(LED8, LOW);    // turn on LED2
  delay(100);

First off, using delays is not a good way of waiting as program execution stops for the delay amount of time.

Google ‘Blink Without Delay’.


To fade a LED, you can use a ‘for()’ loop and use PWM to modulate the LED intensity.

If this is an UNO, the on-board controller has 3, 5, 6, 9, 10, 11 that you can analogWrite to i.e. these are PWM pins.

For other pins, you could make your own function to do PWM, but this is more evolved.

Thank you for that!

I will shift my LED's to the correct pins, could you provide me with a code example please?

You should be able to modify this to your needs:

//Version 1.02
//
//Fade 6 LEDs from OFF to ON then back to OFF, afterword advance to the next LED

//************************************************************

#define LEDon   HIGH
#define LEDoff  LOW

#define HIGHER  0
#define LOWER   1

//************************************************************
//Available PWM pins,
//UNO:  3, 5, 6, 9, 10, 11
//Mega: 2 - 13, 44 - 46
const byte PWMpin[]   = {3, 5, 6, 9, 10, 11};
const byte increment  = 2;        //change in 'light level' per iteration

byte index;                       //the LED we are currently fading
byte fadeDirection    = HIGHER;   //0 = going brighter, 1 = going dimmer

int level;                        //the current brightness of the LED

//Timing stuff
unsigned long LEDmillis;
const unsigned long LEDspeed = 5; //5ms


//************************************************************
void setup()
{
  for (byte x = 0; x < 6; x++)
  {
    pinMode(PWMpin[x], OUTPUT);
    digitalWrite(PWMpin[x], LEDoff);
  }

} //END of setup()

//************************************************************
void loop()
{
  //***********************************
  //is it time to adjust the LED brightness?
  if (millis() - LEDmillis >= LEDspeed)
  {
    //restart the timer
    LEDmillis = millis();

    fadeLED();
  }

  //***********************************
  //other non blocking code
  //***********************************

} //END of loop()


//************************************************************
void fadeLED()
{
  //send the next brightness level to our 'current' LED 
  analogWrite(PWMpin[index], level);

  //***********************************
  switch (fadeDirection)
  {
    //*************************
    case HIGHER:
      //increase the 'next' level
      level = level + increment;

      //have we reached the maximum level?
      if (level > 255)
      {
        //start going down again
        level = 254;
        
        //switching to fading down
        fadeDirection = LOWER;
      }

      break;

    //*************************
    case LOWER:
      //decrease the 'next' level
      level = level - increment;

      //have we reached the minimum level?
      if (level < 0)
      {
        //start going up again
        level = 0;
        analogWrite(PWMpin[index], level);

        //next LED to operate on
        index++;
                  
        //have we finished with the last LED?
        if (index > 5)
        {
          //start back at the first LED
          index = 0;
        }

        //switching to fading up
        fadeDirection = HIGHER;

      }

      break;

    //*************************

  } //END of switch/case

} //END of fadeLED()

//************************************************************