Hello, I'm building a project using arduino, ws2812b led strips and hc05 bluetooth. The idea is to control the leds with different modes depending on the data received from a mobile phone.
I have two modes and a global variable that defines which mode is running, for example:
bool music = false;
void setup(){
//setup
}
void loop() {
someStuff();
if(music){
handleMusic();
}else{
if (miBT.available()){
String r = miBT.readString();
if(r.charAt(0) == 'M'){
music = true;
}else{
handleOtherModes(r);
}
}
}
}
The problem is when I set music = true, the code not works, it seems that is not entering the loop. However, if I remove that line, the code works correctly and the other modes work fine. Also if I set music = true at its definition, the music mode runs correctly.
I don't know if I'm forgetting something about setting global variables in loop or something. The code is a pseudocode, because the original is pretty long.
bool music = false;
void setup() {
//setup
}
void loop() {
someStuff();
if (music) {
handleMusic();
}
// always look for cmds
if (miBT.available()) {
String r = miBT.readString();
if (r.charAt(0) == 'M') {
music = true;
} else {
setOtherModes(r); // just set the mod here
}
}
}
What are the commands that you send? Just single letters? Maybe you're receiving a line ending as well ('\r' and/or '\n').
What are the mysterious functions? Do they block?
Once you are in music mode, you will never be able to change; @drmpf provided a solution for that.
Please be aware that neopixel libraries disable interrupts while updating the the strip. There is a good chance that you will loose Bluetooth data if you send more than one character at a time.
I'm sending letters and a number, for example, if I send M5 is music, if I send M1 ... M4 are other modes, if I send Bx the brightness is set to x.
The thing is that I used an existing project that controls M1 to M4 modes, using classes and functions with classes, and I wanted to add a music reactive project, so I mixed them.
When I change in the code the value of the "music" variable to true or false, all the modes work correctly. But when I try to change it at the loop, at else section, it doesn't enter the loop, or look like that.
I changed the code based on @drmpf solution, but I still have the same problem.
Edit. I can share the code if you want, but maybe is a bit messy.