Final answer:
To make a robotic car move forward over a black line using Microbit Blocks, you can use a program that detects the light level under the car and determines whether it is over a black line. If the car detects an obstacle, it can turn around and come back to the starting point. Here is an example code snippet to accomplish this task.
Step-by-step explanation:
To make a robotic car move forward over a black line using Microbit Blocks, you can use the following program:
- Set the car's LED lights to detect light levels.
- Use a loop to continuously check the light level under the car.
- If the light level is below a certain threshold, it means the car is over a black line, and you can make it move forward.
- If the car detects an obstacle on the line, use a rotation command to make it turn around 180 degrees.
- Continue the loop until the car reaches the end of the line.
Here is an example code snippet:
let
threshold = 100
basic.forever
(
() => {
let
lightLevel =
input.lightLevel
()
if
(lightLevel < threshold) {
driveForward
()
}
else if
(lightLevel >= threshold) {
turnAround
()
}
})
function
driveForward() {
basic.showArrow
(4)
pins.digitalWritePin
(
DigitalPin.P0
, 1)
pins.digitalWritePin
(
DigitalPin.P1
, 0)
}
function
turnAround() {
basic.showArrow
(2)
basic.pause
(1000)
pins.digitalWritePin
(
DigitalPin.P0
, 0)
pins.digitalWritePin
(
DigitalPin.P1
, 1)
basic.pause
(1000)
pins.digitalWritePin
(
DigitalPin.P0
, 0)
pins.digitalWritePin
(
DigitalPin.P1
, 0)
}