Approved content

The content below is provided by a partner.

KidSpark/pxt-sparkbit 0.1.2 GitHub

MakeCode extension for Kid Spark Education Spark:bit robotics controller

Tutorials

Step-by-step tutorials for programming the Spark:bit are available on the Kid Spark student portal.

Input Sensors

Bump Sensor (blue)

Used to trigger an event when the bump sensor is pressed.

sparkbitI.bumpSensorIsPressed

sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)

Returns the bump sensor value as a Boolean true if pressed, false if not pressed.

Parameter

Example

basic.forever(function () {
    if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)){
    }
})

Angle Sensor (green)

Used to measure the rotation angle between two parts of the mechanism.

sparkbitI.angleSensor

Returns the angle sensor value as an integer in degrees (0-359) or as a percent (0-100).

sparkbitI.angleSensor(SparkbitInPort.Input1, SparkbitAngle.Degree)

Paramaters

Eample

basic.forever(function () {
    basic.showNumber(sparkbitI.angleSensor(SparkbitInPort.Input1, SparkbitAngle.Degree))
})

sparkbitI.angleSensorCompareDegree

Compares the angle sensor value to a number in degrees (0-359) and returns a Boolean value of true or false.

sparkbitI.angleSensorCompareDegree(SparkbitInPort.Input1, SparkbitLogic.EQ, 0)

Parameters

Example

basic.forever(function () {
    if (sparkbitI.angleSensorCompareDegree(SparkbitInPort.Input1, SparkbitLogic.GT, 90)) {
    }
})

sparkbitI.angleSensorComparePercent

Compares the angle sensor value to a number as a percent (0-100) and returns a Boolean value of true or false.

sparkbitI.angleSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.EQ, 0)

Parameters

Example

basic.forever(function () {
    if (sparkbitI.angleSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.GT, 25)) {
    }
})

Light Sensor (yellow)

Used to measure the amount of light.

sparkbitI.lightSensorPercent

Returns the light sensor value as an integer between 0 dark and 100 bright.

sparkbitI.lightSensorPercent(SparkbitInPort.Input1)

Parameter

Example

basic.forever(function () {
    basic.showNumber(sparkbitI.lightSensorPercent(SparkbitInPort.Input1))
})

sparkbitI.lightSensorComparePercent

Compares the light sensor value to a number as a percent (0-100) and returns a Boolean value of true or false.

sparkbitI.lightSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.EQ, 0)

Parameters

Example

basic.forever(function () {
    if (sparkbitI.lightSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.LT, 50)) {
    }
})

IR Tx/Rx

Low-Power IR Transmitter (grey) with an IR Receiver (white).

High-Power IR Transmitter (black) with an IR Receiver (white).

Used to detect if an infrared (IR) signal successfully travels from the transmitter (Tx) to the recevier (Rx). Applications include detecting proximity of the receiver to the transmitter and detecting an obstical blocking the IR signal.

sparkbitI.irTransmitterIsReceived

Returns a Boolean value of true if an IR signal is received from the IR transmitter indicating no obstical. Returns a Boolean value of false if the IR signal is not received due to an obstical or being too far apart. This coding block works with both the low-power transmitter (grey) and the high-power transmitter (black).

sparkbitI.irTransmitterIsReceived(SparkbitInPort.Input1, SparkbitInPort.Input2)

Parameters

Example

basic.forever(function () {
    if (sparkbitI.irTransmitterIsReceived(SparkbitInPort.Input1, SparkbitInPort.Input2)) {
    }
})

Analog Sensor Value

sparkbitI.analogSensor

Returns the value of any input sensor as a 10-bit integer (0-1023).

sparkbitI.analogSensor(SparkbitInPort.Input1)

Parameters

Example

basic.forever(function () {
    basic.showNumber(sparkbitI.analogSensor(SparkbitInPort.Input1))
})

sparkbitI.analogSensorPercent

Returns the value of any input sensor as a percent (0-100).

sparkbitI.analogSensorPercent(SparkbitInPort.Input1)

Parameter

Example

basic.forever(function () {
    basic.showNumber(sparkbitI.analogSensorPercent(SparkbitInPort.Input1))
})

Output Modules

Motor Module (red)

Used to rotate parts of the mechanism.

sparkbitO.rotateMotorModule

Rotates the motor module based on direction, speed, and optional duration.

sparkbitO.rotateMotorModule(SparkbitOutPort.Output1, SparkbitDirection.Clockwise, 100)

Paramters

Example

basic.forever(function () {
    if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
        sparkbitO.rotateMotorModule(SparkbitOutPort.Output1, SparkbitDirection.Clockwise, 100, 500)
    }
})

sparkbitO.stopMotorModule

Stops the motor module.

sparkbitO.stopMotorModule(SparkbitOutPort.Output1)

Paramter

Example

sparkbitO.rotateMotorModule(SparkbitOutPort.Output1, SparkbitDirection.Clockwise, 100)
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
    sparkbitO.stopMotorModule(SparkbitOutPort.Output1)
}

sparkbitO.rotateMotorModuleVelocity

Rotates the motor module based on a velocity and an optional duration. The direction is determined by the value of the velocity, positive values rotate counterclockwise and negative values rotate clockwise.

sparkbitO.rotateMotorModuleVelocity(SparkbitOutPort.Output1, 0)

Paramters

Example

sparkbitO.rotateMotorModuleVelocity(SparkbitOutPort.Output1, -50)
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
    sparkbitO.stopMotorModule(SparkbitOutPort.Output1)
}

Light Module (orange)

Used to illuminate a red or green LED light.

sparkbitO.setLightModule

Turns on a red a green LED at a brightness and optional duration.

sparkbitO.setLightModule(SparkbitOutPort.Output1, SparkbitColor.Green, 100)

Parameters

Example

basic.forever(function () {
    if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
        sparkbitO.setLightModule(SparkbitOutPort.Output1, SparkbitColor.Green, 100, 500)
    }
})

sparkbitO.stopLightModule

Turns off the light module.

sparkbitO.stopLightModule(SparkbitOutPort.Output1)

Parameter

Example

sparkbitO.setLightModule(SparkbitOutPort.Output1, SparkbitColor.Green, 100)
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
    sparkbitO.stopLightModule(SparkbitOutPort.Output1)
}

Supported targets

License

MIT

makeCodeRender(“{{ site.makecode.home_url }}”, “{{ site.github.owner_name }}/{{ site.github.repository_name }}”);

Sparkbit=github:KidSpark/pxt-sparkbit#v0.1.2