Snippets

Henry Tonoyan The Sims Halloween Costume LED Code

Created by Henry Tonoyan
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 16

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 17

// Define the array of leds
CRGB leds[NUM_LEDS];
CHSV current_color; // Temporary HSV variable to make it easy to change colors

#define GREEN_TIME 576
#define RED_TIME 38

void setup() { 
    uint8_t i;
    FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);

    // Set all LEDs to initial color (Green)
    for(i=0; i<NUM_LEDS; i++)
    {
        leds[i] = CRGB::Green;
    }
    FastLED.show();
    current_color = CHSV(96, 255, 255);

    Serial.begin(9600);
    Serial.print("Start\n");
}

void loop() { 
    uint8_t i;
    static int8_t increment = -1;
    static uint8_t state = 0;
    static uint32_t state_timer = 0;

    // state 0: Hold on to green for some time
    // go into state 1 when time is up
    if (state == 0)
    {
        state_timer += 1;
        if(state_timer == GREEN_TIME)
        {
            state_timer = 0;
            state = 1;
            Serial.print("Finished green time\n");
        }
    }
    // state 1: roll down to red
    // go into state 2 when red
    else if(state == 1)
    {
        current_color.hue -= 1;
        if (current_color.hue == 0)
        {
            state = 2;
            Serial.print("Reached red, going back to green\n");
        }
    }
    // state 2: wait at red
    // go into state 3 when done waiting
    else if(state == 2)
    {
        state_timer += 1;
        if(state_timer == RED_TIME)
        {
            state_timer = 0;
            state = 3;
            Serial.print("Finished red time\n");
        }
    }
    // state 3: roll up to green
    // go into state 0 when green
    else if(state == 3)
    {
        current_color.hue += 1;
        if(current_color.hue == 96)
        {
            state = 0;
            Serial.print("Reached green, waiting\n");
        }
    }

    // Show LEDs
    hsv2rgb_rainbow(current_color, leds[0]);
    for(i=1; i<NUM_LEDS; i++)
    {
        leds[i] = leds[0];
    }
    FastLED.show();

    delay(1562);
}

Comments (0)