Code for Two Micro:bits - EHMS - Introduction to Robotics 8

by 99771745 in Circuits > Arduino

112 Views, 1 Favorites, 0 Comments

Code for Two Micro:bits - EHMS - Introduction to Robotics 8

Annotation 2023-05-19 133551.png
Annotation 2023-05-17 142422.png

You will create code for two micro:bit to drive a moto:bit in Microsoft Makecode

No extensions needed.

Supplies

Microsoft Makecode

First Code

Annotation 2023-05-17 142537.png


  1. Go to 'Basic' and then drag the 'on start' block into the workplace
  2. Go to 'Radio' and then drag the 'radio set group' block and set the value to 1.
  3. Also in the 'on start' block, in 'Basic' drag the 'show icon' block and then select the one shown in the image

Second Code

Annotation 2023-05-17 143024.png
  1. Create two variables called 'name' and 'value'
  2. Go to 'Radio' and then drag the on 'on radio received' block
  3. Get both of the variables and then add them on the block. 'name' goes first and then 'value'
  4. Go to 'Logic' and then drag an 'if' blocks and then put it in the 'on radio received' block
  5. Also drag the '0 = 0' block and then apply it in 'true'
  6. Click the 'Advanced' dropdown and then go to 'Text'
  7. Drag the 'compare name' block and then put it in the first 0
  8. Get the 'name' variable and then put it in the first slot in the 'compare name' block
  9. In the second slot where the quotation marks are, type 'mgy'
  10. Make two variables called 'backwards' and 'right'
  11. In the 'Variables' section grab a 'set' block
  12. Change the variable to 'backwards' and then get the variable 'value' and then put it in the second slot.
  13. Duplicate the entire 'if' section
  14. In the second 'if' section, change 'mgy' to 'mgx'
  15. Change the variable in the 'set' block to 'right'
  16. Make two variables called 'left_motor' and 'right_motor'
  17. Get a 'set' blocks from the variable section and then make the variable 'right_motor'
  18. From the 'Math' section, grab a '0 x 0' block and then put it into the second slot of the '_motor' 'set' block
  19. Change the first 0 to -1
  20. Get a '0 + 0' block and then put it into the second slot of the '0 x 0' block, replacing the 0
  21. Get the variable 'backwards' and then put it into the first slot of the '0 + 0', replacing the 0
  22. Get the variable 'right' and then put it into the second slot of the '0 + 0' replacing the 0
  23. Duplicate the entire 'set' block
  24. Change the variable into 'left_motor'

Third Code

Annotation 2023-05-17 144840.png
  1. From the 'Basic' panel, grab a 'forever' block
  2. From the 'Logic' panel, grab a 'if else' block and then drag it inside the 'forever' block
  3. From the 'Logic' panel, grab a '0 > 0' block and then put it into the 'if' block
  4. From the 'Advanced' dropdown, go to the 'Pins' section and then grab an 'analog write pin' block. Put it under the 'if' section
  5. In the 'analog write pin' block click the dropdown and then choose the 'P16 (write only)' then set the value to 0
  6. Get another 'analog write pin' block and click the dropdown to choose 'P0'
  7. In the 'Math' section, get the 'absolute of' block and in the value get the variable 'left_motor' and then drag it into the slot
  8. Duplicate the two 'analog write pin' blocks and then put both of them under the 'else' section
  9. For the first 'analog write pin', the pin should be set to 'P0'
  10. The second 'analog write pin', the pin should be set to 'P16 (write only)'
  11. Duplicate the entire 'if else' block then put the second 'if else' under the two 'analog write pin' blocks (make sure it is in the 'else' section
  12. Change the variable in 'if' from 'left_motor' to 'right_motor', leave the value as 0
  13. For the first 'analog write pin' block under 'if' change the pin to 'P12 (write only)', leave the value as 0
  14. For the second 'analog write pin', change the pin to 'P8 (write only)', then at the 'absolute of' block, change the variable to 'right_motor'
  15. For the first 'analog write pin' under 'else', change the pin to 'P8 (write only)', leave the value as 0
  16. For the second 'analog write pin', change the pin to 'P12 (write only)', then at the 'absolute of' block, change the variable to 'right_motor'