Approved content
The content below is provided by a partner.
Step-by-step tutorials for programming the Spark:bit are available on the Kid Spark student portal.
Used to trigger an event when the bump sensor is pressed.
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)){
}
})
Used to measure the rotation angle between two parts of the mechanism.
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
0-359
) or as a percent (0-100
). Eample
basic.forever(function () {
basic.showNumber(sparkbitI.angleSensor(SparkbitInPort.Input1, SparkbitAngle.Degree))
})
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
0-359
) to compare the angle sensor value against.Example
basic.forever(function () {
if (sparkbitI.angleSensorCompareDegree(SparkbitInPort.Input1, SparkbitLogic.GT, 90)) {
}
})
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
0-100
) to compare the angle sensor value against.Example
basic.forever(function () {
if (sparkbitI.angleSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.GT, 25)) {
}
})
Used to measure the amount of light.
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))
})
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
0-100
) to compare the light sensor value against.Example
basic.forever(function () {
if (sparkbitI.lightSensorComparePercent(SparkbitInPort.Input1, SparkbitLogic.LT, 50)) {
}
})
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.
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)) {
}
})
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))
})
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))
})
Used to rotate parts of the mechanism.
Rotates the motor module based on direction, speed, and optional duration.
sparkbitO.rotateMotorModule(SparkbitOutPort.Output1, SparkbitDirection.Clockwise, 100)
Paramters
clockwise
or counterclockwise
.0
stop to 100
max speed.Example
basic.forever(function () {
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
sparkbitO.rotateMotorModule(SparkbitOutPort.Output1, SparkbitDirection.Clockwise, 100, 500)
}
})
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)
}
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
-100
to 100
.Example
sparkbitO.rotateMotorModuleVelocity(SparkbitOutPort.Output1, -50)
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
sparkbitO.stopMotorModule(SparkbitOutPort.Output1)
}
Used to illuminate a red or green LED light.
Turns on a red a green LED at a brightness and optional duration.
sparkbitO.setLightModule(SparkbitOutPort.Output1, SparkbitColor.Green, 100)
Parameters
red
or green
0
off and 100
max brightness.Example
basic.forever(function () {
if (sparkbitI.bumpSensorIsPressed(SparkbitInPort.Input1)) {
sparkbitO.setLightModule(SparkbitOutPort.Output1, SparkbitColor.Green, 100, 500)
}
})
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)
}
MIT
makeCodeRender(“{{ site.makecode.home_url }}”, “{{ site.github.owner_name }}/{{ site.github.repository_name }}”);
Sparkbit=github:KidSpark/pxt-sparkbit#v0.1.3