Code for Two Micro:bits - EHMS - Introduction to Robotics 8
by 99771745 in Circuits > Arduino
115 Views, 1 Favorites, 0 Comments
Code for Two Micro:bits - EHMS - Introduction to Robotics 8


You will create code for two micro:bit to drive a moto:bit in Microsoft Makecode
No extensions needed.
Supplies
Microsoft Makecode
First Code

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

- Create two variables called 'name' and 'value'
- Go to 'Radio' and then drag the on 'on radio received' block
- Get both of the variables and then add them on the block. 'name' goes first and then 'value'
- Go to 'Logic' and then drag an 'if' blocks and then put it in the 'on radio received' block
- Also drag the '0 = 0' block and then apply it in 'true'
- Click the 'Advanced' dropdown and then go to 'Text'
- Drag the 'compare name' block and then put it in the first 0
- Get the 'name' variable and then put it in the first slot in the 'compare name' block
- In the second slot where the quotation marks are, type 'mgy'
- Make two variables called 'backwards' and 'right'
- In the 'Variables' section grab a 'set' block
- Change the variable to 'backwards' and then get the variable 'value' and then put it in the second slot.
- Duplicate the entire 'if' section
- In the second 'if' section, change 'mgy' to 'mgx'
- Change the variable in the 'set' block to 'right'
- Make two variables called 'left_motor' and 'right_motor'
- Get a 'set' blocks from the variable section and then make the variable 'right_motor'
- From the 'Math' section, grab a '0 x 0' block and then put it into the second slot of the '_motor' 'set' block
- Change the first 0 to -1
- Get a '0 + 0' block and then put it into the second slot of the '0 x 0' block, replacing the 0
- Get the variable 'backwards' and then put it into the first slot of the '0 + 0', replacing the 0
- Get the variable 'right' and then put it into the second slot of the '0 + 0' replacing the 0
- Duplicate the entire 'set' block
- Change the variable into 'left_motor'
Third Code

- From the 'Basic' panel, grab a 'forever' block
- From the 'Logic' panel, grab a 'if else' block and then drag it inside the 'forever' block
- From the 'Logic' panel, grab a '0 > 0' block and then put it into the 'if' block
- From the 'Advanced' dropdown, go to the 'Pins' section and then grab an 'analog write pin' block. Put it under the 'if' section
- In the 'analog write pin' block click the dropdown and then choose the 'P16 (write only)' then set the value to 0
- Get another 'analog write pin' block and click the dropdown to choose 'P0'
- 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
- Duplicate the two 'analog write pin' blocks and then put both of them under the 'else' section
- For the first 'analog write pin', the pin should be set to 'P0'
- The second 'analog write pin', the pin should be set to 'P16 (write only)'
- 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
- Change the variable in 'if' from 'left_motor' to 'right_motor', leave the value as 0
- For the first 'analog write pin' block under 'if' change the pin to 'P12 (write only)', leave the value as 0
- 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'
- For the first 'analog write pin' under 'else', change the pin to 'P8 (write only)', leave the value as 0
- 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'