r/ArduinoProjects • • 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
}

0 Upvotes

7 comments sorted by

View all comments

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.

1

u/Eastern-Move549 Jan 18 '24

You sir are my hero!