Heather Parrett
Published

Obstacle Avoiding & Interactive Robot

This robot utilizes a sonar sensor in order to gather data on its surroundings and uses that received data to navigate the area around it!

IntermediateShowcase (no instructions)4 hours132
Obstacle Avoiding & Interactive Robot

Story

Read more

Code

The Code

JavaScript
function forward (num: number) {
    crickit.tank(speed, speed)
    basic.pause(num)
    stop()
}
function back (num: number) {
    crickit.tank(speed * -1, speed * -1)
    basic.pause(num)
    stop()
}
function left (num: number) {
    crickit.tank(speed, speed * -1)
    basic.pause(num)
    stop()
}
function right (num: number) {
    crickit.tank(speed * -1, speed)
    basic.pause(num)
    stop()
}
function stop () {
    crickit.tank(0, 0)
}
let ping = 0
let speed = 0
speed = 40
basic.showLeds(`
    # # . # #
    # # . # #
    . . . . .
    # . . . #
    . # # # .
    `)
basic.forever(function () {
    ping = sonar.ping(
    DigitalPin.P8,
    DigitalPin.P2,
    PingUnit.Centimeters
    )
    serial.writeValue("x", ping)
    if (ping < 10) {
        back(500)
        if (Math.randomRange(1, 10) < 5) {
            left(500)
        } else {
            right(500)
        }
    } else {
        forward(500)
    }
})

Credits

Heather Parrett

Heather Parrett

4 projects • 0 followers

Comments