MB6: Sending a Message
MAKECODE
Diagram
Block Code
*Note: it is recommended to use javascript for this code to guarantee that there are no errors when creating the text list used to store the letters of the alphabet. The block code can be unreliable when it defines the type of lsit it is creating.
The code above creates an alphabet that allows players to select letters in order to build a message they will send to their peer(s).
On start the variable to be used in the program are defined and set. The String message is for storing the message we will be sending on shake and the number variable letter will be used to store the position of the alphabet the player is at. Variables alphabetStr (String) and alphabetArr (List) are used to store the letters of the alphabet first as a string and use each character (each letter) from that String as a value to put into a list. Inserting each letter as an entry into a List will allow for the selection of individual letters when bulding the secrete message.
When pressing the A and B buttons, the letters variable will be incremented in order to display the next or previous letter of the alphabet. In order to go back to the beginning of the alphabet once the end is reached, an if statement is used. This if statement will set the letter variable back to 0 after it reaches the end of the list and will allow for easier scrolling through the alphabet. When A+B is pressed it will select the current letter and add it to the message being built.
A forever loop is used to ensure that the message being sent does not exceed the maximum String length set by the micro:bit, which is 19, forcing the current 19 character message to be sent.
Code avaialble here
JavaScript
/*
*initialize all the variables on start
*and create an array containing the letters of the alphabet
*/
let message = ""
let letter = 0
let alphabetArr: string[] = []
let alphabetStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabetArr = []
letter = 0
message = ""
/* create an array containing the letters of the alphabet
*by looping through a string of the alphabet */
for (let index = 0; index <= alphabetStr.length - 1; index++) {
alphabetArr.push(alphabetStr.charAt(index))
}
/* scroll through the letters on A or B presses and choose letters with A+B */
input.onButtonPressed(Button.A, function () {
if (letter == 0) {
letter = alphabetArr.length - 1
} else {
letter += -1
}
basic.showString("" + alphabetArr[letter])
})
/* B button */
input.onButtonPressed(Button.B, function () {
if (letter == alphabetArr.length - 1) {
letter = 0
} else {
letter += 1
}
basic.showString("" + alphabetArr[letter])
})
/* A+B Button */
input.onButtonPressed(Button.AB, function () {
message = "" + message + alphabetArr[letter]
})
/*shake to send msg*/
input.onGesture(Gesture.Shake, function () {
radio.sendString(message)
basic.pause(1000)
message = ""
})
/**receive and display msg*/
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString)
})
basic.showString("" + alphabetArr[letter])
/*ensure msg doesn't exceed 19 char limit set by micro:bit*/
basic.forever(function () {
if (message.length == 19) {
basic.showString("Shake to send your message!")
}
})
Code avaialble here
The Game
Secret Message | 2 or more players | 1 micro:bit per player
Players each start with one micro:bit each loaded with the Send Message code above. Players can spend time choosing the letters to build their secret word or message using the A+B buttons. Then they can shake the micro:bit to send their message to their friend.