pxt-calliope/docs/projects/wallet/code.md
Peli de Halleux 1d47b4de0d Lightmonster (#346)
* fixed boardname issues

* refactored servo info

* coffee cup monster lesson skeleton

* display event source id / value in advanced section

* normalized project structure

* updated project name

* Update projects.md
2017-01-31 08:29:25 -08:00

1.4 KiB

Code

Simple animation

Let's start by using a combination of forever and show leds to create animation:

basic.forever(() => {
    basic.showLeds(`
        # # . # #
        # # . # #
        . # # # .
        . # . # .
        . # . # .
        `)
    basic.showLeds(`
        . . # . .
        . . # . .
        # . . . #
        # . # . #
        # . # . #
        `)
})

Download this code to your @boardname@ and try it out.

Turn off animation in the pocket

If the wallet is in your pocket, you should turn off the LEDs to save energy.

How do we know that the wallet is in the pocket? It is really dark in there... We can use the light level to detect this!

Using an if statement, we can test if the level of light is sufficient to turn on the screen. Otherwise, we turn off the screen for a few second to save energy.

basic.forever(() => {
    if (input.lightLevel() > 16) {
        basic.showLeds(`
            # # . # #
            # # . # #
            . # # # .
            . # . # .
            . # . # .
            `)
        basic.showLeds(`
            . . # . .
            . . # . .
            # . . . #
            # . # . #
            # . # . #
            `)
    } else {        
        // clear screen and wait
        basic.clearScreen()
        basic.pause(3000)
    }
})