Introduction
Use the servo sonar module to improve our intelligent driving!
-
-
In this project, we'll be using your robot's motors to create a self-driving car!
-
To access the motor libraries, go to https://www.techcamp.org.uk/invent. This will give you access to our Invent modules, which includes a module for controlling the motors.
-
You'll know you're in the right place if you can see the Invent namespace on the right-hand side.
-
-
-
In this project, we want to develop a car that looks left and right when it detects an obstacle, and then goes in the direction with more clearance. Let's get started!
-
First, we reset the servo position to be looking straight ahead in the first 3 lines.
-
Next in the forever loop, we tell the car to drive forwards. We then have an if statement, which will run if there's an obstacle in front of the sonar.
-
In TypeScript, comments are denoted with
//
rather than#
like in Python.
-
-
-
First, we need to stop the car if there's an obstacle so we don't ram into it!
-
Next, we use the servo to look left and take the sonar reading, and then the same for the right.
-
Now that we have our measurements, we can look straight ahead again.
-
-
-
You'll notice we left the contents of the while loop empty!
-
Can you write the code that will make the car turn left if there's more space, or right if not?
-
-
-
On your board from the sonar project, attach the two motors if they're not already installed!
-
-
-
Program your micro:bit and turn on the car!
-
You should find that when it encounters an obstacle, it will stop, look both ways, and then turn in the direction with more clearance. If so, you're done!
-