In this project we program the distance sensors on the Move Motor Car to detect and navigate around objects.
The Move Motor car has and ultrasonic sensor at the front of it.
What is an ultrasonic sensor?
An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity. High-frequency sound waves reflect from boundaries to produce distinct echo patterns.
How Ultrasonic Sensors Work
Ultrasonic sensors work by sending out a sound wave at a frequency above the range of human hearing. The transducer of the sensor acts as a microphone to receive and send the ultrasonic sound. The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the ultrasonic pulse.
Go to the https://makecode.microbit.org website, create a new project and add the kitronik-move-motor extension to your project.
To add an extension follow these steps:
Let's program the sensor to measure the distance to an object.
Create a new variable called distance and add the following code to your project. This code will store the distance (in cms) that the sensor is reading into the 'distance' variable and then it will shot the number on the Microbit that's attached to the Move Motor car.
Download your code onto the Microbit, turn on your Move Motor car and test that the code works by putting an object at different distances from the sensor and seeing the result on the Microbit.
Now that we know how far away the object is, let's add some code to move the car so that it is always 10cm away.
Change your code to the following, download it onto your Microbit and then test that the car moves when you move the object away and then stops when it is 10cm away.
So far the car will stay any distance under 10cm away from the object. Let's add some code to reverse the car until it is exactly 10cm away.
Update your code to the following, download it onto your Microbit and test that it works by moving the object back and forth. Your car should move to stay exactly 10cm away from the object.