To be able to:
Last week we noted that Pin 13 controlled the neopixel LED's.
Have another look at the pin chart to see which pins control the left and right motors.
The motors have got 2 programmable pins per side, for a total of 4 pins.
These pins are pin 8 and pin 14.
Left
Dir Pin 8
Right
Dir Pin 14
When we program the motors we use a 1 and a 0 to tell which way to rotate the motors.
0 is for forwards
1 is for reverse
Dir Pin 8
Dir Pin 14
Which pins and values would we set for moving the vehicle in reverse?
Which pins and values would we set for moving the vehicle forwards?
What about pin 12 and pin 16?
Now that we have told the pins which direction to go in, we have to tell it how quickly to rotate.
Left Motor
Pin 12
0 - 1023
The speed can be programmed within a range of numbers, 0 - 1023.
1023 is the highest if we are going forwards, 0 is the highest if we are in reverse.
Right Motor
Pin 16
0 - 1023
Pin 8 | 0 |
Pin 14 | 0 |
Pin 12 | 1023 |
Pin 16 | 1023 |
Pin 8 | 1 |
Pin 14 | 0 |
Pin 12 | 500 |
Pin 16 | 500 |
Pin 8 | 1 |
Pin 14 | 1 |
Pin 12 | 10 |
Pin 16 | 10 |
Pin 8 | 0 |
Pin 14 | 0 |
Pin 12 | 300 |
Pin 16 | 700 |
Look at the following algorithms. Can you predict the behaviours of the robot in each?
Your speed, pins 12 and 16, uses write_analog.
Your direction, pins 8 and 14, uses write_digital.
from microbit import *
# Left Motor
pin12.write_analog(600)
pin8.write_digital(0)
# Right Motor
pin16.write_analog(600)
pin14.write_digital(0)
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
pin12.write_analog(0)
pin16.write_analog(0)
How will the MiniBit robot behave?
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
sleep(2000)
pin12.write_analog(0)
pin16.write_analog(0)
How will the MiniBit robot behave?
How will the MiniBit robot behave?
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
sleep(2000)
pin12.write_analog(600)
pin8.write_digital(1)
pin16.write_analog(600)
pin14.write_digital(1)
sleep(2000)
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
from microbit import *
# Write your code here...
pin12.write_analog(1023)
pin8.write_digital(0)
pin16.write_analog(1023)
pin14.write_digital(0)
HINT:
from microbit import *
while True:
HINT:
Extension Task:
Use a FOR loop to reduce the amount of coding.
HINT:
from microbit import *
#Forward
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
sleep(2000)
#Stop Forward
pin16.write_analog(0)
pin14.write_digital(0)
sleep(250)
#Turn right
pin12.write_analog(600)
pin8.write_digital(0)
sleep(250)
#Forward
pin12.write_analog(600)
pin8.write_digital(0)
pin16.write_analog(600)
pin14.write_digital(0)
sleep(2000)
Scroll down the code