pxt-calliope/docs/fcp/ch1.md
Peli de Halleux 2b504d863d
Radiodocsupdate ()
* a few updates

* more updates

* reorder radio blocks

* more shuffling of new radio apis

* fixing hot or ocold

* more doc fixes

* more updates

* fixing docs issues

* more doc fixes

* restore docs errors

* missing packate

* renamed argument of callback

* mssing radio

* more odcs fixes

* lock turtle

* ignore docs for now
2018-10-15 15:32:09 -07:00

1.4 KiB

Chapter 1

Here are the code examples from Chapter 1 of Foundations of Computing and Programming: exploring the micro:bit. You can switch between the Blocks and JavaScript versions of each example, as well as run the example in the web browser, and retrieve a file to install on a micro:bit.

Figure 1.11

LED Screen with code:

basic.showIcon(IconNames.Square)
led.plot(2, 2)

Figure 1.12

Take action on button A/B pressed.

input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.Square)
})
input.onButtonPressed(Button.B, () => {
    led.plot(2, 2)
})

Figure 1.13

Take action on button A/B pressed, as well as shaking the micro:bit.

input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.Square)
})
input.onButtonPressed(Button.B, () => {
    led.plot(2, 2)
})
input.onGesture(Gesture.Shake, () => {
    basic.clearScreen()
}) 

Figure 1.15

Program to send signals over pin P0.

basic.forever(() => {
    pins.digitalWritePin(DigitalPin.P0, 1)
    basic.pause(1000)
    pins.servoWritePin(AnalogPin.P0, 180)
    basic.pause(1000)
})

Figure 1.17

Communicating using radio.

input.onButtonPressed(Button.A, () => {
    radio.sendString("A")
})
input.onButtonPressed(Button.B, () => {
    radio.sendString("B")
})
radio.onReceivedNumber(({ receivedString }) => {
    basic.showString(receivedString)
})