|
| 1 | +# Chapter 1 |
| 2 | + |
| 3 | +Here are the code examples from Chapter 1 |
| 4 | +of [Foundations of Computing and Programming: exploring the micro:bit](../fcp). |
| 5 | +You can switch between the Blocks and JavaScript versions of each example, |
| 6 | +as well as run the example in the web browser, and retrieve a file to |
| 7 | +install on a micro:bit. |
| 8 | + |
| 9 | +## Figure 1.11 |
| 10 | +LED Screen with code: |
| 11 | +```blocks |
| 12 | +basic.showIcon(IconNames.Square) |
| 13 | +led.plot(2, 2) |
| 14 | +``` |
| 15 | + |
| 16 | +## Figure 1.12 |
| 17 | + |
| 18 | +Take action on button A/B pressed. |
| 19 | +```blocks |
| 20 | +input.onButtonPressed(Button.A, () => { |
| 21 | + basic.showIcon(IconNames.Square) |
| 22 | +}) |
| 23 | +input.onButtonPressed(Button.B, () => { |
| 24 | + led.plot(2, 2) |
| 25 | +}) |
| 26 | +``` |
| 27 | + |
| 28 | +## Figure 1.13 |
| 29 | + |
| 30 | +Take action on button A/B pressed, as |
| 31 | +well as shaking the micro:bit. |
| 32 | +```blocks |
| 33 | +input.onButtonPressed(Button.A, () => { |
| 34 | + basic.showIcon(IconNames.Square) |
| 35 | +}) |
| 36 | +input.onButtonPressed(Button.B, () => { |
| 37 | + led.plot(2, 2) |
| 38 | +}) |
| 39 | +input.onGesture(Gesture.Shake, () => { |
| 40 | + basic.clearScreen() |
| 41 | +}) |
| 42 | +``` |
| 43 | + |
| 44 | +## Figure 1.15 |
| 45 | + |
| 46 | +Program to send signals over pin P0. |
| 47 | +```blocks |
| 48 | +basic.forever(() => { |
| 49 | + pins.digitalWritePin(DigitalPin.P0, 1) |
| 50 | + basic.pause(1000) |
| 51 | + pins.servoWritePin(AnalogPin.P0, 180) |
| 52 | + basic.pause(1000) |
| 53 | +}) |
| 54 | +``` |
| 55 | + |
| 56 | +## Figure 1.17 |
| 57 | + |
| 58 | +Communicating using radio. |
| 59 | +```blocks |
| 60 | +input.onButtonPressed(Button.A, () => { |
| 61 | + radio.sendString("A") |
| 62 | +}) |
| 63 | +input.onButtonPressed(Button.B, () => { |
| 64 | + radio.sendString("B") |
| 65 | +}) |
| 66 | +radio.onDataPacketReceived(({receivedString}) => { |
| 67 | + basic.showString(receivedString) |
| 68 | +}) |
| 69 | +``` |
0 commit comments