Programming and note
If/else:
The
If/else
block is a logical structure. If the logical statement that is attached to it
(digital read P1 = 0) is true, then it will execute the code blocks inside of the if. If that
statement is false, it will execute the
else
blocks. In this case, if the statement is true(the
value is digital read from P1 port equal to 0, note that it will output a logic LOW signal when
the collision sensor is crashed), then turn on the LED and buzzer on P0 and P2; else, turn off
the LED and buzzer on P0 and P2.
Digital Write:
There are two modules we need to use the
digital write
block to set they
output 1(logic HIGH signal) or 0(logic LOW signal). Here, we set the LED and buzzer output to
1 when the collision sensor detect a collision, otherwise, set it to 0.
Download the program to micro:bit to see what happens.
When the on-board switch of the collision sensor is pressed, LED will light on
and the buzzer will make a big beep noise to remind collision to occur,
otherwise, LED will light off and buzzer stop work.
Result
The car can detect a collision, but how the car detects the obstacle to prevent a crash?
16