ROBOTC.net forums
http://www.robotc.net/forums/

ultrasonic sensor giving an errant -1 reading
http://www.robotc.net/forums/viewtopic.php?f=11&t=3124
Page 1 of 1

Author:  wpgray [ Thu Mar 17, 2011 9:26 am ]
Post subject:  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?

Author:  bfeher [ Thu Mar 17, 2011 9:51 am ]
Post subject:  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,

Author:  wpgray [ Thu Mar 17, 2011 11:47 am ]
Post subject:  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!

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/