Change variable value at loop

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.

Thanks in advice.

You need to put ; (semicolon) after function call. change handleMusic() to handleMusic();

Try

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
    }
  }
}

@facugu

Please don't edit your opening post after comments are made. Reply #1 now does not seem to make sense.

Rather post the fixed code in a new reply.

@facugu

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.

Edit. I can share the code if you want, but maybe is a bit messy.

That sounds like a good idea :wink:

Well, I actually have to refactor lot of things because I've been updating it. I attached the code files.

Nodes.h (9.78 KB)

mixed.ino (11.2 KB)

A quick look at the mixed.ino shows you still have

  if (music) {
    ...
  } else {
    if (miBT.available()) {      // si hay informacion disponible desde modulo
  ...

So if music is true, miBT.available() does not get called.
which may be some of you problem

Yes, maybe I undone that change in some moment. However when I changed according to your solution it still worked incorrectly.

I think the problem is not there because I'm trying to go from "Other mode" to "Music mode" and not reversed.

Thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.