fadeAmount 4 leds

Hi all,
I can’t figure out how to fade (4) leds individually from bright 255 down to 0. For instance led_1 HIGH then fades to LOW, next led_2 HIGH then fades to LOW and so on..I think the delay30; is screwing up the sequence.

I tried an example with 2 leds with the programming attached but the sequence isn’t working right. I am hopeful someone can fix me here.

Thanks!

posting code as a bad photo is the worst idea ever.
How do you expect that this can anyone read and put in his IDE?
Have you read the forum introduction - a sticky post on the top of every subforum?

To your question:
I would use my LED Tooklikt, see the heartbeat LED if the effect meets your need. It can fade in/out several LEDs in parallel. Examples are provided in the library.

In general, no delay() needed.
Use millis() if you don't want to block your code.

#include <Noiasca_led.h>               // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

HeartbeatPin heartbeatLedA {5};         // UNO PWM pins 3, 5, 6, 9, 10, 11 (on any other pin the LED will just blink)
HeartbeatPin heartbeatLedB {6};

void setup() {
  Serial.begin(115200);
  heartbeatLedA.begin();   // you have to call the .begin() method for the LED pair
  heartbeatLedB.begin();
  //heartbeatLedA.on();      // you can switch the LED on
  //heartbeatLedB.on();
  heartbeatLedA.setInterval(30);           // you can modify the delay time for the pulsing. Default value is 50.
  heartbeatLedB.setInterval(30);
  //heartbeatLedA.setMinBrightness(50);    // you can limit the mininum brightness. Default value is 0
  //heartbeatLedA.setMaxBrightness(240);   // you can limit the maximum brightness. Default value is 255. Highest value on Arduino is 255. 
  heartbeatLedA.setCurrentBrightness(255); // you can set the current brightness. Default value is 0. Highest value on Arduino is 255. 
  //heartbeatLedB.setCurrentBrightness(0); // you can set the current brightness. Default value is 0. Highest value on Arduino is 255. 

}

void loop() {
  heartbeatLedA.update();   // you have to call update() for the LED
  heartbeatLedB.update();
}

if you want to expand to more LEDs, consider to put the LEDs in an array.

#include <Noiasca_led.h>               // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

HeartbeatPin heartbeatLed[] {5, 6, 9, 10};         // UNO PWM pins 3, 5, 6, 9, 10, 11 (on any other pin the LED will just blink)

void setup() {
  for (auto & i : heartbeatLed)
  {
    i.begin(); // you have to call the .begin() method for the LED
    i.setInterval(30);
    //i.setMinBrightness(50);    // you can limit the mininum brightness. Default value is 0
    //i.setMaxBrightness(240);   // you can limit the maximum brightness. Default value is 255. Highest value on Arduino is 255. 
  }
  heartbeatLed[0].setCurrentBrightness(255); // you can set the current brightness. Default value is 0. Highest value on Arduino is 255. 
  heartbeatLed[1].setCurrentBrightness(0);  
  heartbeatLed[2].setCurrentBrightness(64);
  heartbeatLed[3].setCurrentBrightness(150);
}

void loop() {
  for (auto & i : heartbeatLed) i.update(); // you have to call update() for the LED
}

Look at File -> Examples -> 03.Analog -> Fading

Take the "fade out" part:

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

Duplicate that for the four pins (or use an array of pin numbers and a loop to run the same code four times with four different pins).

void loop()
{
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
  {
    analogWrite(led_1, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
  {
    analogWrite(led_2, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
  {
    analogWrite(led_3, fadeValue);
    delay(30);
  }

  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
  {
    analogWrite(led_4, fadeValue);
    delay(30);
  }
}

There's also a library for that:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.