/// @file DemoReel100.ino
/// @brief FastLED "100 lines of code" demo reel, showing off some effects
/// @example DemoReel100.ino
#include <FastLED.h>
FASTLED_USING_NAMESPACE
// FastLED "100-lines-of-code" demo reel, showing just a few
// of the kinds of animation patterns you can quickly and easily
// compose using FastLED.
//
// This example also shows one easy way to define multiple
// animations patterns and have them automatically rotate.
//
// -Mark Kriegsman, December 2014
#define DATA_PIN_R 9
#define DATA_PIN_L 10
//#define CLK_PIN 4
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 54
#define NUM_COLS 9
#define RUN_EVERY 10
// Define variable names for pins
#define BUTTON_PIN_STOP 1
#define BUTTON_PIN_PARK 4
#define BUTTON_PIN_REVERSE 6
#define BUTTON_PIN_TURN_L 7
#define BUTTON_PIN_TURN_R 8
// Define booleans to recognize states
bool flagSTOP = false;
bool flagREVERSE = false;
bool flagPARK = false;
bool flagSTOPfadeComplete = false; // Variable to track fade-in completion
bool flagTURN_LEFT = false;
bool flagTURN_RIGHT = false;
uint8_t fadeDirection = 1;
uint8_t val = 0;
uint8_t currColumn = 1;
uint8_t currRun = 11;
// initiate the two led strips
CRGB leds_r[NUM_LEDS];
CRGB leds_l[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 120
void setup() {
delay(2000); // 3 second delay for recovery
pinMode(BUTTON_PIN_STOP, INPUT_PULLUP);
pinMode(BUTTON_PIN_PARK, INPUT_PULLUP);
pinMode(BUTTON_PIN_REVERSE, INPUT_PULLUP);
pinMode(BUTTON_PIN_TURN_L, INPUT_PULLUP);
pinMode(BUTTON_PIN_TURN_R, INPUT_PULLUP);
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN_L,COLOR_ORDER>(leds_l, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.addLeds<LED_TYPE,DATA_PIN_R,COLOR_ORDER>(leds_r, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
}
// List of patterns available as lights, which will be called based on input signals
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = {sinelon, StopFadeIn, turnSignalRight, turnSignalRight, parkLights, turnSignalRight, reverseLightsOnly, turnSignalLeft2, turnSignalRight2};
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void loop()
{
// Check Inputs
flagSTOP = !digitalRead(BUTTON_PIN_STOP);
flagPARK = !digitalRead(BUTTON_PIN_PARK);
flagREVERSE = !digitalRead(BUTTON_PIN_REVERSE);
flagTURN_LEFT = !digitalRead(BUTTON_PIN_TURN_L);
flagTURN_RIGHT = !digitalRead(BUTTON_PIN_TURN_R);
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
//EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
if (flagSTOP && !flagTURN_LEFT && !flagTURN_RIGHT) {
// only STOP light engaged on PIN1
gCurrentPatternNumber = 1;
} else if (!flagSTOP && flagTURN_LEFT && !flagTURN_RIGHT) {
//only LEFT turn engaged on PIN7
gCurrentPatternNumber = 7;
} else if (!flagSTOP && !flagTURN_LEFT && flagTURN_RIGHT) {
//only RIGHT turn engaged on PIN8
gCurrentPatternNumber = 8;
}
else if (!flagSTOP && !flagTURN_LEFT && !flagTURN_RIGHT && !flagREVERSE && flagPARK) {
// only PARK is engaged on PIN4
gCurrentPatternNumber = 4;
}
else if (!flagSTOP && !flagTURN_LEFT && !flagTURN_RIGHT && flagREVERSE) {
// only REVERSE is engaged on PIN6
gCurrentPatternNumber = 6;
}
else {
gCurrentPatternNumber = 0;
// reset STOP fade in value to 0 and reste the flag to false
val = 0;
flagSTOPfadeComplete = false;
currColumn = 1;
currRun = 1;
}
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void rainbow()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds_l, NUM_LEDS, gHue, 7);
fill_rainbow( leds_r, NUM_LEDS, gHue, 7);
}
void rainbowWithGlitter()
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds_r[ random16(NUM_LEDS) ] += CRGB::Red;
}
}
void confetti()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds_r, NUM_LEDS, 10);
int pos = random16(NUM_LEDS);
leds_r[pos] += CHSV( 0 + random8(64), 200, 255);
}
void sinelon()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds_l, NUM_LEDS, 20);
fadeToBlackBy( leds_r, NUM_LEDS, 20);
int pos = beatsin16( 13, 0, NUM_LEDS-1 );
leds_l[pos] += CHSV( gHue, 255, 192);
leds_r[pos] += CHSV( gHue, 255, 192);
}
void bpm()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
//CRGBPalette16 palette = ForestColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for( int i = 0; i < NUM_LEDS; i++) { //9948
leds_r[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
leds_l[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
}
}
void juggle() {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds_r, NUM_LEDS, 5);
uint8_t dothue1 = 0;
uint8_t dothue2 = 96;
//for( int i = 0; i < 8; i++) {
leds_r[beatsin16( 10, 0, NUM_LEDS/2 -1 )] |= CHSV(dothue1, 255, 200);
leds_r[beatsin16( 10, NUM_LEDS/2, NUM_LEDS-1, 0, 32766 )] |= CHSV(dothue1, 255, 200);
//dothue += 32;
//}
}
void turnSignalLeft() {
fadeToBlackBy( leds_l, NUM_LEDS, 2);
fadeToBlackBy( leds_r, NUM_LEDS, 10);
uint8_t dothue1 = 32;
uint8_t pos = map(beat8(40, 0), 255, 0, NUM_LEDS, 0 );
leds_l[pos] = CHSV(dothue1, 255, 200);
}
void turnSignalRight() {
fadeToBlackBy( leds_r, NUM_LEDS, 6);
uint8_t dothue1 = 32;
uint8_t pos = map(beat8(40, 0), 0, 255, 0, NUM_LEDS);
leds_r[pos] = CHSV(dothue1, 255, 200);
}
void turnSignalLeft2() {
fadeToBlackBy(leds_l, NUM_LEDS, 10);
fadeToBlackBy(leds_r, NUM_LEDS, 10);
uint8_t dothue1 = 32;
uint8_t startPos = 0;
uint8_t endPos = NUM_LEDS;
uint8_t NUM_IN_GROUP = (NUM_LEDS - startPos) / NUM_COLS; // Specify the number of LEDs in each group
if (currRun % RUN_EVERY == 0) {
for (int i = 0; i < NUM_IN_GROUP; i++) {
// Set the color for each group of LEDs
// column: currColumn = 1
int pos = (currColumn*NUM_IN_GROUP + i)-NUM_IN_GROUP + startPos;
leds_l[pos] = CHSV(dothue1, 255, 200);
}
if (currColumn != NUM_COLS) {
currColumn++;
}
else {
currColumn=1;
}
}
currRun++;
if (flagREVERSE) reverseLights();
}
void turnSignalRight2() {
fadeToBlackBy(leds_l, NUM_LEDS, 10);
fadeToBlackBy(leds_r, NUM_LEDS, 10);
uint8_t dothue1 = 32;
uint8_t startPos = 0;
uint8_t endPos = NUM_LEDS;
uint8_t NUM_IN_GROUP = (NUM_LEDS - startPos) / NUM_COLS; // Specify the number of LEDs in each group
if (currRun % RUN_EVERY == 0) {
for (int i = 0; i < NUM_IN_GROUP; i++) {
// Set the color for each group of LEDs
// column: currColumn = 1
int pos = (currColumn*NUM_IN_GROUP + i)-NUM_IN_GROUP + startPos;
leds_r[pos] = CHSV(dothue1, 255, 200);
}
if (currColumn != NUM_COLS) {
currColumn++;
}
else {
currColumn=1;
}
}
currRun++;
if (flagREVERSE) reverseLights();
}
void reverseLightsOnly()
{
fadeToBlackBy(leds_l, NUM_LEDS, 10);
fadeToBlackBy(leds_r, NUM_LEDS, 10);
uint8_t NUM_IN_GROUP = NUM_LEDS / NUM_COLS;
for (int col = 0; col < NUM_COLS; col++) {
// Calculate the index of the first LED in the bottom half of the column
int startLEDIndex = col * NUM_IN_GROUP;
// Iterate through each LED in the bottom half of the column
for (int i = 0; i < NUM_IN_GROUP / 2; i++) {
// Calculate the index of the current LED
int ledIndex = startLEDIndex + i;
// Do whatever you need to do with this LED
// For example:
leds_r[ledIndex] = CRGB::White;
}
for (int i = (NUM_IN_GROUP / 2); i < NUM_IN_GROUP; i++) {
// Calculate the index of the current LED
int ledIndex = startLEDIndex + i;
// Do whatever you need to do with this LED
// For example:
leds_l[ledIndex] |= CRGB::White;
}
}
}
void reverseLights()
{
//fadeToBlackBy(leds_l, NUM_LEDS, 10);
//fadeToBlackBy(leds_r, NUM_LEDS, 10);
uint8_t NUM_IN_GROUP = NUM_LEDS / NUM_COLS;
for (int col = 0; col < NUM_COLS; col++) {
// Calculate the index of the first LED in the bottom half of the column
int startLEDIndex = col * NUM_IN_GROUP;
// Iterate through each LED in the bottom half of the column
for (int i = 0; i < NUM_IN_GROUP / 4; i++) {
// Calculate the index of the current LED
int ledIndex = startLEDIndex + i;
// Do whatever you need to do with this LED
// For example:
leds_r[ledIndex] = CRGB::White;
}
for (int i = (NUM_IN_GROUP / 4)*3; i < NUM_IN_GROUP; i++) {
// Calculate the index of the current LED
int ledIndex = startLEDIndex + i;
// Do whatever you need to do with this LED
// For example:
leds_l[ledIndex] |= CRGB::White;
}
}
}
void stopLights()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
// uint8_t BeatsPerMinute = 62;
// CRGBPalette16 palette = PartyColors_p;
// uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
//fadeToBlackBy( leds_r, NUM_LEDS, 20);
//fadeUsingColor( leds_r, NUM_LEDS, CRGB::Red);
fill_solid( leds_r, NUM_LEDS/2, CRGB::Red);
for( int i = NUM_LEDS/2; i < NUM_LEDS; i++) { //9948
//leds_r[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
leds_r[i] += CHSV( 0, 255, 192);
}
}
void parkLights()
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
// uint8_t BeatsPerMinute = 62;
// CRGBPalette16 palette = PartyColors_p;
// uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
fadeToBlackBy( leds_l, NUM_LEDS, 10);
fadeToBlackBy( leds_r, NUM_LEDS, 10);
//fadeUsingColor( leds_r, NUM_LEDS, CRGB::Red);
uint8_t NUM_IN_GROUP = NUM_LEDS / NUM_COLS; // Specify the number of LEDs in each group
fill_solid( leds_r, NUM_LEDS/2, CRGB::Red);
for( int i = 0; i < NUM_LEDS; i++) { //9948
//leds_r[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
if (i%2 > 0) leds_l[i] += CHSV( 0, 255, 192);
//leds_r[i] += CHSV( 0, 255, 100);
}
}
void StopFadeIn ()
{
// this will animation STOP light as fadeIn animation and stop at finish color
// set start and finish colours for STOP animation
CRGB start = CRGB::Black;
CRGB finish = CRGB::Red;
// Increment blend fraction until it reaches maximum (255)
if (val < 255) {
val = val + (fadeDirection * 1);
} else {
// If blend fraction reaches maximum, fade-in effect is completed
flagSTOPfadeComplete = true;
}
if (!flagSTOPfadeComplete) {
// blend from start to finish by the fraction of val
// const CRGB c = blend(start, finish, val);
const CRGB c = nblend(start, finish, val);
fill_solid(leds_l, NUM_LEDS, c);
fill_solid(leds_r, NUM_LEDS, c);
}
}