View unanswered posts | View active topics It is currently Sun Oct 26, 2014 3:07 am






Reply to topic  [ 3 posts ] 
ultrasonic sensor giving an errant -1 reading 
Author Message
Rookie

Joined: Thu Mar 17, 2011 9:18 am
Posts: 2
Post 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
Profile
Site Admin
Site Admin

Joined: Mon Jun 08, 2009 4:50 pm
Posts: 70
Post 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

Undergraduate Intern - NREC, Robotics Academy
ROBOTC - Testing/Documentation/Developer

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


Thu Mar 17, 2011 9:51 am
Profile
Rookie

Joined: Thu Mar 17, 2011 9:18 am
Posts: 2
Post 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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.