Philip Cua
Published

Competitive Math Training Thru Microbits (Master Version)

A Master Microbit(Teacher) issues out math equations to Client Microbits(Students). Students then compete to be first w/ the correct answer.

IntermediateFull instructions provided1 hour60
Competitive Math Training Thru Microbits (Master Version)

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
Kitronik MI:Power Board
×1
Kitronik MI:pro Protector Case
×1

Story

Read more

Code

A Microbit-Based Competitive Math Training Game (Master Version)

JavaScript
The Master program of a Microbit-based competitive math training game.
let EqString = ""
let Answer = 0
let GotCorrect = 0
let CorrectID = 0
let RadioCorrectID = 0
let Y = 0
let ReceiveSw = 0
let RadioEqStr = 0
let ShowAnsSw = 0
let X = 0
let PressButtA = 0
let Lvl = 0
let Eqn = 0
let ShowEqStr = 0
let serial2 = 0
input.onButtonPressed(Button.A, () => {
    if (PressButtA == 0) {
        if (Lvl == 0) {
            X = Math.random(16)
            Y = Math.random(16)
        } else {
            X = Math.random(41)
            Y = Math.random(40)
        }
        X += 10
        Y += 10
        Answer = X + Y
        Eqn = X * 100
        Eqn += Y
        EqString = "" + X + "+" + Y
        PressButtA = 1
        ReceiveSw = 1
        RadioEqStr = 1
    }
    ShowEqStr = 1
    ShowAnsSw = 0
})
input.onButtonPressed(Button.B, () => {
    if (PressButtA == 1) {
        ShowEqStr = 0
        ShowAnsSw = 1
    }
})
radio.onDataPacketReceived( ({ receivedString: name, receivedNumber: value, serial: serial2 }) =>  {
    if (name == "ANS") {
        if (ReceiveSw == 1) {
            if (GotCorrect == 0) {
                if (Answer == value) {
                    CorrectID = serial2
                    ReceiveSw = 0
                    GotCorrect = 1
                    RadioCorrectID = 1
                    RadioEqStr = 0
                    ShowAnsSw = 0
                    ShowEqStr = 0
                    for (let i = 0; i < 4; i++) {
                        basic.clearScreen()
                        basic.showIcon(IconNames.Yes)
                    }
                    music.beginMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once)
                }
            }
        }
    }
})
input.onButtonPressed(Button.AB, () => {
    for (let i = 0; i < 4; i++) {
        basic.clearScreen()
    }
    Eqn = 0
    ReceiveSw = 0
    GotCorrect = 0
    X = 0
    Y = 0
    RadioEqStr = 0
    RadioCorrectID = 0
    ShowEqStr = 0
    PressButtA = 0
    ShowAnsSw = 0
    Answer = 0
    basic.clearScreen()
})
input.onGesture(Gesture.Shake, () => {
    if (Lvl == 0) {
        Lvl = 1
        basic.clearScreen()
        basic.showLeds(`
            # . . # .
            # . # # .
            # . . # .
            # . . # .
            # # . # .
            `)
        basic.pause(5000)
    } else {
        Lvl = 0
        basic.clearScreen()
        basic.showLeds(`
            # . . # .
            # . # . #
            # . # . #
            # . # . #
            # # . # .
            `)
        basic.pause(5000)
    }
})
radio.setGroup(23)
radio.setTransmitSerialNumber(true)
Eqn = 0
ReceiveSw = 0
GotCorrect = 0
X = 0
Y = 0
RadioEqStr = 0
RadioCorrectID = 0
ShowEqStr = 0
PressButtA = 0
ShowAnsSw = 0
Answer = 0
EqString = ""
Lvl = 1
basic.forever(() => {
    if (RadioEqStr == 1) {
        radio.sendValue("PLUS", Eqn)
    }
    if (RadioCorrectID == 1) {
        radio.sendValue("CID", CorrectID)
    }
    if (ShowEqStr == 1) {
        basic.clearScreen()
        basic.showString(EqString)
        basic.pause(500)
    }
    if (ShowAnsSw == 1) {
        basic.clearScreen()
        basic.showNumber(Answer)
        basic.pause(500)
    }
})

Credits

Philip Cua

Philip Cua

4 projects • 0 followers

Comments

Add projectSign up / LoginAbout BBC micro:bit