Go Down

Topic: Change variable value at loop (Read 195 times) previous topic - next topic

facugu

Jan 24, 2021, 05:09 am Last Edit: Jan 24, 2021, 05:24 am by facugu
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:

Code: [Select]
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.

IoT_hobbyist

You need to put ; (semicolon) after function call. change  handleMusic() to handleMusic();
- How to get started with Arduino: https://arduinogetstarted.com
- Arduino Tutorials For Beginners: https://arduinogetstarted.com/arduino-tutorials
- Arduino FAQ: https://arduinogetstarted.com/arduino-faq

drmpf

Try
Code: [Select]
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
    }
  }
}



sterretje

#3
Jan 24, 2021, 06:46 am Last Edit: Jan 24, 2021, 06:46 am by sterretje
@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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

sterretje

#4
Jan 24, 2021, 07:00 am Last Edit: Jan 24, 2021, 07:03 am by sterretje
@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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

facugu

#5
Jan 24, 2021, 11:49 pm Last Edit: Jan 24, 2021, 11:51 pm by facugu
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.

sterretje

Quote
Edit. I can share the code if you want, but maybe is a bit messy.
That sounds like a good idea ;)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

facugu

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

drmpf

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

Code: [Select]
 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

facugu

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!

Go Up