#include <FastLED.h>
#include <EncButton.h>
#include <TimerMs.h>
#define BTN_PIN 2
#define LED_PIN 4
#define PHOTORES_PIN 3
#define COLOR_ORDER RGB
#define CHIPSET WS2812
#define NUM_LEDS 16
#define FRAMES_PER_SECOND 60
#define COOLING 55
#define SPARKING 120
bool photores_status, gReverseDirection = false;
byte baza = 0, BRIGHTNESS = 120;
int8_t mode = 1, power_mode = 1, counter = 5;
CRGB leds[NUM_LEDS];
Button btn(BTN_PIN, INPUT, HIGH);
TimerMs tmr(1000, 1, 1);
void setup() {
delay(3000);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
pinMode(PHOTORES_PIN, INPUT);
tmr.setPeriodMode();
photores_status = digitalRead(PHOTORES_PIN);
}
void loop() {
btn.tick();
if (btn.hasClicks(2)) {
power_mode += 1;
if (power_mode > 3) power_mode = 1;
}
if (btn.click()) {
delay(100);
BRIGHTNESS += 60;
if ((BRIGHTNESS > 240) || (BRIGHTNESS == 44)) BRIGHTNESS = 60;
}
if (btn.hold()) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Blue;
}
FastLED.show();
delay(1000);
mode += 1;
if (mode > 7) mode = 1;
}
check_photores();
if (((photores_status == true) && (power_mode == 1)) || (power_mode == 2)) {
FastLED.setBrightness(BRIGHTNESS);
if (mode == 1) {
rainbow();
} else if (mode == 2) {
rainbow_2();
} else if (mode == 3) {
fire();
} else if (mode == 4) {
fire_2();
} else if (mode == 5) {
cyclone();
} else if (mode == 6) {
focus();
} else if (mode == 7) {
confetti();
}
}
if (((photores_status == false) && (power_mode == 1)) || (power_mode == 3)) {
FastLED.setBrightness(0);
FastLED.show();
delay(20);
}
}
void check_photores() {
bool status;
if (tmr.tick()) {
status = digitalRead(PHOTORES_PIN);
if (status == true) {
counter += 1;
} else {
counter -= 1;
}
}
if (counter == 10) {
photores_status = true;
counter = 5;
} else if (counter == 0){
photores_status = false;
counter = 5;
}
}
void fire()
{
static byte heat[NUM_LEDS];
for( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
for( int k= NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
}
if( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160,255) );
}
for( int j = 0; j < NUM_LEDS; j++) {
CRGB color = HeatColor( heat[j]);
int pixelnumber;
if( gReverseDirection ) {
pixelnumber = (NUM_LEDS-1) - j;
} else {
pixelnumber = j;
}
leds[pixelnumber] = color;
}
FastLED.show();
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
void rainbow()
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(baza+ i * 5, 255, 255);
}
baza++;
FastLED.show();
delay(20);
}
void rainbow_2()
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(baza+ i * 5, 255, 255);
}
baza++;
FastLED.show();
delay(20);
}
void fire_2()
{
fadeToBlackBy(leds, NUM_LEDS, 2);
int pos = beatsin16(13, 0, NUM_LEDS - 1);
leds[pos] += CHSV(baza++, 255, 192);
FastLED.show();
delay(20);
}
void cyclone()
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(250);
}
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(baza++, 255, 255);
FastLED.show();
delay(20);
}
}
void focus()
{
fadeToBlackBy(leds, NUM_LEDS, 2);
for (int i = 0; i < 8; i++) {
leds[beatsin16(i + 7, 0, NUM_LEDS - 1)] |= CHSV(baza+=16, 200, 255);
}
FastLED.show();
delay(20);
}
void confetti()
{
fadeToBlackBy(leds, NUM_LEDS, 2);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV(baza++ + random8(64), 200, 255);
FastLED.show();
delay(20);
}