Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

0
Project freezes when using pulse-in to read Ultrasonic sensor.

So I just bought a Virtuabotix Ultrasonic Sensor for a personal project and the problem its giving me is that when the obstacle is too far it will hang for a great period of time. The code I am using is here:

int readDistance() {
int duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/29.1) / 2;
return distance;
}

How can I fix this? is it a hardware problem?

2 Answers

0
accepted

This problem has been resolved with the latest version of the Virtuabotix Ultrasonic sensor. https://www.virtuabotix.com/reference/index.php?title=Ultrasonic_Sensor_Library

The problem was that the Pulse-In function would cause the Arduino to hang if no reading was returned.

0

This problem is related to the way you are using the Pulsin function. This issue is addressed in the Virtuabotix Ultrasonic Library, but can also be resolved by using pulseIn(myEcho, HIGH, 1000, myTimeout); where myTimeout is the number of microseconds where you want to assume no reading will occur.

I recomend just using the Virtuabotix Ultrasonic library for most folks, but that is the way to resolve the PulsIn problem in general for your next Arduino project.

Your Answer

No comments yet.

Leave a Reply