Connect Ultrasonic detector module with OSOYOO expansion board for Micro:bit “+,p8,p16,-” with 4pin wire as below;
Connect Green LED module with OSOYOO expansion board for Micro:bit P0 with 3pin wire as below;
Connect Yellow LED module with OSOYOO expansion board for Micro:bit P1 with 3pin wire as below;
Connect RED LED module with OSOYOO expansion board for Micro:bit P1 with 3pin wire as below;
Step 1. In the Toolbox, click on the Variables category and then click the Make a Variable button, and then In the New Variable name window, type “range” as the variable name, then click on the Ok button
Step 2. Drag and drop a set range to 0 block inside the forever block.
Step 3. In the Toolbox, click on the Sonar category . Now, click and drag the ping trig echo unit block over and place it inside the placeholder of the set range to 0 block. Then choose P8 in ping trig ,choose p16 in echo ,choose cm in unit from the drop-down menu if it has not already been selected .
Step 4. Click on the Basic category and Click and drag the show number block over and place it inside the placeholder of the forever block just below the set range to 0 block.
Step 5. In the Toolbox, click on the Logic category and Click and drag the if-then-else block over .
Step 6. Place the if-then-else block over inside the placeholder of the forever block just below the show number 0 block.Click on the plus icon to add another section to the if-then-else block.
Step 7. In the Toolbox, click on the Logic category again, click and drag the 0=0 block over and place it inside of the forever block just below the show number block.then choose “≥”from the drop-down menu.
Step 8. Drag and drop a range block from variable category over and place it inside the placeholder of the 0≥0 block.
Step 9. In the Toolbox, click on the Pin category. Then click and drag thedigital write pin to block over and place it inside the forever block.Then choose value “1”from the drop-down menu.
Step 10.Two times right click duplicate from the digital write pin to block over . You will get two duplicated block ,and place it inside the on forever block just below the if range ≥20 then block .change the P0 to P1,P2.
Step 11. click on the Logic category. Then click and drag the and block over and place it inside the placeholder of else block.
Step 12. Click on the Logic category again and drag and drop the 0 <0 block over and place it in front the “and” section of the else block.
Step 13. Drag and drop a range block from variable category over and place it inside the placeholder of the 0<0 block.then write 20 in it as below figure.
Step 14. Click on the Logic category again and drag and drop the 0 <0 block over and place it behind the “and” section of the else block. then write 10 in it as below figure.
Step 15.Drag and drop a range block from variable category again. and place it in front the placeholder of the 0≥0 block.
Step 16.Three times right click duplicate from the digital write pin to block over . You will get three duplicated block ,and place it inside the on forever block just below the else if range <20 and range≥10 then block .change the P0 to P1,P2,0 to 1 as below figure.
Step 17.Three times right click duplicate from the digital write pin to block over again. You will get three duplicated block ,and place it inside the on forever block just below the else block .change the P0 to P1,P2,0 to 1 as below figure.
Step 18.In the Toolbox, click on the Basic category. Then click and drag the pause(ms) block over and place it inside the else block below the three digital write pin to block over .
Step 19. Download this “.hex” file and send it to your MICROBIT disk.
Below is the full code we have done for you as reference:
DownLoad Url osoyoo.com