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

bfeher

Site Admin

Joined: Mon Jun 08, 2009 4:50 pmPosts: 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:

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.

Who is online

Users browsing this forum: No registered users and 2 guests

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