Hi there!
First off, I will put it out there that I am very new to programming in general and need some help being pointed in the right direction with my project.
Putting it simply, I am trying to build a Bluetooth speaker with addressable LEDs inside that can be controlled via a Bluetooth app.
My idea was to have each FastLED pattern (lighting pre-set) stored as a function that can be called using a Switch Case. For example, if the app sent the number 1 over serial Bluetooth then the microcontroller would read this number and play the case associated with the number one on loop until another value was given.
My problem is that after every cycle of the loop void my integer that gets its value from the serial input returns to 0 no matter what, meaning that each lighting preset only plays once.
I thought I could get around this by calling the same function again within the function itself, but something tells me that this is a big no no as that just creates an infinite loop that cant be broken.
For this project I am using an ESP32 connected to 144 WS2812b leds on a strip and am using the FastLED library to write the lighting patterns.
Here is a little test project I made just to try and get my head around making these presets loop and to give you an idea where I my brain was going with this. (I know its not using Bluetooth, I have just been using the serial monitor to input values to keep things simple until I can overcome this hurdle.)
#include <FastLED.h>
#define NUM_LEDS 144
#define LED_PIN 15
CRGB leds[NUM_LEDS];
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(50);
fill_solid(leds, NUM_LEDS, CRGB::Black); //This was just to make sure all the Leds are cleared (there is propably a better way to do so)
FastLED.show();
}
void loop() {
int val = Serial.parseInt(); //Reads incoming number from serial monitor and sets 'val' accordingly.
switch (val) //Switch Case used to switch between different fastLED presets depending on what number is received in the serial monitor.
case 1:{
RedBlink();
break;
}
}
void RedBlink() {
fill_solid(leds, NUM_LEDS, CRGB::Red); //Turn all Leds on to Red
FastLED.show();
delay(100);
fill_solid(leds, NUM_LEDS, CRGB::Black); //Turn all Leds off
FastLED.show();
delay(100);
int val = Serial.parseInt(); //read serial monitor and set val accordingly
bool NewData; //create new boolean
if (val==1){
bool NewData = false; //if the serial monitor is still = to 1 then no new input has been received, set NewData bool to flase.
}
else if (val!=1) {
bool NewData = true; //if the serial monitor does not = 1 then a new input has been received, set NewData bool to true.
}
if (NewData == false){ //if NewData is flase then start the funtion from the top (looping the FastLed effect over and over until a new input is received)
RedBlink();
}
else if (NewData == true) { // if NewData is true then a new input has been received so break the loop
}
}
Please could someone point me in the right direction of how to store multiple FastLED lighting presets that can be called on loop until a new preset is called using the Serial Monitor.
I am really struggling to find a way around it as my knowledge on the matter isn't great enough and feel like I am trying to solve this problem in the completely wrong way. If there are much better ways to do this as I'm sure its been done many times before then please let me know.
I hope that I have given enough detail on the project at hand and thank you for any help in advance!