#include <FastLED.h>
#include <OneButton.h>
#define NUM_LEDS 300
#define LED_PIN 2
#define BTN_PIN 3
uint8_t MODES = 22;
bool flag = true;
int bright = 255;
uint8_t hue = 0;
uint8_t mode = 22;
uint8_t podmode = 0;
CRGB leds[NUM_LEDS];
int index = 0;
OneButton btn = OneButton(BTN_PIN, true, true);
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(100);
Serial.begin(9600);
btn.attachDoubleClick(next_mode);
btn.attachClick(next_podmode);
btn.attachDuringLongPress(change_hue);
FastLED.setMaxPowerInVoltsAndMilliamps(5,4000);
}
void loop() {
btn.tick();
main_loop();
}
void next_mode() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
index = 0;
podmode = 0;
mode = ++mode % (MODES+1);
flag = true;
}
void next_podmode() {
podmode = ++podmode %5;
}
void change_hue() {
hue+=2;
}
void main_loop() {
switch(mode) {
case 0:
raduga();
break;
case 1:
rainbow_duos();
break;
case 2:
sparkles();
break;
case 3:
singlecolor_rgb();
break;
case 4:
flash();
break;
case 5:
flash_rgb();
break;
case 6:
flash_rainbow();
break;
case 7:
flash_double();
break;
case 8:
flash_double_rainbow();
break;
case 9:
singlecolor();
break;
case 10:
pulse();
break;
case 11:
pulse_rgb();
break;
case 12:
solo();
break;
case 13:
solo(); //solor_rgb
if (index == 0 && flag) {hue += random(200);}
break;
case 14:
doub();
break;
case 15:
doub(); //double_rgb
if (index == 0 && flag) {hue += random(200);}
break;
case 16:
strobe();
break;
case 17:
color();
break;
case 18:
dots();
break;
case 19:
dots_double();
break;
case 20:
police();
break;
case 21:
police_strobe();
break;
case 22:
start_finish();
break;
default:
leds[0] = CRGB(255, 0, 0);
leds[1] = CRGB(0, 255, 0);
leds[2] = CRGB(0, 0, 255);
FastLED.show();
break;
}
}
void raduga() {
fill_rainbow(leds, NUM_LEDS, hue, 6);
FastLED.show();
hue-=1*podmode;
}
void rainbow_duos() {
uint8_t start_hue = hue;
for (int i = 0; i<NUM_LEDS/2; i++) {
leds[NUM_LEDS-i-1] = leds[i] = CHSV(hue, 255, 255);
hue-=6;
}
FastLED.show();
hue=start_hue;
hue+=podmode;
}
void sparkles() {
int r;
if(podmode%3 == 0) {
r = random(0,NUM_LEDS);
leds[r] = CHSV(random8(), 255, 255);
}
if(podmode%3 == 1) {
r = random(1,NUM_LEDS);
leds[r-1] = leds[r] = CHSV(random8(), 255, 255);
}
if(podmode%3 == 2) {
r = random(1,NUM_LEDS-1);
leds[r+1] = leds[r-1] = leds[r] = CHSV(random8(), 255, 255);
}
blur1d(leds, NUM_LEDS, 150);
fadeToBlackBy(leds, NUM_LEDS, 2);
FastLED.show();
}
void singlecolor_rgb() {
for(int i=0; i<NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 255);
}
hue+=1+1*podmode;
FastLED.show();
if(hue >= 255) hue = 0;
}
void flash() {
leds[index] = CHSV(hue, 255, 255);
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 1+2*podmode);
index++;
if(index > NUM_LEDS) index = 0;
}
void flash_rainbow() {
leds[index] = CHSV(hue, 255, 255);
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 1+2*podmode);
index++;
hue+=3;
if(index > NUM_LEDS) index = 0;
}
void flash_rgb() {
leds[index] = CHSV(hue, 255, 255);
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 1+2*podmode);
index++;
if(index > NUM_LEDS) {
index = 0;
hue+=random(200);
}
}
void flash_double() {
leds[NUM_LEDS-index] = leds[index] = CHSV(hue, 255, 255);
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 5+20*podmode);
index++;
if(index > NUM_LEDS) index = 0;
}
void flash_double_rainbow() {
leds[NUM_LEDS-index] = leds[index] = CHSV(hue, 255, 255);
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 5+20*podmode);
index++;
hue+=3;
if(index > NUM_LEDS) index = 0;
}
void singlecolor() {
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 150+(255-150)/4*podmode);
}
FastLED.show();
}
void pulse() {
if(flag) {
bright-=6-1*podmode;
if (bright < 0) {
bright = 0;
flag = false;
}
}
if(!flag) {
bright+=6-1*podmode;
if (bright > 255) {
bright = 255;
flag = true;
}
}
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, bright);
}
FastLED.show();
}
void pulse_rgb() {
if(flag) {
bright-=6-1*podmode;
if (bright < 0) {
bright = 0;
flag = false;
}
}
if(!flag) {
bright+=6-1*podmode;
if (bright > 255) {
bright = 255;
flag = true;
}
}
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, bright);
}
hue++;
FastLED.show();
}
void solo() {
if(flag) {
leds[index] = CHSV(hue, 255, 255);
index++;
if(index>NUM_LEDS+1) {
index = 0;
flag=false;
}
}
if(flag == false) {
leds[index] = CHSV(hue, 255, 0);
index++;
if(index>NUM_LEDS+1) {
index = 0;
flag=!flag;
}
}
if(podmode%2 == 1) hue+=10;
FastLED.show();
}
void doub() {
if(flag) {
leds[NUM_LEDS-index] = leds[index] = CHSV(hue, 255, 255);
index++;
if(index>NUM_LEDS/2+1) {
index = 0;
flag=false;
}
}
if(flag == false) {
leds[NUM_LEDS-index] = leds[index] = CHSV(hue, 255, 0);
index++;
if(index>NUM_LEDS/2+1) {
index = 0;
flag=!flag;
}
}
if(podmode%2 == 1) hue+=5;
FastLED.show();
}
void strobe() {
fill_solid(leds, NUM_LEDS, CHSV(hue, 255, 255));
FastLED.show();
delay((140+30*podmode)*20/100);
fill_solid(leds, NUM_LEDS, CHSV(hue, 255, 0));
FastLED.show();
delay(140+30*podmode);
}
void color() {
int k = 6+6*podmode;
fill_solid(leds, NUM_LEDS, CHSV(hue, 255, 255));
for (int i=0; i<=k; i++) {
leds[NUM_LEDS-index-i] = leds[index+i]= CRGB(255,255,255);
if(index + i < NUM_LEDS/2) leds[index+i+NUM_LEDS/2] = leds[NUM_LEDS/2-index-i] = CRGB::White;
if(index + i > NUM_LEDS/2) leds[index+i-NUM_LEDS/2] = leds[NUM_LEDS-index-i+NUM_LEDS/2] = CRGB::White;
}
index++;
if(index>NUM_LEDS-k) index = 0;
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
}
void dots() {
for (int i = 0; i<NUM_LEDS; i++) {
if((index+i)%(10+(10*podmode)) == 0) {
leds[i] = CHSV(hue, 255, 255);
if(i == 0) leds[NUM_LEDS-1] = CHSV(hue, 255, 205);
}
if((index+i)%(10+(10*podmode)) == (10+(10*podmode))/2) {
leds[i] = CHSV((hue+170)%255, 255, 255);
if(i == 0) leds[NUM_LEDS-2] = CHSV(hue+170, 255, 205);
}
}
index++;
if(index > 10+(10*podmode)) index = 0;
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
delay(50);
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
void dots_double() {
for (int i = NUM_LEDS/2; i<NUM_LEDS; i++) {
if((index+i)%(10+(2*podmode)) == 0) leds[NUM_LEDS-i-1] = leds[i] = CHSV(hue, 255, 255);
if((index+i)%(10+(2*podmode)) == (10+(2*podmode))/2) leds[NUM_LEDS-i-1] = leds[i] = CHSV((hue+170)%255, 255, 255);
}
index++;
if(index > 10+(2*podmode)) index = 0;
blur1d(leds, NUM_LEDS, 150);
FastLED.show();
delay(50);
fill_solid(leds, NUM_LEDS, CRGB::Black);
}
void police() {
int k = 6;
for (int i=0; i<k; i++) {
leds[index+i] = CHSV(0, 255, 255);
if(index+i+2*NUM_LEDS/3 < NUM_LEDS) leds[index+i+2*NUM_LEDS/3] = CHSV(158, 255, 255);
else if(index+i > NUM_LEDS/3-1) leds[index+i-NUM_LEDS/3] = CHSV(158,255,255);
if(index+i+NUM_LEDS/3 < NUM_LEDS) leds[index+i+NUM_LEDS/3] = CHSV(158, 0, 255);
else if(index+i > 2*NUM_LEDS/3-1) leds[index+i-2*NUM_LEDS/3] = CHSV(158,0,255);
}
FastLED.show();
index+=1+1*podmode;
if (index > NUM_LEDS) index=0;
fadeToBlackBy(leds, NUM_LEDS, 10+10*podmode);
}
void police_strobe() {
if (index < 3) {
for(int i = 0; i<NUM_LEDS/2; i++) leds[i] = CRGB(0, 0, 255);
}
else {
for(int i = NUM_LEDS/2; i<NUM_LEDS; i++) leds[i] = CRGB(255, 0, 0);
}
index++;
if (index == 6) index = 0;
FastLED.show();
delay((140+30*podmode)*20/100);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(140+30*podmode);
}
void start_finish() {
if (flag) {
leds[index] = CHSV(hue, 255, 255);
}
else {
leds[NUM_LEDS-index] = CHSV(hue, 255, 255);
}
FastLED.show();
fadeToBlackBy(leds, NUM_LEDS, 10);
index++;
if (index>NUM_LEDS-1) {
flag = !flag;
index = 0;
hue+=random(255);
}
}