DIY Electronics

Simple electronics as a hobby

Micro:bit as slot-machine

The Microbit v1 has an extension board, a 16x2 LCD display, a boost-converter, a push-button and a speaker box connected to it.
The game loaded onto the Microbit is a slot-machine. When powering up the Microbit a short tune is heard. The LEDs are turned on at the top row. After pressing the button the LED lights are turned on and off suggesting rotating wheels. They randomly come to a hold at row 2,3,4 or 5.
4 or 5 LEDs on a single row gives you extra credits. Every turn costs one credit and the number is shown on the LCD.





Technical aspects

The microbit uses the I2C_LCD1602 (library extension) to connect the LCD display using only 4 wires (Vcc(+), Gnd(-), SDA(data), SCL (clock)). SDA and SCL are found on the extension board, pin 20 (blue wire) and 19 (green wire). The LCD needs 5 volts to work properly, but the Microbit offers only 3.3 volts. A boost-converter is added to turn 3.3v into 5v. It has a Vin (orange), a Vout (red) and a common ground (yellow).
The speaker box has a simple audio amplifier and a speaker. It needs a 9-volt battery to operate. It is connected to pin 0 (white) and ground (purple). The push button is connected to pin 1 and 3.3volt. In the program pin 1 is pulled down, meaning that applying voltage will trigger a response.

Code for slot-machine in Javascript

It's the maximum code that microbit can handle.
You need to add this url in the extensions search box:
https://github.com/makecode-extensions/i2cLCD1602
It will add code & blocks needed to interface with LCD display 16x2 using i2c protocol.
Copy and paste the Javascript code in the microbit editor and change to blocks if you prefer blocks.

function CheckIfWin () {
    if (Slot1.get(LedSpriteProperty.Y) == Slot2.get(LedSpriteProperty.Y) && (Slot2.get(LedSpriteProperty.Y) == Slot3.get(LedSpriteProperty.Y) && (Slot3.get(LedSpriteProperty.Y) == Slot4.get(LedSpriteProperty.Y) && Slot4.get(LedSpriteProperty.Y) == Slot5.get(LedSpriteProperty.Y)))) {
        wins = 10
        show_credit()
    } else if (Slot2.get(LedSpriteProperty.Y) == Slot3.get(LedSpriteProperty.Y) && (Slot3.get(LedSpriteProperty.Y) == Slot4.get(LedSpriteProperty.Y) && Slot4.get(LedSpriteProperty.Y) == Slot5.get(LedSpriteProperty.Y))) {
        wins = 4
        show_credit()
    } else if (Slot1.get(LedSpriteProperty.Y) == Slot3.get(LedSpriteProperty.Y) && Slot3.get(LedSpriteProperty.Y) == Slot4.get(LedSpriteProperty.Y) && Slot4.get(LedSpriteProperty.Y) == Slot5.get(LedSpriteProperty.Y)) {
        wins = 4
        show_credit()
    } else if (Slot1.get(LedSpriteProperty.Y) == Slot2.get(LedSpriteProperty.Y) && (Slot2.get(LedSpriteProperty.Y) == Slot4.get(LedSpriteProperty.Y) && Slot4.get(LedSpriteProperty.Y) == Slot5.get(LedSpriteProperty.Y))) {
        wins = 4
        show_credit()
    } else if (Slot1.get(LedSpriteProperty.Y) == Slot2.get(LedSpriteProperty.Y) && (Slot2.get(LedSpriteProperty.Y) == Slot3.get(LedSpriteProperty.Y) && Slot3.get(LedSpriteProperty.Y) == Slot5.get(LedSpriteProperty.Y))) {
        wins = 4
        show_credit()
    } else if (Slot1.get(LedSpriteProperty.Y) == Slot2.get(LedSpriteProperty.Y) && (Slot2.get(LedSpriteProperty.Y) == Slot3.get(LedSpriteProperty.Y) && Slot3.get(LedSpriteProperty.Y) == Slot4.get(LedSpriteProperty.Y))) {
        wins = 4
        show_credit()
    } else {
        music.playTone(330, music.beat(BeatFraction.Quarter))
        music.playTone(262, music.beat(BeatFraction.Quarter))
        music.playTone(247, music.beat(BeatFraction.Quarter))
        music.playTone(220, music.beat(BeatFraction.Half))
        ready = true
    }
}
function show_credit () {
    music.startMelody(music.builtInMelody(Melodies.Ringtone), MelodyOptions.Once)
    Credit += wins
    I2C_LCD1602.ShowString("Credits: " + convertToText(Credit) + "   ", 0, 0)
    I2C_LCD1602.ShowString("You win: " + convertToText(wins), 0, 1)
    basic.pause(1000)
    ready = true
}
let Run5 = false
let Run4 = false
let Run3 = false
let Run2 = false
let Run1 = false
let Slot5: game.LedSprite = null
let Slot4: game.LedSprite = null
let Slot3: game.LedSprite = null
let Slot2: game.LedSprite = null
let Slot1: game.LedSprite = null
let Credit = 0
let ready = false
let wins = 0
pins.setPull(DigitalPin.P1, PinPullMode.PullDown)
wins = 0
let once = false
ready = true
Credit = 20
Slot1 = game.createSprite(0, 0)
Slot2 = game.createSprite(1, 0)
Slot3 = game.createSprite(2, 0)
Slot4 = game.createSprite(3, 0)
Slot5 = game.createSprite(4, 0)
I2C_LCD1602.LcdInit(39)
I2C_LCD1602.BacklightOn()
I2C_LCD1602.ShowString("Credits: " + convertToText(Credit), 0, 0)
music.startMelody(music.builtInMelody(Melodies.PowerUp), MelodyOptions.Once)
basic.forever(function () {
    if (Run1) {
        for (let index = 0; index < randint(31, 33); index++) {
            basic.pause(40)
            Slot1.change(LedSpriteProperty.Y, 1)
            if (Slot1.get(LedSpriteProperty.Y) == 4) {
                basic.pause(40)
                Slot1.set(LedSpriteProperty.Y, 1)
            }
        }
        Run1 = false
    }
})
basic.forever(function () {
    if (once) {
        once = false
        if (Credit <= 0) {
            I2C_LCD1602.ShowString("GAME OVER....   ", 0, 0)
        } else {
            Credit += -1
            Run1 = true
            Run2 = true
            Run3 = true
            Run4 = true
            Run5 = true
            I2C_LCD1602.ShowString("Credits: " + convertToText(Credit) + "   ", 0, 0)
            I2C_LCD1602.ShowString("                ", 0, 1)
        }
        basic.pause(10)
    }
    if (pins.digitalReadPin(DigitalPin.P1) == 1 && ready == true) {
        once = true
        ready = false
    }
})
basic.forever(function () {
    if (Run5) {
        for (let index = 0; index < randint(43, 45); index++) {
            basic.pause(60)
            Slot5.change(LedSpriteProperty.Y, 1)
            if (Slot5.get(LedSpriteProperty.Y) == 4) {
                basic.pause(60)
                Slot5.set(LedSpriteProperty.Y, 1)
            }
        }
        Run5 = false
        CheckIfWin()
    }
})
basic.forever(function () {
    if (Run2) {
        for (let index = 0; index < randint(34, 36); index++) {
            basic.pause(45)
            Slot2.change(LedSpriteProperty.Y, 1)
            if (Slot2.get(LedSpriteProperty.Y) == 4) {
                basic.pause(45)
                Slot2.set(LedSpriteProperty.Y, 1)
            }
        }
        Run2 = false
    }
})
basic.forever(function () {
    if (Run4) {
        for (let index = 0; index < randint(40, 42); index++) {
            basic.pause(55)
            Slot4.change(LedSpriteProperty.Y, 1)
            if (Slot4.get(LedSpriteProperty.Y) == 4) {
                basic.pause(55)
                Slot4.set(LedSpriteProperty.Y, 1)
            }
        }
        Run4 = false
    }
})
basic.forever(function () {
    if (Run3) {
        for (let index = 0; index < randint(37, 39); index++) {
            basic.pause(50)
            Slot3.change(LedSpriteProperty.Y, 1)
            if (Slot3.get(LedSpriteProperty.Y) == 4) {
                basic.pause(50)
                Slot3.set(LedSpriteProperty.Y, 1)
            }
        }
        Run3 = false
    }
})

Top