Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Controlling the Robotic Arm using the BBC micro:bit
You need to plug the BBC micro:bit into a computer using a USB cable – or sync to it using Bluetooth after downloading the correct app from your app store.
This guide assumes you have already done that!!! If you need help, get in contact [email protected]
If you want to use a fifth motor, you will have to connect it to pin 11 and pin 16, when connecting to pin 11, you cannot use Button B
The guide below assumes you are using four motors and not five
Once you have wired up the robotic arm, head over to the BBC micro:bit website: http://microbit.org/ and click on the ‘Let’s Code’ button
Find the ‘JavaScript Blocks Editor (PXT)’ section and click on the ‘Let’s Code’ button
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on ‘projects’ ‘new project…’
Next you will need to rename the project so it is easier to locate later on in the activity
Get rid of all the blocks on the page, this can be done by dragging them to the left, over the tabs
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Basic’ tab, select the ‘on start’ block and drag it onto the page
Click on the ‘Variables’ tab, select the ‘set item to’ block and drag it in-between the ‘on start’ block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on ‘item’ and rename it to ‘MotorNumber’
Again, click on the ‘Variables’ tab, select the ‘set item to’ block and drag it below the ‘set MotorNumber’ block
Rename the ‘item’ variable to ‘CurrentMotor’
Now we need to stop power going through to the pins when the robotic arm turns on. To do this, click on the ‘Advanced’ tab, then the ‘Pins’ tab, select the ‘digital write pin’ block and place it underneath the ‘set CurrentMotor’ block.
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now we need to add the other seven pins, to do this duplicate the ‘digital write pin’ seven times and place them underneath each other
Next we need to replace the ‘P0’ in each of the statements with the corresponding pins from the wiring diagram previously
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now we need to show the motor number, click on the ‘Basic’ tab, select the ‘show number’ block and place it underneath the ‘digital write’ pin block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now, click on the ‘Variables’ tab, select the ‘MotorNumber’ block and drag it over the ‘0’ from the ‘show number’ block
Now we need to create a bit of code to select a motor number.
Click on the ‘Input’ tab, select the ‘on button A pressed’ block and drag it onto a blank section of the page
Now, we need to create an ‘if’ statement so that each time button A is pressed it will count up to 6
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Logic’ tab, select the ‘if’ statement and drag it in-between the ‘on button A pressed’ block
Click on the ‘Logic’ tab, select the ‘0=0’ block and place it over the ‘true’ block on the ‘if’ statement
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next click on the ‘Variables’ tab, select the ‘MotorNumber’ block and drag it over the first ‘0’ in the ‘0=0’ block. Then change the second ‘0’ to ‘5’ – this tells the micro:bit that when the variable ‘MotorNumber’ reaches 5 it will run the statements below, in the ‘then’ section
Next, click on the ‘Variables’ tab, select the ‘set item to’ block and drag it into he then section of the ‘if’ statement
Change the ‘item’ to ‘MotorNumber’, then duplicate and place in the else section
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now we need to set the counter. To do this, click on the ‘Math’ tab, select the ‘0+0’ block and drag it over the ‘0’ on the second ‘set MotorNumber to’ block
Click on the ‘Variables’ tab, select the ‘MotorNumber’ block and drag it over the first ‘0’ on the ‘0+0’ block, then change the second ‘0’ to ‘1’
Click on the ‘Basic’ tab, select the ‘show number’ block and drag it underneath the ‘if’ statement
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Variables’ tab, select the ‘MotorNumber’ block, drag and replace the ‘0’ from the ‘show number’ block
Now we need to set button B so that when it is pressed it locks the number so you can move the motor chosen
Click on the ‘input’ tab, select the ‘on button A pressed’ block and drag it onto the page
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
You will see that the block has now been greyed out, click on the ‘A’ and change it to ‘B’
Next, click on the ‘Variables’ tab, select the ‘set item to’ block an drag it in-between the ‘on button B pressed’ block
Click on ‘item’ and rename to ‘CurrentMotor’
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, click on the ‘Variables’ tab, select the ‘MotorNumber’ block, drag and replace the ‘0’ from the ‘set CurrentMotor to’ block
We now have created code for button A and button B, next we need to create the code that goes on “behind the scenes”
Click on the on the ‘Basic’ tab, select the ‘forever’ block and drag it onto the page
Next, click on the ‘Logic’ tab, select the ‘if’ statement and drag it in-between the ‘forever’ block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the blue cog and drag an ‘else if’ underneath the if statement to create another section
Click on the ‘Logic’ tab, select the ‘0 < 0’ block and drag it over the ‘true’ block
Now we need to tell the micro:bit that if you tilt it left, the motor will turn left etc.
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Input’ tab, select the ‘acceleration’ block and drag it over the first ‘0’
Now, change the ‘0’ to ‘-300’ – when you are using numbers on the X axis, minus ‘-‘ numbers are for when you want the micro:bit to tilt left and positive numbers are for when the micro:bit tilts right
Now we need to tell the micro:bit to show a directional arrow but also control the motors.
Click on the ‘Basic’ tab, then ‘… More’ and select the ‘show arrow’ block.
Drag the ‘show arrow’ block into the ‘then’ section of the ‘if’ statement
3.
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘north’ block and change it to ‘west’ – this changes the arrow so when you tilt the micro:bit left, the arrow will point left or west on a compass
Click on the ‘Logic’ tab, select the ‘if’ statement and drag it underneath the ‘show arrow’ block
Click on the blue cog icon located on the ‘if’ statement
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now drag the else if block underneath the ‘if’ block on the right. Repeat two more times to have a total of 3 ‘else if’ blocks
Now we need to tell the micro:bit to move the robotic arm motors, to do this we define a variable for each motor.
Click on the ‘Logic’ tab, select the ‘0=0’ block, drag and drop it to the right of the if statement, replacing the ‘true’ block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Variables’ tab, select the ‘CurrentMotor’ block and drag it over the first ‘0’ on the ‘0=0’ statement
Change the second ‘0’ to ‘1’ – this statement tells the micro:bit, that if the motor selected with button B is number 1 then run the code we are going to put in the ‘then’ section
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, click on the ‘Advanced’ tab, then the ‘Pins’ tab. Select the ‘digital write pin’ block and drag it into the first ‘then’ section of the ‘if’ statement
Duplicate the ‘digital write’ block and place it underneath
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now we need to change the pin number and the value of the pins
Change the ‘0’ in the first ‘digital write pin’ block to ‘1’, then change the Pin on the second ‘digital write’ block to ‘P1 – this will move the base motor left
Now we need to create the second motor function
Duplicate the ‘CurrentMotor = 1’ block, drag and snap into place on the right of the first ‘else if’ section
Change the ‘1’ to ‘2’
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, click on the ‘Advanced’ tab, then the ‘Pins’ tab. Select the ‘digital write pin’ block and drag it into the second ‘then’ section
Duplicate the ‘digital write’ block and place it underneath
Change the pin from ‘P0’ to ‘P8’ and ‘0’ to ‘1’ on the first ‘digital write’ statement.
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, change the pin from ‘P0’ to ‘P12’ on the second ‘digital write’ statement – this will create a section of code to control the second motor
Duplicate the ‘CurrentMotor = 2’ block, drag and snap into place on the right of the second ‘else if’ section
Replace the ‘2’ with ‘3’
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, click on the ‘Advanced’ tab, then the ‘Pins’ tab. Select the ‘digital write pin’ block and drag it into the third ‘then’ section
Duplicate the ‘digital write’ block and place it underneath
Change the pin from ‘P0’ to ‘P2’ and ‘0’ to ‘1’ on the first ‘digital write’ block, then change the pin to ‘P13’ on the second ‘digital write’ block – this creates the block of code to control the third motor
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Duplicate the ‘CurrentMotor = 3’ block, drag and snap into place on the right of the third ‘else if’ section
Replace the ‘3’ with ‘4’
Next, click on the ‘Advanced’ tab, then the ‘Pins’ tab. Select the ‘digital write pin’ block and drag it into the fourth ‘then’ section
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Duplicate the ‘digital write’ block and place it underneath
Change the pin from ‘P0’ to ‘P14’ and ‘0’ to ‘1’ on the first ‘digital write’ block, then change the pin to ‘P15’ on the second ‘digital write’ block – this creates the block of code to control the fourth motor
Now we need to create a block of code so when the micro:bit tilts right the motors turn anticlockwise
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Click on the ‘Logic’ tab, select the ‘0 < 0’ block and drag it to the right of the ‘else if’ on the first ‘if’ statement
Click on the ‘Input’ tab, select the ‘acceleration’ block and drag it over the first ‘0’ on the ‘0 < 0’ statement
Next, change the ‘<’ symbol to ‘>’, then ‘0’ to ‘300’
Duplicate the whole ‘if’ statement located in the first ‘then’ section and place it below the ‘acceleration > 300’ block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Next, we need to change the pin numbers so when the micro:bit tilts left, the motors turn the opposite direction. All that is needed here is to change the ‘1’s to ‘0’s and the ‘0’s to ‘1’s
Now, most of the code is done, all that is left to do is to add code into the ‘else’ section so when the micro:bit is not doing any of the previous code it will stop all motors
Click on the ‘Basic’ tab, select the ‘show leds’ block and drag it into the ‘else’ section
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Now we need to create a plus sign in the ‘show leds’ block, to do this, click on the individual boxes until you make a ‘+’ shape
Now we need to make sure the pins we use are turned off.
Click on the ‘Advanced’ tab, then the ‘Pins’ tab. Select the ‘digital write’ block and drag it below the previously placed ‘show leds’ block
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Repeat the above step, seven more times
Now, change the pins on all eight ‘digital write’ blocks to the micro:bit pins used to control the motors (see image below)
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
You now will have all the code to control the Robotic Arm using a micro:bit
You can now download the code to the micro:bit or use the ‘Downloading code to the BBC micro:bit’ guide on the next page if you are unsure
Downloading code to the BBC micro:bit
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
On the bottom left of the PXT editor is a button that says 'Download' - shown below in a red box, click on it to save the code file
Click on the downloaded file at the bottom of the browser - select 'Show in Folder'
This will open the folder containing all your downloaded files - this will include a file called something like 'microbit-micro-bit-Robotic-Arm.hex'
Copyright © 2017 CB Information Systems Ltd. All Rights Reserved
Right click on the file and choose to copy it (this is ready for the next step), now you need to plug the micro:bit, into your computer
Right click on a blank space within the micro:bit folder and choose 'Paste' - it will display a window a bit like the below as it copies the file to the micro:bit.
The code has now downloaded, have fun playing with the micro:bit Robotic Arm!!