HCSR04 really slow in App Inventor

I have a problem in my app, I’m trying to change an image and reproduce a sound when the bt read a letter sent by the HC05, I have 3 modes for the distance of my sensor and each one sent a different letter.

My problem it’s that the time that the app do the activity programmed is really really slow, how can it change in real time.

This is my code

I have the polling polling rate in 10
Byte limitator in 0

And the time Interval for the clocks are 1

You have 7 Clocks?

You only need 1.

Expand that IF block where you check the incoming letter, adding ELSEIF branches for each other incoming letter and their corresponding action.

Use the Player component instead of the Sound component, which lacks parallelism.

ive already used the player component and i tried with just 1 clock and it works worse

Show your new blocks by right clicking and selecting Download Blocks as PNG. There is a way to do this.


i put all in this block and i tested it, it didnt work, didnt do anything :confused:

I even see in a bt serial app if it receives the letters correctly and there is no problem, apparently the problem is in my app

You are asking to receive ALL the available bytes of text in the buffer.
If there are more than one byte, the resulting test will fail all tests against single letters.

If all your codes are going to be single letters, only ask for 1 byte at a time in your ReceiveText blocks.

Also, you should have a global variable to catch the received text, and do the Receive Text only once, at the top of your Clock Timer after the connection and available bytes tests.
Once you have received a byte into that global variable, do all your testing against that variable.

Doing multiple ReceiveTexts discards previously received text, depriving subsequent tests of access to what failed the previous test.

1 Like

My code of arduino sent 2 letters at the same time, 1 for each sensors, and how can I do that :sweat_smile:
How can I use the global variable, do u have any tuto?

These are sample blocks for a slightly different case:

Here is an updated blocks sample illustrating these ideas ...

BlueTooth_delimiter_sample.aia (3.4 KB) global message

This case was built for multiple items per message, with Line Feed (10) at the end, and ',' between items in the message.

I’m gonna try rn, thx

Alse read http://www.appinventor.org/bookChapters/chapter16.pdf
FAQ Section: Books, Tips, Tutorials for AI2

And do you know then how I should modify my arduino code? I don't separate the letters for anything, I only use the print option for each case

Post the code?

\n is line feed.

if (distancia < lejos)
else if (distancia > lejos)
  digitalWrite (led, LOW);
   if (distancia2 < lejos)
else if (distancia2 > lejos)
  digitalWrite (led, LOW);

void alertas(float distancia) {
if (distancia < lejos && distancia >= medio) {
else if (distancia < medio && distancia > cerca) {
else if (distancia <= cerca) {

void parpadeo(int tiempo) {
digitalWrite(led, HIGH); // enciende el LED.
delay(tiempo); // retardo en milisegundos
digitalWrite(led, LOW); // apaga el LED.
void cuidado(float distancia2) {
if (distancia2 < lejos && distancia2 >= medio) {
else if (distancia2 < medio && distancia > cerca) {
else if (distancia2 <= cerca) {

void parpadeo2(int tiempo2) {
digitalWrite(led, HIGH); // enciende el LED.
delay(tiempo2); // retardo en milisegundos
digitalWrite(led, LOW); // apaga el LED.

I see you use print().

If you use println() instead, the codes will are one at a time.

do i have tu use ","?

Ive changed the pirintln, so now i have to add the local variable and try to work with that? :confused:

Export your .aia file and upload it here.


A global variable is easier for debugging at this point.