Error while showing ultrasonic distance sensor value in ai2

47 views
Skip to first unread message

Abdullah Alvi

unread,
Apr 3, 2019, 6:08:33 AM4/3/19
to MIT App Inventor Forum
i am trying to show values of ultrasonic distance sensor in ai2,
i have made alot of efforts according to my little knowledge ai2, arduino code and error are attached please help me to remove these,
i shall be very thankful to you 
trythisAPP (1).aia
trythiss.docx
651dfcb7-b56e-41af-9593-9d93d27a90f2.jpg
1d82ab28-adb3-4ac9-b20c-b0761b96e9c3.jpg

TimAI2

unread,
Apr 3, 2019, 7:18:59 AM4/3/19
to MIT App Inventor Forum
It could be one or many of many things !!

You check if a result is within range within your arduino code, then you check it again in your blocks code ? Should only need to do it once.
This can also cause an error because AI2 is expecting a number (for the comparison) and it will get "out of range" instead ?
I am certain you do not need 4 clock timers to access this data. Get all the data (FR,BK,LT,RT) into one string on your arduino and send that
sort it out in AI2 when all the data arrives
Your first clock fires every 100ms the other three fire every 1000ms  ??

I do not have the equipment to test any of this.

Your blocks below to save others having to download and load up your aia file:

blocksDistanceSensor.png


Chris Ward

unread,
Apr 3, 2019, 7:31:46 AM4/3/19
to MIT App Inventor Forum
Hello Abdullah

So, the error messages are from the App. This is probably because the App is not receiving what you expect it to receive. I think the method of gathering data and sending it to the App could be better.

Your Sketch code checks for an out of range value, so why are you also doing that in the App Blocks code?

Your App code is using Clock Timers for each set of data it expects to receive (Front, Back, Left, Right). This is impractical because it is very difficult to synchronise data send, data receive and data process.


Chris Ward

unread,
Apr 3, 2019, 9:03:04 AM4/3/19
to MIT App Inventor Forum
Hi again Abdullah

I have an hour spare time this afternoon ( 15:00 UK time) so I will put together example code for you.

Chris Ward

unread,
Apr 3, 2019, 10:05:09 AM4/3/19
to mitappinv...@googlegroups.com
Right then - just simplified everything as much as possible and suggest that it is kept that way.

Note, the Arduino code I have put together is not tested, I do not have your hardware setup and I am trusting your collection of data from the sensors method. If that does not work, nothing else will work.

The App Inventor Blocks I have used are largely from a Project that is working but there is no guarantee that I have not made mistakes.  I have simplified your GUI a little bit, so that values and messages can be displayed before the receipt of the next packet of data. You will no doubt need to tweak the timings.

Blocks:

UltrasonicDistanceBlocks.png



Arduino code:


#include "NewPing.h"


#define TRIGGER_PIN12 12

#define ECHO_PIN13 13

#define MAX_DISTANCE 366 // 366 IS 12FT


#define TRIGGER_PIN10 10

#define ECHO_PIN11 11


#define TRIGGER_PIN8 8

#define ECHO_PIN9 9


#define TRIGGER_PIN7 7

#define ECHO_PIN6 6


NewPing sonar(TRIGGER_PIN12, ECHO_PIN13, MAX_DISTANCE);


NewPing sonar1(TRIGGER_PIN10, ECHO_PIN11, MAX_DISTANCE);


NewPing sonar2(TRIGGER_PIN8, ECHO_PIN9, MAX_DISTANCE);


NewPing sonar3(TRIGGER_PIN7, ECHO_PIN6, MAX_DISTANCE);


float fgDurationFront;

float fgDurationBack;  

float fgDurationRight; 

float fgDurationLeft;  


float fgFront;

float fgBack;

float fgRight;

float fgLeft;


int iIterations = 5;


void setup() {


               Serial.begin(9600);

}


void loop() {


               fgDurationFront =  sonar.ping_median(iIterations);

                fgDurationBack = sonar1.ping_median(iIterations);

               fgDurationRight = sonar2.ping_median(iIterations);

                fgDurationLeft = sonar3.ping_median(iIterations);

               

               // determin distance from duration

               // 343 metres per second is speed of sound

               

               fgFront = (fgDurationFront / 2) * 0.0343;  

                fgBack = (fgDurationBack / 2)  * 0.0343;  

               fgRight = (fgDurationRight / 2) * 0.0343;  

                fgLeft = (fgDurationLeft / 2)  * 0.0343;  

               

               //send values to App e.g. 10.11|20.22|30.33|40.44\n

               

               Serial.print(fgFront, 2);

               Serial.print("|");

               Serial.print(fgBack, 2);

               Serial.print("|");

               Serial.print(fgRight, 2);

               Serial.print("|");

               Serial.print(fgLeft, 2);

               Serial.println();

               delay(1000);

}



Ultrasonic.ino
Ultrasonic_Dist.aia
UltrasonicDistanceBlocks.png

Chris Ward

unread,
Apr 3, 2019, 10:22:46 AM4/3/19
to MIT App Inventor Forum
Hi Abdullah

For future posts:

1) Use this method to post your Blocks:
https://www.professorcad.co.uk/appinventortips#TipsBlocks  How to create an image of your Blocks and add it to your Forum Post

2) Always upload Sketch files (.ino) - a word processor format can contain hidden syntax/decoration code in it that could cause a .ino file to fail if pasted to.

3) Use your phone's screenshot functions to capture the screen! Less hassle and we can see them properly. Most Android phones will capture the screen when you hold-down the power and volume buttons at the same time. Many phones have a built-in app to do it.

4) You have sent an email but I'm sorry, we do not respond to direct communications. This is because, by keeping the discussion on the forum, there are many people from all around the World (different time zones) who can potentially help you, not just the first responder, who may be unavailable. Plus, there is often something that many forum members can learn by reading the posts.



Abdullah Alvi

unread,
Apr 3, 2019, 10:39:53 AM4/3/19
to MIT App Inventor Forum
first of all, sorry for private inbox message  next thing is that i tried your code and its not showing output :( 
Screenshot is attached
please guide me some more.
May God Bless You
994563c4-f154-4f13-85d5-4936a03445c1.jpg

Chris Ward

unread,
Apr 3, 2019, 11:11:05 AM4/3/19
to MIT App Inventor Forum
Hi Abdullah

There are things to try.

First of all, set "Label_Message_Data" with "DataReceived" to check if any data is received. If there is, post that data string here.

If there is no data received at all by the App, do you have either Terminal software on your PC to read the Serial Data from Arduino or an LCD to attach to the Arduino?

Abdullah Alvi

unread,
Apr 3, 2019, 12:19:01 PM4/3/19
to MIT App Inventor Forum
brother its not showing any data in app but its showing arudino code data in arduino monitor.

Chris Ward

unread,
Apr 3, 2019, 1:04:10 PM4/3/19
to MIT App Inventor Forum
Is the Arduino monitor showing the Serial.print data?

Chris Ward

unread,
Apr 3, 2019, 1:07:18 PM4/3/19
to MIT App Inventor Forum
Hi again

What Bluetooth module are you using on the Arduino: Exact name and Bluetooth version?
What is the Android Version and Bluetooth version on your phone?

Abdullah Alvi

unread,
Apr 3, 2019, 2:31:50 PM4/3/19
to MIT App Inventor Forum
yes brother its showing serial.print data
btw sorry for late reply

Abdullah Alvi

unread,
Apr 3, 2019, 2:32:42 PM4/3/19
to MIT App Inventor Forum
i am using hc05 bluetooth module.
its connecting with app then further its not showing ultrasonic sensor;s value

Chris Ward

unread,
Apr 3, 2019, 3:02:01 PM4/3/19
to mitappinv...@googlegroups.com
Hi Abdullah

I am wondering if your Arduino (uno?) needs to be initialised for the HC05 first.

Also, when you upload a Sketch to Arduino, the Bluetooth module must be disconnected!

Check out my tips:

With the Bluetooth Module attached, Arduino needs at least 500mA.



 

Abdullah Alvi

unread,
Apr 3, 2019, 3:07:19 PM4/3/19
to MIT App Inventor Forum
brother, i am saying that ultrasonic sensor values are showing in arduino ,
but the values in app whole only be shown by using hc05 bluetooth module

Chris Ward

unread,
Apr 3, 2019, 3:12:05 PM4/3/19
to MIT App Inventor Forum
Exactly so, hence my comments.

Chris Ward

unread,
Apr 3, 2019, 6:46:38 PM4/3/19
to MIT App Inventor Forum
Hi again Abdullah

Bluetooth Tips:
The last link in the list is a PDF about HC-05 and it's sister HC-06. On page 5 of that PDF is a schematic. Ensure your setup matches that schematic and that the voltage is 100% correct for your HC-05 version. Note also that the current must be enough to drive the Arduino and the HC-05. 

Note that even though the Phone and the App can "see" the HC-05, the Arduino does not seem to "know" it's there. Once you are sure the hardware setup is perfect, you might like to try an initialisation Sketch (even though it should not be necessary). This is the Sketch used by the Arduino guys before dispatching the Arduino-BT boards:

The Arduino Forum is a good place to seek help too: https://forum.arduino.cc

...remember, when you upload the Ultrasonic Sketch to Arduino, the HC05 must be disconnected first.

Arduino Tips:
See the link for Random Nerd Tutorials: Arduino Trouble-shooting Guide


Reply all
Reply to author
Forward
0 new messages