HCSR04 really slow in App Inventor

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
from
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)
{
  alertas(distancia);
}
else if (distancia > lejos)
{
  digitalWrite (led, LOW);
  bt.print("d");
  delay(400);
}
   if (distancia2 < lejos)
{
  cuidado(distancia2);
}
else if (distancia2 > lejos)
{
  digitalWrite (led, LOW);
  delay(400);
  bt.print("h");
} 

}
void alertas(float distancia) {
if (distancia < lejos && distancia >= medio) {
bt.print("a");
delay(400);
parpadeo(1000);
}
else if (distancia < medio && distancia > cerca) {
bt.print("b");
delay(400);
parpadeo(500);
}
else if (distancia <= cerca) {
bt.print("c");
delay(400);
parpadeo(250);
}
}

void parpadeo(int tiempo) {
digitalWrite(led, HIGH); // enciende el LED.
delay(tiempo); // retardo en milisegundos
digitalWrite(led, LOW); // apaga el LED.
delay(tiempo);
}
void cuidado(float distancia2) {
if (distancia2 < lejos && distancia2 >= medio) {
bt.print("e");
delay(400);
parpadeo2(1000);
}
else if (distancia2 < medio && distancia > cerca) {
bt.print("f");
delay(400);
parpadeo2(500);
}
else if (distancia2 <= cerca) {
bt.print("g");
delay(400);
parpadeo2(250);
}
}

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

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.
export_and_upload_aia

Yes.

A global variable is easier for debugging at this point.

i cant post it haha, network problem, sorry
hcsr04 (1).aia (243.7 KB)

can u help me? pls

hcsr04 (1).aia (274.0 KB)


I also had to fix
Delimiter Byte

I tried to test the application as it is and it tells me that it does not respond, it does not load, do you know why?

i fix the problem of the doesnt respond but the app still doesn't work, maybe it's my arduino code.
Now the app simply doesn't do anything, it doesn't change the image, it doesn't play sound and nothing

void loop()
{

long distancia = ultrasonic.read();
long distancia2 = ultrasonic2.read();
if (distancia < lejos)
{
  alertas(distancia);
}
else if (distancia > lejos)
{
  digitalWrite (led, LOW);
  bt.println("d");
  delay(400);
}
   if (distancia2 < lejos)
{
  cuidado(distancia2);
}
else if (distancia2 > lejos)
{
  digitalWrite (led, LOW);
  delay(400);
  bt.println("h");
} 

}
void alertas(float distancia) {
if (distancia < lejos && distancia >= medio) {
bt.println("a");
delay(400);
parpadeo(1000);
}
else if (distancia < medio && distancia > cerca) {
bt.println("b");
delay(400);
parpadeo(500);
}
else if (distancia <= cerca) {
bt.println("c");
delay(400);
parpadeo(250);
}
}

void parpadeo(int tiempo) {
digitalWrite(led, HIGH); // enciende el LED.
delay(tiempo); // retardo en milisegundos
digitalWrite(led, LOW); // apaga el LED.
delay(tiempo);
}
void cuidado(float distancia2) {
if (distancia2 < lejos && distancia2 >= medio) {
bt.println("e");
delay(400);
parpadeo2(1000);
}
else if (distancia2 < medio && distancia > cerca) {
bt.println("f");
delay(400);
parpadeo2(500);
}
else if (distancia2 <= cerca) {
bt.println("g");
delay(400);
parpadeo2(250);
}
}

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

Add a final ELSE to the AI2 if/then/else to display anything that the preceding branches missed in a Label.

Also, is Switch1 On?

I notice you lack branches to handle disconnection.

See FAQ Section: BlueTooth Starter Guides

I dont get it :frowning: