Using If Statement to Send Notification when Speed Exceeds

24 views
Skip to first unread message

Deborah

unread,
Apr 12, 2019, 9:39:03 PM4/12/19
to MIT App Inventor Forum
I want to send a notification to the user when he is about to or has exceeded the set speed based on the measured speed detected by the app from the location sensor. May I know why this is an error and cant be executed? How do I go about to solve it? Thank you
Ifstatement.PNG

SteveJG

unread,
Apr 12, 2019, 9:58:04 PM4/12/19
to mitappinv...@googlegroups.com
Put your code into the LocationSensor.LocationChanged event handler.    Speed is treated like a local variable, therefore you cannot use the speed variable in your showlocation Procedure.

Another way to do this would be to create a global variable ... perhaps    mySpeed  and in the LocationChanged event set  mySpeed to speed     and only THEN use mySpeed in place of speed in your showlocation Procedure.

Also be aware, that speed is only a valid number after the LocationSensor has made two separate LocationChanged events (a satellite fix has to be achieved twice .. the variable needs two different geocoordinate readings to calculate a speed.     

For your else statement, rather than mySpeed > 8   you might use     mySpeed > 8   and  mySpeed <10   ... do you see why?   Also, you want to be aware of the units output with    speed   

speed units - the LocationSensor reports speed in m/sec units), convert to km/hr if you want speed in kilometers (1000 meters) and also be aware m/sec must be addressed to convert kilometers/hour.  m/sec * km/1000m * 60 sec/min * 60 min/hour = km/hour .

Your use of speed > 8   and <10  are suspect:    10 speed units  (10 m/sec)   is only about 22.4 miles per hour.

Regards,
Steve
Reply all
Reply to author
Forward
0 new messages