improve the code

This commit is contained in:
hybris 2022-02-19 00:56:13 +01:00
parent f03ab40cb2
commit 903e059b4a

View File

@ -1,50 +1,40 @@
#include "FastLED.h"
#define NUM_LEDS 18 // # of LEDS in the strip
CRGB leds[NUM_LEDS];
#define NUM_LEDS 60 // # of LEDS in the strip
#define DATA_PIN 2 // Output Pin to Data Line on Strip
// Change effect here
#define DELAY 200
///////
int fadeAmount = 1; // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
int brightness = 0;
int fade_switch = 0;
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
}
int colors[3][3] = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } };
int counter = 0;
int color_index = 0;
void loop() {
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i].setRGB(colors[color_index][0],colors[color_index][1],colors[color_index][2]);
leds[i].fadeLightBy(brightness);
}
FastLED.show();
int hue = get_hue();
int new_brightness = get_brightness();
for(int i = 0; i < NUM_LEDS; i++ )
{
leds[i] = CHSV(hue,255,new_brightness);
}
FastLED.show();
FastLED.delay(100);
}
uint16_t gHue = 0;
uint8_t gHueDelta = 1;
int get_hue() {
gHue += gHueDelta;
gHue = gHue % 360;
return gHue;
}
int brightness = 0;
int fadeAmount = 5; // 5, 10, 15, 20, 25, ..., 255.
int get_brightness() {
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if(brightness == 0 || brightness == 255)
{
fadeAmount = -fadeAmount;
}
if(brightness == 255)
{
fade_switch = fade_switch+1;
}
if(fade_switch == 1) {
fade_switch = 0;
counter = counter + 1;
color_index = counter % 3;
}
if (counter >= 2048) {
counter = 0;
}
delay(DELAY);
return brightness;
}