Open
Description
I have used a variation of your sketch for a bluetooth-enabled cloudlamp and it works magnificently. Am now attempting to modify it for a variant that uses a pushbutton to switch between modes in case switch. The other patterns seem to work fine but constant_lightning is randomly freezing in Thunderburst at FastLED.show (used several Serial.prints to pinpoint the freeze location). Sometimes it happens the first time it hits TB, sometimes it will run a few times. As I said, it works fine in my bluetooth sketch and run it where constant_lighting is called from void loop where it will run for hours at a time. Any suggestions on what might be causing the freeze? Below is a simplified version of my code I'm using to troubleshoot. Thanks!
/*
Lighting Cloud Mood Lamp By James Bruce
View the full tutorial and build guide at http://www.makeuseof.com/
Sound sampling code originally by Adafruit Industries. Distributed under the BSD license.
This paragraph must be included in any redistribution.
*/
#include "FastLED.h"
// constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 8; // the number of the pushbutton pin1
#define NUM_LEDS 20
#define DATA_PIN 5
#define BRIGHTNESS 175
CRGB leds[NUM_LEDS]; // Define the array of leds
#define FRAMES_PER_SECOND 120
// variables will change:
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
void setup() {
// this line sets the LED strip type - refer fastLED documeantion for more details https://github.com/FastLED/FastLED
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
Serial.begin(9600); // initialize serial communication:
randomSeed(analogRead(0));
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
}
void loop() {
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState1 to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter1++;
Serial.print("Button Pushed...MODE = ");
Serial.println(buttonPushCounter1);
}
delay(50); // Delay a little bit to avoid bouncing
}
// save the current state as the last state, for next time through the loop
lastButtonState1 = buttonState1;
if (buttonPushCounter1 > 2) {
buttonPushCounter1 = 0;
}
switch (buttonPushCounter1) {
case 1:
constant_lightning();
break;
case 2:
reset(); break;
}
}
// basically just a debug mode to show off the lightning in all its glory, no sound reactivity.
void constant_lightning() {
switch (random(1, 10)) {
case 1:
thunderburst();
delay(random(10, 500));
break;
case 2:
rolling();
break;
case 3:
crack();
delay(random(50, 250));
break;
}
}
void rolling() {
// a simple method where we go through every LED with 1/10 chance
// of being turned on, up to 10 times, with a random delay wbetween each time
for (int r = 0; r < random(2, 10); r++) {
//iterate through every LED
for (int i = 0; i < NUM_LEDS; i++) {
if (random(0, 100) > 90) {
leds[i] = CHSV( 0, 0, 255);
}
else {
//dont need reset as we're blacking out other LEDs here
leds[i] = CHSV(0, 0, 0);
}
}
FastLED.show();
delay(random(5, 100));
reset();
}
}
void crack() {
//turn everything white briefly
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV( 0, 0, 255);
}
FastLED.show();
delay(random(10, 100));
reset();
}
void thunderburst() {
// this thunder works by lighting two random lengths
// of the strand from 10-20 pixels.
int rs1 = random(0, NUM_LEDS / 2);
int rl1 = random(10, 20);
int rs2 = random(rs1 + rl1, NUM_LEDS);
int rl2 = random(10, 20);
//repeat this chosen strands a few times, adds a bit of realism
for (int r = 0; r < random(3, 6); r++) {
for (int i = 0; i < rl1; i++) {
leds[i + rs1] = CHSV( 0, 0, 255);
}
if (rs2 + rl2 < NUM_LEDS) {
for (int i = 0; i < rl2; i++) {
leds[i + rs2] = CHSV( 0, 0, 255);
}
}
FastLED.show();
//stay illuminated for a set time
delay(random(10, 250)); //50
reset();
delay(random(10, 1000)); //50
}
}
void reset() // utility function to turn all the lights off.
{
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV( 0, 0, 0);
}
FastLED.show();
}
Metadata
Metadata
Assignees
Labels
No labels