Morse Chat
{Introducing Sky }
π· Meet Sky, the pig! Sky can only communicate using Morse code.
Luckily, you can use your micro:bit with sound to talk to Sky π
{Setup}
Letβs start by making a way to send Morse code messages.
~hint What is Morse code? π€·π½
Morse code is an alphabet composed of dots (short signals) and dashes (long signals). The message βHi there!β is ββ¦. .. - β¦. . .-. . -.-.ββ in Morse code.
hint~
β From the ||input:Input||
category in the toolbox, drag an ||input:on logo [pressed]||
container into to your workspace.
β From the ||radio:Radio||
category, get ||radio:radio send number [0]||
and snap it into your empty ||input(noclick):on logo [pressed]||
container.
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
radio.sendNumber(0)
})
{Sending different messages pt. 1}
β From ||input:Input||
, grab another ||input:on logo [pressed]||
container and add it to your workspace.
π‘ This container is greyed out because it matches another. Letβs change that!
β On the greyed-out ||input(noclick):on logo [pressed]||
container, click on the pressed
dropdown and set it to ||input(noclick):long pressed||
.
// @highlight
input.onLogoEvent(TouchButtonEvent.LongPressed, function () {
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
radio.sendNumber(0)
})
{Sending different messages pt. 2}
β From the ||radio:Radio||
category, get a ||radio:radio send number [0]||
block and snap it into your empty ||input(noclick):on logo [long pressed]||
container.
β Set the number to be 1
.
input.onLogoEvent(TouchButtonEvent.LongPressed, function () {
// @highlight
radio.sendNumber(1)
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
radio.sendNumber(0)
})
{Receiving different messages}
To ensure Sky gets the right message, we will use an if then / else conditional statement.
β From ||radio:Radio||
, find the ||radio:on radio received [receivedNumber]||
container and add it to your workspace.
β From ||logic:Logic||
, grab an ||logic:if <true> then / else||
statement and snap it into your new ||radio(noclick):on radio received [receivedNumber]||
container.
β Go back to the ||logic:Logic||
category, grab ||logic:<[0] [=] [0]>||
, and click it in to replace the ||logic(noclick):<true>||
argument in your ||logic(noclick):if <true> then / else||
statement.
radio.onReceivedNumber(function (receivedNumber) {
// @highlight
if (0 == 0) {
} else {
}
})
{Conditioning on the input}
β From your ||radio:on radio received [receivedNumber]||
container, grab the receivedNumber
input and drag out a copy.
β Use your copy of receivedNumber
to replace the [0]
on the left side of ||logic(noclick):<[0] [=] [0]>||
.
radio.onReceivedNumber(function (receivedNumber) {
// @highlight
if (receivedNumber == 0) {
} else {
}
})
{Displaying a message pt. 1}
β We want to display a dash if the logo is long pressed. From ||basic:Basic||
, grab ||basic:show leds||
and snap it into the empty bottom container of your ||logic(noclick):if then / else||
statement.
β Turn on 3 LEDs in a row to be a dash: -
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
} else {
// @highlight
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
}
})
{Playing a sound pt. 1}
β From the ||music:Music||
category, grab a ||music:play tone [Middle C] for [1 beat] [until done]||
block and snap it at the end of the bottom container in your ||logic(noclick):if then / else||
statement.
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
// @highlight
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
})
{Displaying a message pt. 2}
β We want to display a dot if the logo is pressed. From ||basic:Basic||
, grab another ||basic:show leds||
and snap it into the top container of your ||logic(noclick):if then / else||
statement.
β Turn on a single LED to make a dot: .
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
// @highlight
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
})
{Playing a sound pt. 2}
β From the ||music:Music||
category, grab ||music:play tone [Middle C] for [1 beat] [until done]||
and snap it in at the end of the top container in your ||logic(noclick):if then / else||
statement.
β Dots are shorter than dashes! Set the tone to play for 1/4 beat
.
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
// @highlight
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
})
{Clearing the screens}
β From ||basic:Basic||
, find ||basic:clear screen||
and snap it in at the very bottom of your ||radio(noclick):on radio received [receivedNumber]||
container.
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
// @highlight
basic.clearScreen()
})
{Testing in the simulator - Connect!}
Test what youβve created. Remember to turn your sound on!
β Touch the gold micro:bit logo at the top of your micro:bit on the simulator. Youβll notice that a second micro:bit appears. This is the micro:bit for Sky π
π‘ If your screen is too small, you might not be able to see it.
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.clearScreen()
})
input.onLogoEvent(TouchButtonEvent.LongPressed, function () {
radio.sendNumber(1)
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
radio.sendNumber(0)
})
{Testing in the simulator - Send message}
β Touch the logo again to send messages to Sky π
Press to send a dot.
Long press (count to 3!) to send a dash.
β If you have multiple micro:bits with sound (they have shiny gold logos at the top), download this code and try it out!
radio.onReceivedNumber(function (receivedNumber) {
if (receivedNumber == 0) {
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Quarter)), music.PlaybackMode.UntilDone)
} else {
basic.showLeds(`
. . . . .
. . . . .
. # # # .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.clearScreen()
})
input.onLogoEvent(TouchButtonEvent.LongPressed, function () {
radio.sendNumber(1)
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
radio.sendNumber(0)
})