#include <FastLED.h>
#define NUM_LEDS 44
CRGB leds[NUM_LEDS];
#define PIN 3
int colorPin = A2;
//Buttons
const int patternSelect = 2; // Pattern Select
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;
void setup() {
FastLED.addLeds<WS2812B, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
pinMode(patternSelect, INPUT);
}
void loop() {
static uint32_t lastChangeMs = 0; // for debouncing
buttonState = digitalRead(patternSelect);
if (millis() - lastChangeMs > 100 && buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
setAll(0, 0, 0); // clear LEDs when changing effect
if (buttonPushCounter == 4 ) {
buttonPushCounter = 1;
}
}
lastChangeMs = millis();
lastButtonState = buttonState;
}
switch (buttonPushCounter) {
case 1:
meteorRain(0xff, 0xff, 0xff, 5, 64, false, 10);
break;
case 2:
Fire(35, 120, 55);
break;
case 3:
color();
break;
//colorWipe(0x00,0xff,0x00, 50);
//colorWipe(0x00,0x00,0x00, 10);
//Fire(35,120,55);
}
}
void color() {
int mappedHue;
mappedHue = map(analogRead(colorPin), 0, 1023, 0, 255);
fill_solid( leds, NUM_LEDS, CHSV(mappedHue, 255, 255));
FastLED.show();
}
void Fire(int Cooling, int Sparking, int SpeedDelay) {
static byte heat[NUM_LEDS];
int cooldown;
EVERY_N_MILLIS(SpeedDelay) {
// Step 1. Cool down every cell a little
for ( int i = 0; i < NUM_LEDS; i++) {
cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
if (cooldown > heat[i]) {
heat[i] = 0;
} else {
heat[i] = heat[i] - cooldown;
}
}
// Step 2. Heat from each cell drifts 'up' and diffuses a little
for ( int k = NUM_LEDS - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Step 3. Randomly ignite new 'sparks' near the bottom
if ( random(255) < Sparking ) {
int y = random(7);
heat[y] = heat[y] + random(160, 255);
//heat[y] = random(160,255);
}
// Step 4. Convert heat to LED colors
for ( int j = 0; j < NUM_LEDS; j++) {
setPixelHeatColor(j, heat[j] );
}
showStrip();
}
}
void setPixelHeatColor (int Pixel, byte temperature) {
// Scale 'heat' down from 0-255 to 0-191
byte t192 = round((temperature / 255.0) * 191);
// calculate ramp up from
byte heatramp = t192 & 0x3F; // 0..63
heatramp <<= 2; // scale up to 0..252
// figure out which third of the spectrum we're in:
if ( t192 > 0x80) { // hottest
setPixel(Pixel, 255, 255, heatramp);
} else if ( t192 > 0x40 ) { // middle
setPixel(Pixel, 255, heatramp, 0);
} else { // coolest
setPixel(Pixel, heatramp, 0, 0);
}
}
void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
static uint16_t i = 0;
EVERY_N_MILLIS(SpeedDelay) {
setPixel(i, red, green, blue);
showStrip();
i++;
if (i == NUM_LEDS) i = 0;
}
}
void theaterChaseRainbow(int SpeedDelay) {
byte *c;
static int j = 0;
static int q = 0;
EVERY_N_MILLIS(SpeedDelay) {
for (int i = 0; i < NUM_LEDS; i = i + 3) {
c = Wheel( (i + j) % 255);
setPixel((i + q) % NUM_LEDS, *c, *(c + 1), *(c + 2)); //turn every third pixel on
}
showStrip();
for (int i = 0; i < NUM_LEDS; i = i + 3) {
setPixel((i + q) % NUM_LEDS, 0, 0, 0); //turn every third pixel off
}
q++;
if (q == 3) {
q = 0;
j++;
if (j == 256) j = 0;
}
}
}
byte * Wheel(byte WheelPos) {
static byte c[3];
if (WheelPos < 85) {
c[0] = WheelPos * 3;
c[1] = 255 - WheelPos * 3;
c[2] = 0;
} else if (WheelPos < 170) {
WheelPos -= 85;
c[0] = 255 - WheelPos * 3;
c[1] = 0;
c[2] = WheelPos * 3;
} else {
WheelPos -= 170;
c[0] = 0;
c[1] = WheelPos * 3;
c[2] = 255 - WheelPos * 3;
}
return c;
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
static int i = 0;
EVERY_N_MILLIS(SpeedDelay) {
// fade brightness all LEDs one step
for (int j = 0; j < NUM_LEDS; j++) {
if ( (!meteorRandomDecay) || (random(10) > 5) ) {
fadeToBlack(j, meteorTrailDecay );
}
}
// draw meteor
for (int j = 0; j < meteorSize; j++) {
if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
setPixel(i - j, red, green, blue);
}
}
showStrip();
i++;
if (i == NUM_LEDS + NUM_LEDS) i = 0;
}
}
void fadeToBlack(int ledNo, byte fadeValue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
uint32_t oldColor;
uint8_t r, g, b;
int value;
oldColor = strip.getPixelColor(ledNo);
r = (oldColor & 0x00ff0000UL) >> 16;
g = (oldColor & 0x0000ff00UL) >> 8;
b = (oldColor & 0x000000ffUL);
r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
strip.setPixelColor(ledNo, r, g, b);
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[ledNo].fadeToBlackBy( fadeValue );
#endif
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}