Overview

This micro:bit reads morse code inputs and sends them back to the user, translated into letters!

Code

let currentChar: int8[] = []
let dictionary = [
[0, 1],
[
1,
0,
0,
0
],
[
1,
0,
1,
0
],
[1, 0, 0],
[0],
[
0,
0,
1,
0
],
[1, 1, 0],
[
0,
0,
0,
0
],
[0, 0],
[
0,
1,
1,
1
],
[1, 0, 1],
[
0,
1,
0,
0
],
[1, 1],
[1, 0],
[1, 1, 1],
[
0,
1,
1,
0
],
[
1,
1,
0,
1
],
[0, 1, 0],
[0, 0, 0],
[1],
[0, 0, 1],
[
0,
0,
0,
1
],
[0, 1, 1],
[
1,
0,
0,
1
],
[
1,
0,
1,
1
],
[
1,
1,
0,
0
]
]
let alphabet = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z"
]
loops.everyInterval(50, function () {
input.onButtonPressed(Button.A, function () {
music.playTone(262, music.beat(BeatFraction.Half))
currentChar.push(0)
})
input.onButtonPressed(Button.B, function () {
music.playTone(262, music.beat(BeatFraction.Whole))
currentChar.push(1)
})
input.onButtonPressed(Button.AB, function () {
for (let i = 0; i < dictionary.length; i++) {
if (JSON.stringify(dictionary[i]) === JSON.stringify(currentChar)) {
basic.showString(alphabet[i])
}
}
currentChar = []
})
})