Approved content
The content below is provided by a partner.
The package adds support for the moto:bit add-on board from SparkFun.
TODO: To use this package, go to https://pxt.microbit.org, click Add package and search for moto-bit.
The package adds support for the moto:bit add-on board from SparkFun.
The following micro:bit pins are used for analog and digital sensors, motor driving:
P0 – Analog Input 0P1 – Analog Input 1P2 – Analog Input 2P8 – Digital Input/OutputP12 – Digital Input/Output P14 – Digital Input/OutputP15 – Servo MotorP16 – Servo MotorP19 – motor driver - SCLP20 – motor driver - SDA To set the speed and direction for a motor, place the |set motor| block.
The block takes three parameters: motor select, direction, and speed.
Left or RightForward or Reverse0 and 100motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50)
When a motor turns opposite to the direction that was declared, the invert
block may be used. The block accepts two parameters: motor select, and a
boolean variable. When the boolean is set to true, the motor direction is
inverted so that Forward no longer causes the motor to spin in Reverse,
but rather Forward.
Left or Righttrue or falsemotobit.invert(Motor.Left, true)
Regardless of the set motor speed, before the motors will turn on the switch on moto:bit
must be set to “Run Motors”, and the enable motors command must be set to ON
On or Off.motobit.enable(MotorPower.On)
The following program reads a string to control the direction of two motors.
radio.onDataPacketReceived( ({ receivedNumber }) => {
// Drive forward
if (receivedNumber == 128) {
led.plot(2, 0)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50)
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 50)
motobit.enable(MotorPower.On)
} else {
led.unplot(2, 0)
}
// Turn left
if (receivedNumber == 64) {
led.plot(0, 2)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 50)
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 50)
motobit.enable(MotorPower.On)
} else {
led.unplot(0, 2)
}
// Turn right
if (receivedNumber == 32) {
led.plot(4, 2)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50)
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50)
motobit.enable(MotorPower.On)
} else {
led.unplot(4, 2)
}
// Drive in reverse
if (receivedNumber == 16) {
led.plot(2, 4)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 50)
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50)
motobit.enable(MotorPower.On)
} else {
led.unplot(2, 4)
}
// Stop
if (receivedNumber == 0) {
motobit.enable(MotorPower.Off)
}
})
radio.setGroup(13)
MIT
motobit=github:sparkfun/pxt-moto-bit
motobit=github:sparkfun/pxt-moto-bit#v0.0.4