Dad needs help Fading multiple LEDs

So my son and i have been working with an arduino uno board using it to install LEDs in a VanGogh starry night scene. I have everything wired but am struggling with the programming of different fades for different pins. There are 9 pins (leds) we wanted to fade but research ive learned only 5 pins on the arduino board can be faded in and out. I have soent hours trying to figure out how to write the code. Ive followed examples on here, youtube, etc and i cant get it to work. Im only able to get 1 led to fade. I try copying code for the others and i get nowhere. Was hoping somone could help me write the code.

Led pins 3, 5, 6 10, 11 can be faded. I guess ill just javewd pins 6, 8, 12 and 14 do a delayed blink

The LEDs in the moon we have wired to stay on constant whichbis what we want. We wanted to other LEDs (stars) to fade.

Welcome to the forum

Pin 9 is also capable of PWM output

Are all of the LEDs going to fade at the same time and finish at different times or some other sequence ?

Hey there UKHeliBob. Ideally we wanted the 9 LEDs that we have as stars to fade in and out at diffretent times/rates. Im only able to get 1 to work in programming. I tried copying the code and assigning it to a different pin but i get these error codes.

Why don't you fade them in pairs? With 6 PWM pins, you could fade up to 12 LEDs.

Please post your code.

What error codes would they be ?

Hey Red Car. Good question. This is above my expertise. Im not sure how to wire it that way.

We want the leds in the moon to stay on constant and then we wanted to fade the 9 leds we have in the stars

Hi Dad
What color are the LEDs, I'm thinking white? What resistor values are you using?
I'd stick with the six for now, get it working, then you can play with doubling them up.
If you show us what code you've tried, we may be able to recommend a fix. Barring that, you'll get a lot of suggestions as to how to do this, but they may not fit with what you've done.

Hey Cam.

They are all yellow LEDs. Let me write the code i started with.

I assume that is is too late for you to change from normal LEDs to Neopixels

Posting pictures of code is a waste of time

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Yeah it is. They are all glued and soldered in

LED lens caps.

Looking forward to a proper code posting, per Bob's message. From what I can see in the pic, you may have more than one setup(), and more than one loop(). That's incorrect. Your error message is probably:
void loop() {
^~~~
exit status 1
redefinition of 'void setup()'

or something similar.
C

An example for you to try

struct ledData
{
  byte ledPin;
  unsigned int fadeInterval;
  int currentValue;
  unsigned long previousStepStart;
};

ledData leds[] =
{
  {3, 20, 0, 0},
  {5, 40, 0, 0},
  {6, 50, 0, 0},
  {9, 30, 0, 0}
};

const byte NUM_LEDS = sizeof(leds) / sizeof(leds[0]);

void setup()
{
  Serial.begin(115200);
  for (int led = 0; led < NUM_LEDS; led++)
  {
    pinMode(leds[led].ledPin, OUTPUT);
    //    digitalWrite(leds[led].ledPin, LOW);  //start with LEDS on
  }
}

void loop()
{
  fadeLeds();
}

void fadeLeds()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < NUM_LEDS; led++)
  {
    if (currentTime - leds[led].previousStepStart >= leds[led].fadeInterval)
    {
      leds[led].currentValue++;
      if (leds[led].currentValue > 255)
      {
        leds[led].currentValue = 255;
      }
      analogWrite(leds[led].ledPin, leds[led].currentValue);
      leds[led].previousStepStart = currentTime;
    }
  }
}

NOTE :
This example fades 4 LEDS from 255 to 0 but can be extended to as many PWM capable pins as you have by adding more LEDs to the definition of the leds array

The values in each row of the leds array are the LED pin number and the interval between fade steps in milliseconds. The other values should be left as zero and will be used by the sketch. The sketch will adjust itself to the number of LEDs defined when you compile it

As written the fade start as soon as the sketch runs but it could be started in a number of ways (you have not specified what starts the fade) and whether it is running or not controlled by a boolean variable where true means run and false means don't run

2 Likes

how to post code

Bob - thank you so much for taking the time to post this code. I copied it and now I have 4 leds that are constant on. I did read your note but I'm not sure what it means. Sorry.

The problem is caused by the fact that my LEDs are wired active LOW, ie a LOW value turns them on, whereas yours appear to be wired active HIGH so that I HIGH value turns them on

This is amended version of the sketch which should work for your setup

struct ledData
{
  byte ledPin;
  unsigned int fadeInterval;
  int currentValue;
  unsigned long previousStepStart;
};

ledData leds[] =
{
  {3, 30, 255, 0},
  {5, 40, 255, 0},
  {6, 50, 255, 0},
  {9, 25, 255, 0}
};

const byte NUM_LEDS = sizeof(leds) / sizeof(leds[0]);

void setup()
{
  Serial.begin(115200);
  for (int led = 0; led < NUM_LEDS; led++)
  {
    pinMode(leds[led].ledPin, OUTPUT);
  }
}

void loop()
{
  fadeLeds();
}

void fadeLeds()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < NUM_LEDS; led++)
  {
    if (currentTime - leds[led].previousStepStart >= leds[led].fadeInterval)
    {
      leds[led].currentValue--;
      if (leds[led].currentValue <0)
      {
        leds[led].currentValue = 0;
      }
      analogWrite(leds[led].ledPin, leds[led].currentValue);
      leds[led].previousStepStart = currentTime;
    }
  }
}

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