Control your LEGO® Power Functions motors using your micro:bit or Calliope-Mini, an infrared LED and MakeCode. This extension turns your device into a remote control for your Power Functions devices.
A project using this extension is documented on hackster.io
Open MakeCode and select '+ Extensions' in the 'Advanced' menu. You need to enter our project URL https://github.com/philipphenkel/pxt-powerfunctions in the search field, hit return and then select the powerfunctions extension.
Configures the infrared LED pin. A 940 nm emitting diode is required.
powerfunctions.connectIrLed(AnalogPin.P0)
pin
- analog pin with an attached IR-emitting LED
Sets the speed of a motor.
powerfunctions.setSpeed(PowerFunctionsMotor.Red1, 3)
motor
- the motorspeed
- the speed of the motor from-7
to7
.
Brakes then float. The motor's power is quickly reversed and thus the motor will stop abruptly.
powerfunctions.brake(PowerFunctionsMotor.Red1)
motor
- the motor
Floats a motor to stop. The motor's power is switched off and thus the motor will roll to a stop.
powerfunctions.float(PowerFunctionsMotor.Red1)
motor
- the motor
Configures a motor direction.
powerfunctions.setMotorDirection(PowerFunctionsMotor.Red1, PowerFunctionsDirection.Right)
motor
- the motordirection
- the direction of the motor
basic.showIcon(IconNames.Heart);
powerfunctions.connectIrLed(AnalogPin.P1);
powerfunctions.setMotorDirection(
PowerFunctionsMotor.Blue1,
PowerFunctionsDirection.Backward
);
input.onButtonPressed(Button.A, () => {
powerfunctions.setSpeed(PowerFunctionsMotor.Blue1, 3);
});
input.onButtonPressed(Button.B, () => {
powerfunctions.float(PowerFunctionsMotor.Blue1);
});
basic.forever(() => {
led.plotBarGraph(input.lightLevel(), 255);
if (input.lightLevel() > 200) {
powerfunctions.float(PowerFunctionsMotor.Blue1);
basic.pause(5000);
powerfunctions.setSpeed(PowerFunctionsMotor.Blue1, 2);
basic.pause(3000);
}
});
LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.
Copyright (C) 2017-2020 Philipp Henkel
Licensed under the MIT License (MIT). See LICENSE file for more details.
- for PXT/microbit