HCSR04 really slow in App Inventor

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:

yeah switch 1 is on

its a hc05 bt module

Try changing that -1 in the ReceiveText block to +1, to force read single characters.

2 Likes

now works, thanks a loooot

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