Appendix B

JavaScript Recipes

THIS APPENDIX CONTAINS complete code listings for the programs detailed in Chapter 6, ‘JavaScript’, and Chapter 8, ‘The Wireless BBC micro:bit’, presented without comments to make them easier to type in and compare against. When a line of code would extend past the border of the page, a ↩ symbol is printed. When you see this symbol, continue to type the code without pressing the Enter or Return keys. If you’re not sure how a line of code should be entered, visit the website at www.wiley.com/go/bbcmicrobituserguide to download plain-text versions of each program; these can then be used for reference or even simply copy and pasted directly into the editors.

Chapter 6: Hello, World! (Non-looping)

basic.forever(() => {

})
basic.showString("Hello, World!")

Chapter 6: Hello, World! (Looping)

basic.forever(() => {
basic.showString("Hello, World!")
})

Chapter 6: Button Inputs (Single Button)

input.onButtonPressed(Button.A, () => {
basic.showIcon(IconNames.Happy)
})

Chapter 6: Button Inputs (Two Buttons)

input.onButtonPressed(Button.A, () => {
basic.showIcon(IconNames.Happy)
})
input.onButtonPressed(Button.B, () => {
basic.showIcon(IconNames.Sad)
})

Chapter 6: Touch Inputs

let touches = 0
input.onPinPressed(TouchPin.P0, () => {
touches += 1
basic.showNumber(touches)
})

Chapter 6: Temperature Sensor (No Formatting)

basic.forever(() => {
basic.showNumber(input.temperature())
})

Chapter 6: Temperature Sensor (with Formatting)

basic.forever(() => {
basic.showNumber(input.temperature())
basic.showString(" Celsius")
})

Chapter 6: Compass Sensor

basic.forever(() => {
basic.showString("Heading " +↩
input.compassHeading())
})

Chapter 6: Accelerometer Sensor (Single Icon)

input.onGesture(Gesture.Shake, () => {
basic.showIcon(IconNames.Surprised)
})

Chapter 6: Accelerometer Sensor (Two Icons)

input.onGesture(Gesture.Shake, () => {
basic.showIcon(IconNames.Surprised)
basic.pause(1000)
basic.showIcon(IconNames.Asleep)
})

Chapter 6: Accelerometer Sensor Data

basic.forever(() => {
basic.showString("X:" +↩
input.acceleration(Dimension.X))
basic.showString("Y:" +↩
input.acceleration(Dimension.Y))
basic.showString("Z:" +↩
input.acceleration(Dimension.Z))
})

Chapter 6: Fruit Catcher Game

let delay = 1000
let fruit: game.LedSprite = null
let player: game.LedSprite = game.createSprite(2, 4)
game.setScore(0)

basic.forever(() => {
fruit = game.createSprite(Math.random(5), 0)
basic.pause(delay)
while (fruit.get(LedSpriteProperty.Y) < 4) {
fruit.change(LedSpriteProperty.Y, 1)
basic.pause(delay)
}
if (player.isTouching(fruit)) {
game.addScore(1)
} else {
game.gameOver()
}
fruit.set(LedSpriteProperty.Brightness, 0)
delay = delay - delay / 10
})
input.onButtonPressed(Button.A, () => {
if (player.get(LedSpriteProperty.X) > 0) {
player.change(LedSpriteProperty.X, -1)
}
})

input.onButtonPressed(Button.B, () => {
if (player.get(LedSpriteProperty.X) < 4) {
player.change(LedSpriteProperty.X, 1)
}
})

Chapter 8: One-to-One Communication (BBC micro:bit A)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from A!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})

Chapter 8: One-to-One Communication (BBC micro:bit B)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from B!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})

Chapter 8: One-to-Many Communication (BBC micro:bit C)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from C!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})

Chapter 8: Radio Groups Communication (BBC micro:bit A)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from A!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})
input.onButtonPressed(Button.B, () => {
radio.setGroup(2)
basic.showString("Switching to Group 2")
})

Chapter 8: Radio Groups Communication (BBC micro:bit B)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from B!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})
input.onButtonPressed(Button.B, () => {
radio.setGroup(2)
basic.showString("Switching to Group 2")
})

Chapter 8: Radio Groups Communication (BBC micro:bit C)

radio.setGroup(1)
input.onButtonPressed(Button.A, () => {
radio.sendString("Hello from C!")
})
radio.onDataPacketReceived(({receivedString}) => {
basic.showString(receivedString)
})
input.onButtonPressed(Button.B, () => {
radio.setGroup(2)
basic.showString("Switching to Group 2")
})

Chapter 10: Reading a Button Input

basic.forever(() => {
while (pins.digitalReadPin(DigitalPin.P0) == 1) {
basic.showIcon(IconNames.Surprised)
}
basic.showIcon(IconNames.Asleep)
})

Chapter 10: Writing to an LED Output

basic.forever(() => {
pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digitalWritePin(DigitalPin.P1, 0)
basic.pause(1000)
})

Chapter 10: Fading an LED via PWM

pins.analogSetPeriod(AnalogPin.P1, 10000)
let index = 0
basic.forever(() => {
for (let index = 0; index <= 1023; index++) {
pins.analogWritePin(AnalogPin.P1, index)
basic.pause(1)
}
for (let index = 0; index <= 1023; index++) {
pins.analogWritePin(AnalogPin.P1, 1023 - index)
basic.pause(1)
}
})

Chapter 10: Reading an Analogue Input

basic.forever(() => {
basic.showNumber(pins.analogReadPin(AnalogPin.P2))
basic.showString("")
})

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.103.5