Distance Sensor Microbit

Distance sensor - Microbit 

Purpose:
This program measures 1 inch of from distance sensor and is not entirely accurate and when it is 2 cm 5 cm or 10 cm it preforms a Pokemon move.

Materials needed:
This project uses the BBC Microbit, Microbit Programming interface,Breadboard,Breadboard connector + Pin setup,5 VT battery,Connection wires, and the Microbit distance sensor.

Procedure:
Set up the circuit, Write the code, Download to Microbit,Run.`


Circuit:
`
THE BLACK WIRE IS CONNECTED TO A 5 V BATTERY


Code Snippet:



















---------------------

Java code snippet


The code is in JavaScript Microbit Code
let Distance4 = 0
let Distance3 = 0
let Distance2 = 0
let Distance = 0
let echoTime = 0
basic.forever(() => {
    echoTime = 0
    // echoTime = sonar.ping( DigitalPin.P0,
    // DigitalPin.P1, PingUnit.Centimeters ) send pulse
    pins.digitalWritePin(DigitalPin.P0, 0)
    control.waitMicros(2)
    pins.digitalWritePin(DigitalPin.P0, 1)
    control.waitMicros(10)
    pins.digitalWritePin(DigitalPin.P0, 0)
    // read pulse

    Distance = pins.pulseIn(DigitalPin.P1, PulseValue.High) / 58
    // Distance = echoTime / 58
    basic.showString("" + Distance)
    Distance2 = 2
    Distance3 = 5
    Distance4 = 10

    if (Distance == Distance2) {
        basic.showString("Use Close Combat|||||||||||||")
        for (let i = 0; i < 4; i++) {
            basic.showLeds(`
                # # # . .
                # # # . .
                . . . . .
                # # # # #
                # # # # #
                `)
            basic.showLeds(`
                # # # # #
                # # # # #
                . . . . .
                # # # . .
                # # # . .
                `)
        }
        basic.pause(300)
    }
    if (Distance == Distance3) {
        basic.showString("Use HIGH JUMP KICK||||||||||||")
        basic.showLeds(`
            . . # . .
            . # # . .
            . # # . .
            # # . . .
            # . . . .
            `)
        basic.showLeds(`
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            . . . . .
            `)
        basic.showLeds(`
            . # # . .
            . . # # .
            . . # # .
            . . . # #
            . . . . #
            `)
        basic.showLeds(`
            . . # # #
            . # # # #
            # # # # #
            # # # # #
            # # # # #
            `)
        basic.pause(300)
    }
    if (Distance == Distance2) {
        basic.showString("Use HYPERBEAM|||||||||||||")
        basic.showLeds(`
            . . . . .
            # # # # #
            # # # # #
            # # # # #
            . . . . .
            `)
        basic.showLeds(`
            # # # . .
            # # # # .
            # # # # #
            # # # # .
            # # # . .
            `)
        basic.pause(300)
    }
})


Result:
It will show the amount of centimeters with the exception of 2 5 and 10 which will show a Pokemon move Close Combat, High Jump Kick OR Hyper Beam.

Demo:


Comments

Popular posts from this blog

Gears and basic concepts

Gears

Courses Offered