
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.
Try it and see if it helps your problem,