need block forprogramming with arduino uno,hc05 ,hcs04

40 views
Skip to first unread message

Abdullah Alvi

unread,
Mar 25, 2019, 3:00:53 PM3/25/19
to MIT App Inventor Forum
someone please help me in making this arudino un0, hc05 and ultasonic distance sensor to show distance and red ,orange , green bars. thanks in advance

int trig_pin1 = 12;
int echo_pin1 = 13;
int trig_pin2 = 10;
int echo_pin2 = 11;
int trig_pin3 = 8;
int echo_pin3 = 9;
long echotime1;
long echotime2;
long echotime3;//in micro seconds
float distance1;
float distance2;
float distance3;//in cm

void setup() {
  Serial.begin (9600);
  pinMode(trig_pin1, OUTPUT); 
  pinMode(echo_pin1, INPUT);
  digitalWrite(trig_pin1, LOW); 

  pinMode(trig_pin2, OUTPUT); 
  pinMode(echo_pin2, INPUT);
  digitalWrite(trig_pin2, LOW);

  pinMode(trig_pin3, OUTPUT); 
  pinMode(echo_pin3, INPUT);
  digitalWrite(trig_pin3, LOW);

}


void loop() {

  digitalWrite(trig_pin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin1, LOW);
  delayMicroseconds(2);

  //trigger a pulse-echo measurement
  digitalWrite(trig_pin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin2, LOW);
  delayMicroseconds(2);

  //trigger a pulse-echo measurement
  digitalWrite(trig_pin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin3, LOW);
  delayMicroseconds(2);
  
  //get the result
  echotime1= pulseIn(echo_pin1, HIGH);
  distance1= (echotime1*0.034)/2;
  
  echotime2= pulseIn(echo_pin2, HIGH);
  distance2= (echotime2*0.034)/2;

   echotime3= pulseIn(echo_pin3, HIGH);
  distance3= (echotime3*0.034)/2;
  
  //send over Bluetooth 
// left
  Serial.print("T"+String(echotime2)+"");
  Serial.print("D"+String(distance2,1)+"");
  if (distance2<30) Serial.print("LR255G0B0"); //Red
  if (distance2>=30&&distance2<=100) Serial.print("LR255G200B0"); //Orange
  if (distance2>100) Serial.print("LR0G255B0"); //Green

//right
  Serial.print("T"+String(echotime3)+"");
  Serial.print("D"+String(distance3,1)+"");
  if (distance3<30) Serial.print("LR255G0B0"); //Red
  if (distance3>=30&&distance3<=100) Serial.print("LR255G200B0"); //Orange
  if (distance3>100) Serial.print("LR0G255B0"); //Green

//back
  Serial.print("T"+String(echotime1)+"");
  Serial.print("D"+String(distance1,1)+"");
  if (distance1<30) Serial.print("LR255G0B0"); //Red
  if (distance1>=30&&distance1<=100) Serial.print("LR255G200B0"); //Orange
  if (distance1>100) Serial.print("LR0G255B0"); //Green
  
  delay(1000);
}

ABG

unread,
Mar 26, 2019, 8:27:24 AM3/26/19
to MIT App Inventor Forum
Post your AI2 blocks.

In the meantime,

Use println() to separate messages when you transmit.
Set Delimiter = 10 in BlueTooth Designer.
Check for Bytes Available > 0 in blocks,
but then request -1 bytes to get only full  messages.

Also, to get colors in Ai2, do the decision on AI2,
not on the Arduino.

If distance < 30 then color =  ...
else if distance < 100 then color = ...
else color = ...
end if

Also see BlueTooth and Arduino section of FAQ
ABG



Abdullah Alvi

unread,
Mar 28, 2019, 12:18:03 PM3/28/19
to MIT App Inventor Forum
Dear these are my blocks

Capture.PNG


Abdullah Alvi

unread,
Mar 28, 2019, 12:27:17 PM3/28/19
to MIT App Inventor Forum

this is my main screen interfaceCapture.PNG


ABG

unread,
Mar 28, 2019, 12:33:45 PM3/28/19
to MIT App Inventor Forum
See the Debugging section of FAQ
for how to debug using Do It.

ABG

Abdullah Alvi

unread,
Mar 28, 2019, 2:44:26 PM3/28/19
to MIT App Inventor Forum
sir waiting for humble reply 

ABG

unread,
Mar 28, 2019, 2:44:34 PM3/28/19
to MIT App Inventor Forum
In this type of app, it is important for the person
who wrote the Arduino code to read  the AI2 blocks,
and for the person who wrote the AI2 code to read the Arduino code.

This did not happen here.

The 2 programs differ in how they think the messages should be formatted
and how to deal with the formatting.

The AI2 blocks do not check the messages to see which message is carrying the distance.
They also look for distance values next to the Red color,
which does not even need to be sent if the color choice logic is in AI2.

Also, Delimiter advice has been ignored.

Also, no attempt at use of the Companion has been made to see the incoming data stream.

ABG



Abdullah Alvi

unread,
Mar 28, 2019, 2:48:31 PM3/28/19
to MIT App Inventor Forum
will you please do a little effort to correct this code or make application that could help me.
thanks in advance

ABG

unread,
Mar 28, 2019, 2:52:37 PM3/28/19
to MIT App Inventor Forum
Pardon me, I must go do something more productive at this time.

I relinquish this thread to any one with more patience.

ABG

Ghica

unread,
Mar 29, 2019, 10:51:54 AM3/29/19
to MIT App Inventor Forum
We try to help you to solve your problems yourself, we are not here to write your app for you, which would be anyway impossible because we do not have the same hardware.
ABG gave you plenty of tips how you could improve your code. If you have a problem doing so, the ask a specific question on your specific problem.
Cheers, Ghica.
Reply all
Reply to author
Forward
0 new messages