r/ArduinoProjects • u/Eastern-Move549 • Jan 17 '24
LED fading maddness
I will start this by saying I really dont know what I'm doing!
I managed to get some basic effects to work but this fade on/off is driving me up the wall.
I have an Arduino Mini connected via USB and a strip of addressable LEDs.
When running the following code, the LEDs will fade up and down as expected once.
After the first cycle its gets stuck in a fade off loop and does no longer fade on.
The fade on/off are different colours purely for troubleshooting.
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 332
CRGB leds[NUM_LEDS];
uint8_t brightness = 0;
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 800);
}
void loop() {
// Fade on
for (brightness = 0; brightness <= 50; brightness++) {
FastLED.setBrightness(brightness);
fill_solid(leds, NUM_LEDS, CRGB(255, 0, 255)); // Pink color
FastLED.show();
delay(10);
}
// Fade off
for (brightness = 50; brightness >= 0; brightness--) {
FastLED.setBrightness(brightness);
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 0)); // Yellow color
FastLED.show();
delay(10);
}
brightness = 0; // Reset brightness for next cycle
delay(100); // Pause between cycles
}
1
u/BraveNewCurrency Jan 18 '24
It could be a power problem. If you have a lot of leds, you need lots of Amps of current (generally about 60mA per LED for RGB LEDs.)
1
u/Eastern-Move549 Jan 18 '24
There is a line to set the max power for this reason.
When they were getting to bright the arduino would freeze.
1
2
u/Chemichicka Jan 17 '24 edited Jan 18 '24
Why do you have a curly bracket after your Fade on code? If the editor didn't flag this it might be why your code gets stuck on a Fade off loop because it doesn't look at your Fade on code again.
Delete the curly brackets after the Fade on and Fade off blocks of code and this should fix your problem.
1
u/tipppo Jan 17 '24
Brightness is defined as uint8_t which is an unsigned 8 bit value. It can't go negative, just wraps to 255, so brightness >= 0; is always true. Maybe try using "brightness > 0;" or make brightness an int, signed 16 bit value. The compiler will automatically convert it to a uint8 for the call to setBrightness.