Hello, i am trying to use FastLED and neopixel strips for the first time. Short version i want the leds to light when one switch is pressed and to change color palettes when a second switch is pressed. I came up with what I though was good code but it seems to be non functional. I have rule out faulty hardware as a potential issue. i have attached the code, I just need another set of eyes to see what I am not because it is most likely painfully obvious.
TIA
#include <FastLED.h> // led strip library
#include <SoftwareSerial.h> // softsware serial for df player connection
#include <DFRobotDFPlayerMini.h>// DFRobotDFPlayerMini - Version: Latest
#define LED_PIN 3
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define NUM_LEDS 14
#define BRIGHTNESS 200
#define FRAMES_PER_SECOND 60
bool gReverseDirection = false;
CRGB leds[NUM_LEDS];
SoftwareSerial sound_serial (7, 8); // set rx and tx for serial communication
DFRobotDFPlayerMini sound_FX; // defines df player as sound fx
int trigger_switch = 4; // trigger switch for plasma pistol
byte blue_barrel = 11; // blue rgb led signal for barrel flash
byte red_barrel = 9; // red rgb led signal for barrel flash
byte green_barrel = 10; // green rgb led signal for barrel flash
byte vibe_motor = 6; // viberation motor transistor base signal
byte beaver_switch = 12; // beaver tail saftey switch
byte power_led = 5; // power on led indicator
byte push_count = 0; // set push count for trigger pull, start at zero
byte last_state2; // last status for tigger pull
byte last_state1; // last status for safety switch
unsigned long previousMillis1 = 0; // set start time zero
unsigned long previousMillis2 = 0; // set start time zero
unsigned long previousMillis3 = 0; // set start time zero
unsigned long previousMillis4 = 0; // set start time zero
unsigned long previousMillis5 = 0; // set start time zero
byte trigger;
byte safety;
byte trigger_state;
byte safety_state;
byte rando;
CRGBPalette16 gPal;
void setup() {
delay(500);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
pinMode(trigger_switch, INPUT_PULLUP); // set trigger switch as input with pull up resistor
pinMode(blue_barrel, OUTPUT); // set blue barrel led pin as output
pinMode(green_barrel, OUTPUT); // set green barrel led pin as output
pinMode(red_barrel, OUTPUT); // set red barrel led pin as output
pinMode(vibe_motor, OUTPUT); // set transitor base pin for vibe motor control as output
pinMode(beaver_switch, INPUT_PULLUP); // set beaver tail saftey switch as input with ppull up resistor
pinMode(power_led, OUTPUT); // set pin to output
analogWrite(power_led, 100);
sound_serial.begin(9600);
sound_FX.volume(25);
sound_FX.play("start up");
Serial.begin(9600);
delay(500);
}
void loop() {
random16_add_entropy( random());
unsigned long currentMillis = millis(); // set current time in milliseconds since board turned on
trigger = digitalRead(trigger_switch); // read trigger switch pull
safety = digitalRead(beaver_switch); // read beaver tail saftey switch pull
rando = random(20);
Serial.println("safe off");
if (safety == LOW && trigger == HIGH) {
safety_state = safety;
if (safety_state != last_state1) {
if (safety_state == LOW) {
sound_FX.play("energize"); // play "energize" power up sound FX
digitalWrite(vibe_motor, HIGH); // turn on vibe motor
if (currentMillis - previousMillis1 >= 1500) { // measure 400 millis
previousMillis1 = currentMillis; //
digitalWrite(vibe_motor, LOW); // turn off vibe motor after 400 millis
}
}
last_state1 = safety_state;
}
if (push_count <= 4) {
gPal = CRGBPalette16( CRGB::Blue, CRGB::Aqua, CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 5) {
gPal = CRGBPalette16( CRGB( 0, 0, 255), CRGB( 80, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 6) {
gPal = CRGBPalette16( CRGB( 50, 0, 255), CRGB( 0, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 7) {
gPal = CRGBPalette16( CRGB( 100, 0, 255), CRGB( 0, 200, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 8) {
gPal = CRGBPalette16( CRGB( 150, 0, 255), CRGB( 0, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 9) {
gPal = CRGBPalette16( CRGB( 200, 0, 255), CRGB( 0, 100, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 10) {
gPal = CRGBPalette16( CRGB( 255, 0, 255), CRGB( 0, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 11) {
gPal = CRGBPalette16( CRGB( 255, 0, 200), CRGB( 100, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 12) {
gPal = CRGBPalette16( CRGB( 255, 0, 150), CRGB( 150, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 13) {
gPal = CRGBPalette16( CRGB( 255, 0, 100), CRGB( 200, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
if (push_count == 14) {
gPal = CRGBPalette16( CRGB( 255, 0, 0), CRGB( 255, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
FastLED.delay(1000 / FRAMES_PER_SECOND);
}
}
if (safety == LOW && trigger == LOW) {
trigger_state = trigger; // registers each time the trigger button is pressed
if (trigger_state != last_state2 && currentMillis - previousMillis2 >= 2000) { // alternates between on/off states to deploy/retract blade
if (trigger_state == LOW) {
push_count++;
}
last_state2 = trigger_state;
previousMillis2 = currentMillis;
}
if (trigger_state != last_state2) {
if (trigger_state == LOW) {
if (push_count <= 4 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 200, 255, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 200); // red led on
analogWrite(green_barrel, 255); // max green led
delay(700);
gPal = CRGBPalette16( CRGB::Blue, CRGB::Aqua, CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 5 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 210, 240, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 210); // red led on
analogWrite(green_barrel, 240); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 0, 0, 255), CRGB( 80, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 6 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 210, 240, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 210); // red led on
analogWrite(green_barrel, 240); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 50, 0, 255), CRGB( 0, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 7 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 220, 230, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 220); // red led on
analogWrite(green_barrel, 230); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 100, 0, 255), CRGB( 0, 200, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 8 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 220, 230, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 220); // red led on
analogWrite(green_barrel, 230); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 150, 0, 255), CRGB( 0, 150, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 9 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 230, 220, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 230); // red led on
analogWrite(green_barrel, 220); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 200, 0, 255), CRGB( 0, 100, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 10 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 230, 220, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 230); // red led on
analogWrite(green_barrel, 220); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 255, 0, 255), CRGB( 0, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 11 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 240, 210, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 240); // red led on
analogWrite(green_barrel, 210); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 255, 0, 200), CRGB( 100, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 12 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 240, 210, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 240); // red led on
analogWrite(green_barrel, 210); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 255, 0, 150), CRGB( 150, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 13 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 255, 200, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 255); // red led on
analogWrite(green_barrel, 200); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 255, 0, 100), CRGB( 200, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
Serial.println(push_count);
}
if (push_count == 14 && rando != 1) {
sound_FX.play("fire"); // play fire sound fx
gPal = CRGBPalette16( CRGB( 255, 200, 255));
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH); // turn on vibration
analogWrite(blue_barrel, 255); // max blue led
analogWrite(red_barrel, 255); // red led on
analogWrite(green_barrel, 200); // max green led
delay(700);
gPal = CRGBPalette16( CRGB( 255, 0, 0), CRGB( 255, 0, 255), CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 0);
analogWrite(green_barrel, 0);
analogWrite(red_barrel, 0);
digitalWrite(vibe_motor, LOW);
}
Serial.println(push_count);
}
if (push_count >= 15) {
sound_FX.play("overheat");
gPal = CRGBPalette16( CRGB::Red, CRGB::DarkRed, CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
digitalWrite(vibe_motor, HIGH);
if (currentMillis - previousMillis3 >= 88000) {
digitalWrite(vibe_motor, LOW);
gPal = CRGBPalette16( CRGB::Blue, CRGB::Aqua, CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
previousMillis3 = currentMillis;
push_count = 0;
}
Serial.println(push_count);
}
}
}
if (safety == LOW && trigger == LOW && rando == 1) {
sound_FX.play("Nat1");
digitalWrite(vibe_motor, HIGH);
gPal = CRGBPalette16( CRGB::White);
Fire2012WithPalette(); // run simulation frame, using palette colors
FastLED.show(); // display this frame
analogWrite(blue_barrel, 250); // max blue led
analogWrite(red_barrel, 250); // red led on
analogWrite(green_barrel, 250); // max green led
delay(6000);
digitalWrite(vibe_motor, LOW);
analogWrite(blue_barrel, 0); // max blue led
analogWrite(red_barrel, 0); // red led on
analogWrite(green_barrel, 0); // max green led
}
if ( currentMillis - previousMillis4 >= 3000 && trigger_state == last_state2) {
trigger_state = trigger;
if ( currentMillis - previousMillis5 >= 600 && trigger_state == last_state2) {
push_count--;
previousMillis5 = currentMillis;
}
previousMillis4 = currentMillis;
last_state2 = trigger_state;
}
else {
gPal = CRGBPalette16( CRGB::Black);
FastLED.show(); // display this frame
digitalWrite(vibe_motor, LOW);
analogWrite(blue_barrel, 0); // max blue led
analogWrite(red_barrel, 0); // red led on
analogWrite(green_barrel, 0); // max green led
}
}
// Fire2012 by Mark Kriegsman, July 2012
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
////
// This basic one-dimensional 'fire' simulation works roughly as follows:
// There's a underlying array of 'heat' cells, that model the temperature
// at each point along the line. Every cycle through the simulation,
// four steps are performed:
// 1) All cells cool down a little bit, losing heat to the air
// 2) The heat from each cell drifts 'up' and diffuses a little
// 3) Sometimes randomly new 'sparks' of heat are added at the bottom
// 4) The heat from each cell is rendered as a color into the leds array
// The heat-to-color mapping uses a black-body radiation approximation.
//
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
// Looks best on a high-density LED setup (60+ pixels/meter).
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames. More cooling = shorter flames.
// Default 55, suggested range 20-100
#define COOLING 55
// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire. Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 120
void Fire2012WithPalette()
{
// Array of temperature readings at each simulation cell
static uint8_t heat[NUM_LEDS];
// Step 1. Cool down every cell a little
for ( int i = 0; i < NUM_LEDS; i++) {
heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
}
// 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' of heat near the bottom
if ( random8() < SPARKING ) {
int y = random8(7);
heat[y] = qadd8( heat[y], random8(160, 255) );
}
// Step 4. Map from heat cells to LED colors
for ( int j = 0; j < NUM_LEDS; j++) {
// Scale the heat value from 0-255 down to 0-240
// for best results with color palettes.
uint8_t colorindex = scale8( heat[j], 240);
CRGB color = ColorFromPalette( gPal, colorindex);
int pixelnumber;
if ( gReverseDirection ) {
pixelnumber = (NUM_LEDS - 1) - j;
} else {
pixelnumber = j;
}
leds[pixelnumber] = color;
}
}