View unanswered posts | View active topics It is currently Fri Jul 01, 2016 2:28 am

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
ultrasonic sensor giving an errant -1 reading
Author Message
Rookie

Joined: Thu Mar 17, 2011 9:18 am
Posts: 2
ultrasonic sensor giving an errant -1 reading
I am trying to write a program that has my robot stop at a distance of 15 inches from the wall, however my ultrasonic sensor will read distances correctly but I will get flashes of a -1 in the readings as it approaches an object. How do I proceed?

Thu Mar 17, 2011 9:26 am

Joined: Mon Jun 08, 2009 4:50 pm
Posts: 70
Re: ultrasonic sensor giving an errant -1 reading
Hi wpgray,

The -1 means that it didn't receive any information back (the sonar sends pulses and waits for them to bounce off of stuff and then uses the time it took to measure distance).
This can happen if the signal bounces off of an object at a bad angle and never returns to the sensor, or some material that doesn't reflect it well enough, or even just some garbage data caused by cosmic rays or voltage spikes, etc.

One way to get around this would be to add some logic into your program to account for seeing a -1:

 Code://...while(SensorValue(sonar) > 15 || SensorValue(sonar) == -1){  // forward~}// stop//...

What I've done is add the || (the forward slash key + shift, twice) which is the logical OR operator. So we are now saying, "while the value of the sonar sensor is greater than 15 OR equal to -1".
That way if you are traveling along and the sensor reads -1 erroneously, it will still pass the condition of the loop. Logical OR needs only one of the conditions to be true for the whole condition to pass. Logical operators are very useful and I recommend reading up on them.

Try it and see if it helps your problem,

_________________
Bence Feher

ROBOTC - Testing/Documentation/Developer

Computer Science, Japanese, East Asian Studies
University of Pittsburgh, Konan University 甲南大学

Thu Mar 17, 2011 9:51 am
Rookie

Joined: Thu Mar 17, 2011 9:18 am
Posts: 2
Re: ultrasonic sensor giving an errant -1 reading
Thank you,
I will try it asap (monday @ school), your answer was what I was looking for an example with an explanation. Thanks for the lesson teach!

Thu Mar 17, 2011 11:47 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 3 posts ]

#### Who is online

Users browsing this forum: nekinzjuriya and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning