I think there are people here who are also familiar with Arduino.
I have an Arduino Nano, an Bluetooth module HC-06 and a LED Stripe WS2812b.
I want to make an animation lamp.
It all works perfectly at the Arduino Ide, with the serial monitor I can switch case between all different animations if I enter a, b, c, d or 1, 2, 3 and so forth.
If I do this with an App (created with MIT App Inventor) then works it not dependable every time. Sometimes I have to push the buttons for the respective animations mutiple times. Sometimes the animation stops before it works properly. In some cases I have to push the buttons 4 times.
I don't guess that is up to the bluetooth connection, I see that the LED from the HC-06 every time shows reaction at button pushes. So I think it could be due to the app, but my knowledge is not enough.
I hope there is anybody which have a deeper insight.
Maybe there is a possibility to solve this problem? Lichtrohr_test.aia (2.7 KB)
Hallo @ber63,
I guess you don't need to cehck periodically (each 200 ms) whether the BT is connected or not. Try to remove the clock1 and just leave the buttons.
Thank you for the quickly help.
I tried your recommend. It works in principle... without the clock too. But it changes nothing on the behavior. The switch between the animations is still unreliable. Besides that I tried this:
One could think it is up to the animations in the Arduino *.ino file, which have implement for loops. It is generally known that these cannot be easily interrupted. But it works with the serial monitor.
However, here you have to set "no end of line".
There are other suggestions?
Thanks. Bye for now
Hallo @ber63,
without looking to your ino file, what I can tell you "theoretically" is:
are you using the same serial line for both the BT and the Serial Monitor ? Normally I separate them: z.b. for a UNO board, I use for the Serial Monitor the HW serial, while for the BT the SoftwareSerial library (on pins 10,11). In other posts of the community there are many examples (mainly from @JuanAntonio and @PatrickF). Or as an alternative, disable (i.e.comment out //) all the lines where in your Arduino code the communication (both Rx and Tx) are invoked toward the Serial Monitor. Sometimes there is a conflict between the BT coming from outside (your App, in this case) and the SerialMonitor itself.
If I test it with the serial monitor is the Arduino Nano connected with the PC via USB.
If I test it with the bluetooth than I separate the USB cabel. I also delete the line Serial.begin from the ino file.
To upload a code respectively an ino-file is forbidden, is a txt-file from this is practically the same. Or is this allowed? I try a picture....
Hallo @ber63,
honestly your chunk of code you attached does not explains too much because
the loop function is truncated. I see that you begins a switch case, but I don't see the rest...
Anyway, you have removed everything for the HW serial and it still does not work (if I correctly undertand), so it's something else.
Let's test it in this way:
Since you use the BT communication with SoftwareSerial on pins 2,3 (another hint could be to use it with pins 9,10) restore the HW serial and use it as a debugger.
So, put a loop to wait for any incoming character, then when a character is ready, read it and display what character is arrived in the serial monitor.
In other words:
while (!myBluetooth.available()) delayMicroseconds(100);
anyNumber = myBluetooth.Read();
Serial.print(anyNumber);
Note that the delayMicroseconds stops the Arduino code but it releases the interrupt to happen (i.e. the BT communication) every 100 microseconds, in this case. To make it better we could use the millis() but to make it "quick" we can start with this approach. Another thing: there isn't a timeout, so if the BT never receives, the code stucks in the while forever. Tis is not a good programming, but it's just to debug
Thank you Ugo and Juan.
I had already tried it (SoftwareSerial pins 10, 11).
When i was working on it in the spring (i took a break in the summer) .... I remember it was difficult to get out of the loops. When it then worked via serial monitor I was happy. But via bt there are again difficulties.
Here is the rest of the code.
@ Juan_Antonio
I have also tested this structure beforehand, it works perfectly.
I remember, when I have tested my animations many months ago with hardware buttons. There was difficulties to braek the loops. Hence my assumption that it has something to do with the animations of the LEDs.
Then I set it up so that it worked perfectly with HW serial, the animations switch cleanly immediately after sending the corresponding number or letter. But with HC-06 Bluetooth it doesn't switch over cleanly every time. Sometimes the leds standing still and it only switches when I click again or I have to click multiple time in the worst case.
I will follow next week the recommend to debugging of Ugo. Maybe there is something to recognize.
So if you change these lines, does it work for you with Serial Monitor?
if(Serial.available())
{
anyNumber = Serial.read();
}
Another thing you can try is to put a Serial.println (anyNumber);
in the lines below each case, to check the arrival of the data.
case '1'
Serial.println(anyNumber);
case '2'
Serial.println(anyNumber);
...
It can also happen that some 'voids' take time to be fully processed and during that time another change request arrives, but it is strange that it works for you with the Serial Monitor.
Yep !
Serial.println(anyNumber); is the same that I told ber63 in my previous post
Typical debug means..."oldie and goodie"
Have a great Christmas to all !!
Cheers, Ugo.
Hi @Juan_Antonio,
yep, I understand, but in this case he cannot see nothing if the characters are out of those trapped by the "case" instructions, while, on the contrary, if he puts the printout on any character, then he can understand what it's really arriving. Maybe there is a mismatch in the baudrate of the BT and the incoming characters are not decoded correctly by the UART of Arduino.
(exactly what happened me yesterday I had a HC05 labelled 115K baudrate while effectively it was set to 9600 ... , so just putting a printout I saw "strange" characters and I detected the wrong baudrate).
Anyway I'm with you: our aim is to help
the baudrate of the hc-06 ist 9600, everything is correct there. but I also suspect that the characters coming in piece by piece.
I have mainly to do with my aquarium this week. When I was done I try out your suggestions.
have a nice christmas to you
I hope you both are still my partners in this game after that.
I've tried that now. (incidentally....(aniNumber) is not a typo....it means ani for animation )
Back to troubleshooting: the display in the serial monitor reflects exactly what can ultimately be seen on the LED strip. What is noticeable is that the character sent continues to appear (probably every millisecond) in new lines in the serial monitor. You can see that because the bottom line flickers. I can imagine that this is why the transmissions only find a "gap" sporadically. If it sometimes happens that the LED animation "frosen", than the serial monitor display standing still too. Only when you click again or several times will the new character be transferred and switch to the desired animation and the incoming number is running until the end of time.
The behavior with the control about the serial monitor and USB is as I said completely different, the character will be transmitted only once and the animation changes, immediately, regardless of where the animation loop is in the moment.
I have read that it is important that one set "no end of line" in the serial monitor. Maybe this is the way... program the app to do that too if this is possible. But maybe it's also a hardware-related behavior.
I will continue to research and look forward to your support.
Hallo @ber63,
I don't remember whether you have posted the complete loop() function of your Arduino code, but as a suggestion, if I have correctly understood what you have written in your last post, it seems that you receive too many characters and this is why only seldom a character from the BT seems to be capable to enter. Then what normally I do in fast loop() is to put a 100 ms delay after having received one character, so to enable to buffer to be flushed.
something like:
void loop()
{
if Serial.Available()
{
carinp = Serial.Read();
switch(carinp)
{
case '1':
something_to_do();
break;
case '2':
something_to_do();
break;
default:
something_ to_do();
}
delay(100);
}
else delay(100);
}
This trick avoids that the same characater is read 3 or 4 times before it's flushed out the buffer.
Hope it helps.
Cheers, Ugo.
(code escape added for readability - ABG)
No, the animations don't work with a delay.
If I put the curly braces like in your example it doesn't work anymore.
I found out half a year ago that it doesn't work like that, but I forgot it.
It must be written this way:
Hallo @ber63,
one moment !!!! Sorry but when I wrote Serial.Available I was intending myBluetooth.Available()
If it works only if you remove the curly braces, this means that your loop() is not waiting far a character from the Serial line and processes always an old character, irrespective of what is received, or not, on the serial line !!!
Anyway, if restoring the correct istruction myBluetooth.Available() and by putting the curly braces it does not work, this means that the Arduino board is never receiving from the BT any character.
Let's try differently: the annexed .aia is an app that I developed few days ago.
It drives an Arduino (Nano, but it is irrelevant for your purposes) fitted with a BT HC05 to manage an RGB LED's strip on my car.
The app sends several "one character" commands via BT to my Arduino board that drives the RGB's.
The Arduino board does not send back any feedback to the app, but it just receives. Most probably this is sufficient also for your needs. The app also sends periodically a "live" character to the HC05, just to avoid the the BT disconnects itself if not solicitated for a long time.
The "true commands" are sent only whenever a button is pressed (the app runs on my phone).
In the .aia file, it is also included the .ino program that you can find in the "asset" directory (you open the .aia with winzip or 7zip so you can extract the .ino).
Please don't mind if the comments are in Italian. The code is quite simple, but if you need an english translation, please let me know.
Hoping this truly helps you RGB_BT.aia (352.2 KB)
viel Glück
PS in the app you will see that there are two close app paths: one is suitable for Android versions capable to handle the android.bluetooth.adapter.action.REQUEST_DISABLE activity, and the other path for those older Android versions not featuring this activity.
And, if the Android version is one of those where the activity is executed, in order to leave time do accomplish the activity, before affectively closing the app, I start a timer that allows some seconds before closing really the app, otherwise the activity is not done, because the app closes too early.
That's it.
it all works perfectly when controlled via the serial monitor.
BUT....
If I use this code ( with #include <SoftwareSerial.h> and myBluetooth.available and so on) and I want to control the stripe via app and bluetooth..... it works in principle too, but I have too push the buttons for the animations (1 to 9) sometimes multiple times. I could live with it, but if I wanted to give the lamp away as a gift, I would be embarrassed.