pxt-ev3/docs/reference/brick/battery-level.md
Peli de Halleux e94ac6f6f1
renaming brick.setLight to brick.setStatusLight (#307)
* renaming brick.setLight to brick.setStatusLight

* updated docs
2018-02-06 22:18:39 -08:00

920 B

battery Level

Return the current battery level.

brick.batteryLevel();

Returns

  • a number which is the current charge level of the brick's battery. This is a percentage of total charge left in the battery.

Example

Show the battery level percentage on the screen. Also, show a green light if the battery level is above 15%. If the battery level is below 15% but above 5%, show a orange light. But, if the battery level is below 5%, show a pulsing red light.

let battery = 0;
loops.forever(function() {
    brick.showString("Battery level:", 1)
    brick.showNumber(battery, 2)
    battery = brick.batteryLevel();
    if (battery > 15)
    {
        brick.setStatusLight(StatusLight.Green);
    } else if (battery > 5) {
        brick.setStatusLight(StatusLight.Orange);
    } else {
        brick.setStatusLight(StatusLight.RedPulse)
    }
    loops.pause(30000)
})