Public Hummingbird Methods

 

NOTE: If you issue Bluetooth commands very close together (< 100 ms), it is possible that some commands may be skipped. For example, if you set the color of the tri-color LED to green and then immediately set it to red, you may not see the effect of the green command.

Method Signature: setLED(port: Int, intensity: Int)
Description: Sets an LED to a given intensity value. The method requires the port number of the LED (1-3) and an intensity value from 0-100. An intensity value of 0 turns the LED off.
Example: hummingbird.setLED(port: 1, intensity: 100)

Method Signature: setTriLED(port: Int, red: Int, green: Int, blue: Int)
Description: Sets a tri-color LED to a given color by setting the intensities of the red, green, and blue elements inside it . The method requires the port number of the tri-color LED (1-2) and three intensity values from 0-100. Setting all three intensity values to 0 turns the LED off.
Example: hummingbird.setTriLED(port: 1, red: 75, green: 0, blue: 75)

Method Signature: setPositionServo(port: Int, angle: Int)
Description: Sets a position servo to a given angle. The method requires the port number of the servo (1-4) and an angle from 0°-180°.
Example: hummingbird.setPositionServo(port: 1, angle: 90)

Method Signature: setRotationServo(port: Int, speed: Int)
Description: Sets a rotation servo to spin at a given speed. The method requires the port number of the servo (1-4) and a speed between -100 and 100. A speed of 0 turns the motor off.
Example: hummingbird.setRotationServo(port: 1, speed: 100)

Method Signature: playNote(note: Int, beats: Double)
Description: Plays a note using the buzzer on the Finch. The method requires an integer representing the note (32-135) and a number giving the number of beats (0-16). The number of beats can be a decimal number.
Example: hummingbird.playNote(note: 60, beats: 0.5)

Method Signature: setDisplay(pattern: Array<Int>)
Description: Sets the LED array of the micro:bit to display a pattern defined by an array. Each value in the array must be 0 (off) or 1 (on). The first five values in the array correspond to the five LEDs in the first row, the next five values to the second row, etc.
Example: hummingbird.setDisplay(pattern: [1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1])

Method Signature: printString(_ stringToPrint: String)
Description: Print a string on the micro:bit LED array. The string must have 15 or fewer characters and should contain only digits and English letters (upper or lower case).
Example: hummingbird.printString("hello")

Method Signature: stopAll()
Description: Stops all outputs. This includes the LED display for the micro:bit and all lights and motors for the Hummingbird.
Example: hummingbird.stopAll()

Method Signature: calibrateCompass()
Description: This function sends a Bluetooth command to calibrate the compass. When the Hummingbird receives this command, it will place dots on the micro:bit screen as it waits for you to tilt the Hummingbird in different directions. If the calibration is successful, you will then see a check on the micro:bit screen. Otherwise, you will see an X.
Example: hummingbird.calibrateCompass()

Hummingbird Sensor State

 

You can access the following variables within hummingbirdSensorState:

timestamp: Date Time of the sensor reading

batteryVoltage: Float Voltage of Hummingbird battery

isStale: Bool True when data is out of date

sensor1: HummingbirdSensor Value of the Hummingbird sensor in port 1. Each of the Hummingbird sensor ports supports different types of sensors, so this value has a data type HummingbirdSensor that computes all of the possibilities.

  • Use sensor1.distance: Int if you are using a distance sensor in port 1. This value is the distance in centimeters.
  • Use sensor1.dial: Int if you are using the dial. This value is between 0 and 100.
  • Use sensor1.light: Int if you are using the light sensor. This value is between 0 and 100.
  • Use sensor1.voltage: Double if you want the voltage at the port, which is between 0 and 3.3 V.

sensor2: HummingbirdSensor Value of the Hummingbird sensor in port 2. See the options for sensor1.

sensor3: HummingbirdSensor Value of the Hummingbird sensor in port 3. See the options for sensor1.

acceleration: Array<Double> Acceleration in m/s2 in the x, y, and z directions

magnetometer: Array<Double> Magnetometer readings in µT in the x, y, and z directions

compass: Int? Degrees from North; undefined when acceleration in the z direction is 0

buttonA: Bool True when button A is pressed

buttonB: Bool True when button B is pressed

shake: Bool True when Hummingbird is being shaken