πŸ‘¨β€πŸ’» Getting the Ultrasonic Distance Module to Work with Dragit | myBetabox

πŸ‘¨β€πŸ’» Getting the Ultrasonic Distance Module to Work with Dragit

Now we’re going to get the Ultrasonic Module working so that our car can measure distance and stop before obstacles.

This module works in a similar fashion to how a bat uses echolocation to determine what is around it.

In Dragit, under the PiCar_S, you will find the ultrasonic distance variable.

Note: Variables will have rounded corners.

Go ahead and drag that element into the work area.

You’ll notice that the 20 is for the pins we plugged the distance module into. You should see B20 next to where you plugged the line follower in. This variable will change from -1 to 100 depending on how far away something is from the sensor with -1 being the closest and 100 being the farthest.

Let’s make an algorithm that will stop the car if it gets too close to something. Create this program in DragIt:

This code is included below! Download and import it into Dragit just in case you run into issues building this algorithm yourself.

Download by right clicking and selecting Save link as: Ultrasonic Stopper XML
You should see something like this when you run it:

If you’d like to change stopping distance between your obstacle and the car, simply modify theΒ 10 in the green operator function in the IF statement.

Take some time to play around with this program. What kind of robots could you make with these capability? When you are ready, let’s move on.