Hello all. I am by no means a coder. I primarily do 3d printing of movie props and the such. My current project is an Avengers Stormbreaker that has 7 led light strips and an led ring inside to mimic lightening and other visual effects. The Code for the Arduino Nano was included with my STL files. When I verify I get several warnings but it will upload to the board. Once uploaded to the board I get odd behavior from the programs. Below is the coding. I can screen shot the warnings also. Thanks in advance for any help to resolve this issue.
#include <FastLED.h>
#include <stdio.h>
#include <stdlib.h>
#include <hsv2rgb.h>
#define PIN12 12
#define PIN11 11
#define PIN10 10
#define PIN9 9
#define PIN8 8
#define PIN7 7
#define PIN6 6
#define PIN5 5
#define MAX_STRIPS 8
#define MAX_LED_STRIP 30
#define LIGHTNING_PROB 450
#define COLOR_ORDER RGB
#define LED_TYPE WS2812B
int STATE = 0;
int lastStates[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // 0: incandescent, 1: lightning, 2: burst
int time_stamp = 0;
int buttonKnob = 0;
int burstStage = 0;
int pace = 60;
int burstOption = 0;
int lastState = 0;
int ticksExplode = 0;
bool burstToIdle = false;
uint8_t max_brightness = 255;
CRGB leds[MAX_STRIPS][MAX_LED_STRIP];
void lightning(int strip, int maxlength, int probability);
void incandescentYellow(int strip, int maxlength);
void incandescentRed(int strip, int maxlength);
void incandescent();
void lightningIdle();
void burst();
void gatherForBurst(int strip);
void prepareForBurst(int strip);
void explode(int strip);
void off();
void turnOff(int strip);
void stillIncandescent();
void red(int strip);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000); // sleep for one second, to give you time to do a pose
pinMode(buttonKnob, INPUT);
LEDS.addLeds<LED_TYPE, PIN8, COLOR_ORDER>(leds[0], 12);// We are telling FastLed that we have a 15 LED strip of the type WS2812B connected to pin 12
LEDS.addLeds<LED_TYPE, PIN5, COLOR_ORDER>(leds[1], 18);
LEDS.addLeds<LED_TYPE, PIN11, COLOR_ORDER>(leds[2], 18);
// Hammer head LEDs
LEDS.addLeds<LED_TYPE, PIN10, COLOR_ORDER>(leds[3], 16); // Hammer head bottom
LEDS.addLeds<LED_TYPE, PIN6, COLOR_ORDER>(leds[4], 16); // Hammer head top
LEDS.addLeds<LED_TYPE, PIN12, COLOR_ORDER>(leds[5], 10); // Hammer head left
LEDS.addLeds<LED_TYPE, PIN9, COLOR_ORDER>(leds[6], 10); // Hammer head right
LEDS.addLeds<LED_TYPE, PIN7, COLOR_ORDER>(leds[7], 24); // Hammer head ring
FastLED.setBrightness(max_brightness);
}
void burst() {
switch (burstStage) {
case 0:
gatherForBurst(0);
lastStates[0] = 2;
break;
case 1:
prepareForBurst(0);
break;
case 2:
explode(0, 100);
explode(1, 100);
explode(2, 100);
explode(3, 100);
explode(4, 100);
explode(5, 100);
delay(1000/60);
break;
case 3:
burstToIdle = true;
STATE = 3;
break;
}
}
void gatherForBurst(int strip) {
//CRGB dimmedWhite(5,5,5);
//fill_solid(leds[strip], sizeof(leds[strip]), dimmedWhite);
for (int i = 14 - 1; i >= 0; i--) {
CHSV color = rgb2hsv_approximate(leds[strip][i]);
if(lastStates[strip] != 2){
leds[0][i].setRGB(97, 63, 169); // Turn all the LED strip to a soft blue
leds[1][i].setRGB(97, 63, 169); // Turn all the LED strip to a soft blue
leds[2][i].setRGB(97, 63, 169); // Turn all the LED strip to a soft blue
Serial.println("Soft blue");
FastLED.show();
if (i >= 18 - 1) {
pace = pace - 30;
}
if (i <= 5) {
pace = pace + 30;
}
}
else if(leds[strip][i].blue == 0 || i == sizeof(leds[strip]) - 1){ // if it's ready for a flash
leds[strip][i].setRGB(200, 200, 255); // Flash
if(i - 1 > 0){ // Prepare for flash
leds[0][i].setRGB(0, 0, 0);
leds[1][i].setRGB(0, 0, 0);
leds[2][i].setRGB(0, 0, 0);
Serial.println("Preparing");
FastLED.show();
}
if (i >= 18 - 1) {
pace = pace - 30;
}
if (i <= 5) {
pace = pace + 30;
}
}
else if(leds[strip][i].blue <= 255){ // If the led just flashed
leds[0][i].setHSV(color.h, color.s, color.v - 15);
leds[1][i].setHSV(color.h, color.s, color.v - 15);
leds[2][i].setHSV(color.h, color.s, color.v - 15);
leds[3][i].setHSV(color.h, color.s, color.v - 15);
leds[4][i].setHSV(color.h, color.s, color.v - 15);
leds[5][i].setHSV(color.h, color.s, color.v - 15);
leds[6][i].setHSV(color.h, color.s, color.v - 15);
leds[7][i].setHSV(color.h, color.s, color.v - 15);
pace = 360;
FastLED.show();
Serial.println("Dimming");
//Serial.println(color.v);
if (color.v - 25 <= 50 && i == 1) {
ticksExplode = 0;
burstStage = 2;
Serial.println(pace);
}
}
if(i > 0)
delay(1000/pace);
}
}
void prepareForBurst(int strip) {
Serial.println("Preparing for burst");
for (int i = 0; i < sizeof(leds[strip]); i++) {
CHSV color = rgb2hsv_approximate(leds[strip][i]);
leds[strip][i].setHSV(208, 50, color.v - 5);
Serial.println("Fading to dim white");
if (color.v - 5 <= 15)
burstStage = 2;
}
}
void explode(int strip, int probability){
Serial.println("EXPLODE");
for (int i = 0; i < sizeof(leds[strip]); i++) {
CHSV color = rgb2hsv_approximate(leds[strip][i]);
int flash = rand() % probability; // 1 out of 50
if (flash == 2) {
// we get it ready for a flash. Principle of anticipation.
//leds[strip][i].setRGB(44, 66, 127);
CRGB flash = CRGB(200, 200, 255);
fill_solid(leds[strip], sizeof(leds[strip]), flash);
//Serial.println("GETREADY");
}
// We check the state of that LED
if(leds[strip][i].red == 200 && leds[strip][i].blue == 255) { // If the LED just flashed a moment ago
leds[strip][i].setHSV(213, 100, 100); // We fade it to blue
} else if (leds[strip][i].blue <= 255) {
//if(rand()%10 == 1)
leds[strip][i].setHSV(213, 100, color.v - 1);
//leds[strip][i].setRGB(leds[strip][i].red - 40, leds[strip][i].green - 40, leds[strip][i].blue--); // Keep fading down
//Serial.println("KEEP FADING TO BLUE");
if(color.v <= 80) {
leds[strip][i].setHSV(213, 100, 80);
if(rand()%60 == 1){
if(color.v < 80 && color.v > 0){
if(rand()%2 == 1){
leds[strip][i].setHSV(213, 100, color.v - 1);
}
else{
leds[strip][i].setHSV(213, 100, color.v + 1);
}
}
}
}
}
lastStates[strip] = 2;
ticksExplode++;
if(ticksExplode >= 10000){
burstStage = 3;
}
}
FastLED.show();
}
/*
Function ligthningIdle. It calls lightning on all of the LED strips
Params:
*/
void lightningIdle() {
// Reset the timestamp
burstStage = 0;
// Here we call the lightning on all our LED Strips
lightning(0, 12, 200);
lightning(1, 18, 200);
lightning(2, 18, 200);
lightning(3, 16, 200);
lightning(4, 16, 200);
lightning(5, 10, 200);
lightning(6, 10, 200);
lightning(7, 24, 200);
/*
lightning(&B, NUM_LEDS_B);
lightning(&C NUM_LEDS_C);
lightning(&D, NUM_LEDS_D);
lightning(&E, NUM_LEDS_E);
lightning(&F, NUM_LEDS_F);
lightning(&G, NUM_LEDS_G);
lightning(&H, NUM_LEDS_H);
*/
delay(1000 / 30);
}
/*
Function incandescent. It calls incandescentYellow and Red depending on the strip
Params:
*/
void incandescent() {
burstStage = 0;
incandescentRed(0, 12);
incandescentYellow(1, 18);
incandescentRed(2, 18);
incandescentRed(3, 16);
incandescentRed(4, 16);
incandescentYellow(5, 10);
incandescentYellow(6, 10);
incandescentRed(7, 24);
delay(1000 / 120);
}
/*
Function ligthning. It calls the lightning animation on a single LED strip
Params: strip. It's the number of the LED strip. Meaning, we can call this function with any of the strips we have and it will work. * means it's a pointer to the first LED of the strip
maxlength. the size of the LED strip
*/
void lightning(int strip, int maxlength, int probability) {
// We loop through the LED strip
/*
BLUE: 80, 150, 220 - 130, 180, 230
GET READY: 44, 66, 127
FLASH: 255, 255, 255
*/
//fill_solid(leds[strip], maxlength, CRGB::White);
for (int i = 0; i < maxlength; i++) {
// Prepare random for a flash
if (leds[strip][i].blue == 0){
if(rand()%probability == 1)
leds[strip][i].setRGB(0, 0, 0);
}
CHSV color = rgb2hsv_approximate(leds[strip][i]);
int flash = rand() % probability; // 1 out of 50
if (flash == 2) {
// we get it ready for a flash. Principle of anticipation.
//leds[strip][i].setRGB(44, 66, 127);
leds[strip][i].setRGB(0, 0, 0);
//Serial.println("GETREADY");
}
// We check the state of that LED
if (leds[strip][i].red == 0 && leds[strip][i].blue == 0) { // If the LED is ready for a flash
leds[strip][i].setRGB(255, 255, 255); // We make it flash white
if (i + 1 < maxlength)
leds[strip][i + 1].setRGB(194, 132, 248);// And the next 2 LEDs too
if (i - 1 < maxlength)
leds[strip][i -1].setRGB(194, 132, 248); // And the next 2 LEDs too
} else if (leds[strip][i].red == 255 && leds[strip][i].blue == 255 || leds[strip][i].blue == 248 || burstToIdle == true) { // If the LED just flashed a moment ago
//Serial.println("FADE TO BLUE");
leds[strip][i].setHSV(190, 100, 100); // We fade it to blue
} else if (leds[strip][i].blue <= 255) {
//if(rand()%10 == 1)
leds[strip][i].setHSV(190, 100, color.v - 1);
//leds[strip][i].setRGB(leds[strip][i].red - 40, leds[strip][i].green - 40, leds[strip][i].blue--); // Keep fading down
//Serial.println("KEEP FADING TO BLUE");
if(color.v <= 80) {
leds[strip][i].setHSV(190, 100, 80);
if(rand()%60 == 1){
if(color.v < 80 && color.v > 0){
if(rand()%2 == 1){
leds[strip][i].setHSV(190, 100, color.v - 1);
}
else{
leds[strip][i].setHSV(190, 100, color.v + 1);
}
}
}
}
} else { // If it was in the incandescent state, transition into lightning by maybe getting ready
}
//FastLED.show();
//delay(1000/120);
}
lastStates[strip] = 1;
FastLED.show();
}
void incandescentYellow(int strip, int maxlength) {
/*
Colours:
- Bright Yellow: 255, 255, 0
- Dark orange: 255, 50, 0
- Bright orange: 255, 100, 0
*/
//fill_solid(leds[strip], maxlength, CRGB::Red);
// First, glow everything to bright yellow
for (int i = 0; i < maxlength; i++) {
if (leds[strip][i].blue > 0) {
leds[strip][i].setRGB(1, 30, 0);
}
if (leds[strip][i].red == 0) {
leds[strip][i].setRGB(1, 30, 0);
//Serial.println("Init");
} else if (leds[strip][i].red >= 1 && leds[strip][i].red < 255) {
Serial.println(leds[strip][i].red);
//Serial.println(color.hue);
leds[strip][i].setRGB(leds[strip][i].red + 1, leds[strip][i].green + 1, 0); // We add up a little bit of red and green light (yellow)
}
if (leds[strip][i].red >= 30) {
leds[strip][i].red = 30;
}
}
lastStates[strip] = 0;
FastLED.show();
}
void red(int strip) {
/*
Colours:
- Bright Yellow: 255, 255, 0
- Dark orange: 255, 50, 0
- Bright orange: 255, 100, 0
*/
//fill_solid(leds[strip], maxlength, CRGB::Red);
for (int i = 0; i < sizeof(leds[strip]); i++) {
leds[strip][i].setRGB(1, 30, 0);
}
FastLED.show();
lastStates[strip] = 0;
}
void incandescentRed(int strip, int maxlength) {
/*
Colours:
- Bright Yellow: 255, 255, 0
- Dark orange: 255, 50, 0
- Bright orange: 255, 100, 0
*/
//fill_solid(leds[strip], maxlength, CRGB::Red);
for (int i = 0; i < maxlength; i++) {
if (leds[strip][i].blue > 1) {
if(rand()%2 == 0){
leds[strip][i].setRGB(1, 30, 0);
}
else{
leds[strip][i].setRGB(1, 30, 1);
}
}
if (leds[strip][i].red == 0) {
if(rand()%4 == 0){
leds[strip][i].setRGB(1, 30, 1);
}
else{
leds[strip][i].setRGB(1, 30, 0);
}
//Serial.println("Init");
} else if (leds[strip][i].red >= 1 && leds[strip][i].red < 255) {
int red = leds[strip][i].red;Serial.println("blue");
red = red + 1;
if(leds[strip][i].blue == 1){
Serial.println("blue");
leds[strip][i].setRGB(red, leds[strip][i].green + 1, 1); // We add up a little bit of red and green light (yellow)
}
else{
//leds[strip][i].setRGB(red, leds[strip][i].green + 1, 0); // We add up a little bit of red and green light (yellow)
}
//
}
if (leds[strip][i].red >= 20) {
leds[strip][i].red = 20;
}
}
FastLED.show();
lastStates[strip] = 0;
}
void stillIncandescent(){
red(0);
incandescentYellow(1, 18);
red(2);
red(3);
red(4);
incandescentYellow(5, 10);
incandescentYellow(6, 10);
red(7);
delay(1000/30);
}
void turnOff(int strip){
fill_solid(leds[strip], sizeof(leds[strip]), CRGB::Black);
}
void off(){
for(int i = 0; i < MAX_STRIPS - 1; i++){
turnOff(i);
}
}
void loop() {
// He we have our Finite State Machine (the brain of the Stormbreaker where we will send our animation request using the potentiometer (knob)
// First we read the input from the knob, which is the voltage and we transform it to a state
time_stamp++;
lastState = STATE;
int val = analogRead(1);
if (val >= 0 && val <= 255) {
STATE = 0;
}
else if (val > 255 && val <= 512) {
STATE = 1;
}
else if (val > 512 && val <= 768) {
STATE = 2;
}
else if (val > 768 && val <= 1024) {
STATE = 3;
}
if(burstToIdle == true){
STATE = 3;
}
//lightningIdle();
// Then we run our state machine
switch (STATE) {
case 0:
burst();
Serial.println("burst");
break;
case 1:
stillIncandescent();
Serial.println("STILL INCANDESCENT");
break;
case 2:
incandescent();
Serial.println("INCANDESCENT");
break;
case 3:
lightningIdle();
Serial.println("LIGHTNING");
break;
}
}