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
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.
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.
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()'
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
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;
}
}
}