Code Monkey home page Code Monkey logo

pxt-powerfunctions's Introduction

Power Functions IR Sender

Build Status

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

Installation

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.

Documentation

powerfunctions.connectIrLed

Configures the infrared LED pin. A 940 nm emitting diode is required.

powerfunctions.connectIrLed(AnalogPin.P0)

Parameters

  • pin - analog pin with an attached IR-emitting LED

powerfunctions.setSpeed

Sets the speed of a motor.

powerfunctions.setSpeed(PowerFunctionsMotor.Red1, 3)

Parameters

  • motor - the motor
  • speed - the speed of the motor from -7 to 7.

powerfunctions.brake

Brakes then float. The motor's power is quickly reversed and thus the motor will stop abruptly.

powerfunctions.brake(PowerFunctionsMotor.Red1)

Parameters

  • motor - the motor

powerfunctions.float

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)

Parameters

  • motor - the motor

powerfunctions.setMotorDirection

Configures a motor direction.

powerfunctions.setMotorDirection(PowerFunctionsMotor.Red1, PowerFunctionsDirection.Right)

Parameters

  • motor - the motor
  • direction - the direction of the motor

MakeCode Example

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);
  }
});

Disclaimer

LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.

License

Copyright (C) 2017-2020 Philipp Henkel

Licensed under the MIT License (MIT). See LICENSE file for more details.

Supported targets

  • for PXT/microbit

pxt-powerfunctions's People

Contributors

philipphenkel avatar yishii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pxt-powerfunctions's Issues

Create a port for m5stack's UIFlow

This is a fantastic project, thanks for doing this!
I wanted to ask if it might be possible to port this over to M5Stack's UIFLow (also blockly based). m5stack is a series of nice little modules based on ESP32, many of them coming with IR LED built in (e.g. m5stick and m5atom). These are perfect to use with lego, many of their modules are lego-compatible, see e.g. the m5stack fire

Possibly brittle timing

This code leaves itself open to being possibly brittle in its timing (especially with upgrades to the MakeCode runtime and interference from interrupts).

            // calliope correction -85, -210
            // microbit correction -65, -150
            transmitBit(markMicroSeconds: number, pauseMicroSeconds: number): void {
                pins.analogWritePin(this.pin, 511)
                control.waitMicros(Math.max(1, markMicroSeconds - 65))
                pins.analogWritePin(this.pin, 0)
                control.waitMicros(Math.max(1, pauseMicroSeconds - 150))

There is a new feature added to MakeCode beta that should make it possible implement bit-banging solutions like this to be more resilient in these cases...

microsoft/pxt#3198

Calculate a forward looking time horizon, then wait until the microsecond timer has achieved that value. Variances in timing due to different execution speeds on different versions of the runtime, or interference from interrupts should then be minimised.

Extension not works in new version of MakeCode

Hi Philipp!
First I want to thank you for this material. Unfortunately, I must say that with new version of MakeCode package is not functional again - https://makecode.microbit.org/v0 and https://makecode.microbit.org/v1 works well, but latest version dont make connection.

Is there some way how to fix it again? Thank you very much!

Just for information: Im using Grove - Infrared Emitter http://wiki.seeedstudio.com/Grove-Infrared_Emitter/ with Grove Shield for micro:bit v2.0.
By specification it using TSAL6200 diode https://www.vishay.com/docs/81010/tsal6200.pdf

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.