bitty controller micro:bit code

Using MakeCode for 4tronix products

Bit:Bot uses pins as follows:

PIN Purpose Digital Values Analog (PWM) Values
0 Left motor speed 0=stopped, 1=full speed 0-1023 where 0=always off, 1023=always and (e.g.) 511=50% on
1 Right motor speed 0=stopped, 1=full speed 0-1023 where 0=always off, 1023=always and (e.g.) 511=50% on
8 Left motor direction 0=forwards, 1=backwards N/A
12 Right motor direction 0=forwards, 1=backwards N/A

JavaScript

MakeCode lets you work in either JavaScript or using graphical blocks. The JavaScript code for the touchpad controller interface used by bitty controller with a Bit:Bot appears below.

let CONTROL_TARGET_1_OFF = 0
let CONTROL_TARGET_1_ON = 0
let OTHER_CONTROL = 0
let p12_val = 0
let lft_rgt_lvl = 0
let p8_val = 0
let rgt = 0
let lft = 0
let bwd_fwd_lvl = 0
let pin12 = 0
let drive = 0
let pin1 = 0
let p1_val = 0
let bwd = 0
let pin8 = 0
let analog = 0
let p0_val = 0
let fwd = 0
let pin0 = 0
let connected = 0
let ev = 0
bluetooth.onBluetoothDisconnected(function () {
    connected = 0
    setConnectedIndicator2()
})
bluetooth.onBluetoothConnected(function () {
    connected = 1
    setConnectedIndicator2()
})
function setIndicatorActivated() {
    basic.clearScreen()
    led.plot(2, 2)
}
function resetPinValues() {
    pin0 = 0
    pin8 = 0
    pin1 = 0
    pin12 = 0
}
function stop() {
    fwd = 0
    bwd = 0
    resetPinValues()
    drive = 0
}
function writePinsAnalog() {
    p0_val = fwd * bwd_fwd_lvl * 102
    p1_val = fwd * bwd_fwd_lvl * 102
    if (fwd == 1) {
        if (lft == 1) {
            if (lft_rgt_lvl > -9 && lft_rgt_lvl < -2) {
                p1_val = fwd * bwd_fwd_lvl * 102
                p0_val = p1_val * (10 + lft_rgt_lvl) / 10
                p12_val = 0
                p8_val = 0
            } else {
                p1_val = fwd * bwd_fwd_lvl * 102
                p8_val = p1_val * (-8 - lft_rgt_lvl) / 2
                p12_val = 0
                p0_val = 0
            }
        } else if (rgt == 1) {
            if (lft_rgt_lvl < 9) {
                p0_val = fwd * bwd_fwd_lvl * 102
                p1_val = p0_val * (10 - lft_rgt_lvl) / 10
                p12_val = 0
                p8_val = 0
            } else {
                p0_val = fwd * bwd_fwd_lvl * 102
                p12_val = p0_val * (lft_rgt_lvl - 8) / 2
                p1_val = 0
                p8_val = 0
            }
        } else if (lft == 0 && rgt == 0) {
        	
        }
    }
    if (bwd == 1) {
        if (lft == 1) {
            if (lft_rgt_lvl > -9 && lft_rgt_lvl < -2) {
                p12_val = bwd * bwd_fwd_lvl * -102
                p8_val = p12_val * (10 + lft_rgt_lvl) / 10
                p1_val = 0
                p0_val = 0
            } else {
                p12_val = bwd * bwd_fwd_lvl * -102
                p0_val = p12_val * (-8 - lft_rgt_lvl) / 2
                p1_val = 0
                p8_val = 0
            }
        } else if (rgt == 1) {
            if (lft_rgt_lvl < 9) {
                p8_val = bwd * bwd_fwd_lvl * -102
                p12_val = p8_val * (10 - lft_rgt_lvl) / 10
                p1_val = 0
                p0_val = 0
            } else {
                p8_val = bwd * bwd_fwd_lvl * -102
                p1_val = p8_val * (lft_rgt_lvl - 8) / 2
                p0_val = 0
                p12_val = 0
            }
        } else if (lft == 0 && rgt == 0) {
            p8_val = bwd * bwd_fwd_lvl * -102
            p0_val = 0
            p12_val = bwd * bwd_fwd_lvl * -102
            p1_val = 0
        }
    }
    pins.analogWritePin(AnalogPin.P0, p0_val)
    pins.analogWritePin(AnalogPin.P1, p1_val)
    pins.analogWritePin(AnalogPin.P8, p8_val)
    pins.analogWritePin(AnalogPin.P12, p12_val)
}
function onTouchpadMotionEvent2() {
    // value[0] -ve=back +ve=forwards range:0-10 0=no
    // movement forwards or backwards 1 to 10=forward
    // speed (1=slowest, 10=fastest), -1 to -10 backwards
    // value[1] -ve=left +ve=right    range:0-10 0-2=no
    // movement left or right (deliberately less sensitive
    // than fwd/bwd control) +/- 3-8 gradually increasing
    // turn +/- 9-10 sharp turn
    let bufr = pins.createBuffer(2);
bwd_fwd = (control.eventValue() & 0x00ff);
lft_rgt = ((control.eventValue() & 0xff00) >> 8);
bufr.setNumber(NumberFormat.Int8LE, 0, bwd_fwd);
bufr.setNumber(NumberFormat.Int8LE, 1, lft_rgt);
bwd_fwd = bufr.getNumber(NumberFormat.Int8LE, 0);
lft_rgt = bufr.getNumber(NumberFormat.Int8LE, 1)
if (bwd_fwd == 0 && lft_rgt == 0) {
        return;
    }
    fwd = 0
    bwd = 0
    lft = 0
    rgt = 0
    if (bwd_fwd > 0) {
        fwd = 1
        bwd = 0
        bwd_fwd_lvl = bwd_fwd
    }
    if (bwd_fwd < 0) {
        bwd = 1
        fwd = 0
        bwd_fwd_lvl = bwd_fwd
    }
    if (lft_rgt < -2) {
        lft = 1
        rgt = 0
        lft_rgt_lvl = lft_rgt
    }
    if (lft_rgt > 2) {
        rgt = 1
        lft = 0
        lft_rgt_lvl = lft_rgt
    }
    setDirectionIndicators2()
    writePinsAnalog()
}
function setDeactivated() {
    basic.clearScreen()
    pins.analogWritePin(AnalogPin.P0, 0)
    pins.analogWritePin(AnalogPin.P1, 0)
    pins.analogWritePin(AnalogPin.P8, 0)
    pins.analogWritePin(AnalogPin.P12, 0)
}
function touchpadControl() {
    let ctrl1 = control.eventValue() & 0xFF;
let ctrl2 = control.eventValue() >> 8;
if (ctrl1 == 0 && ctrl2 == 1) {
        setIndicatorActivated()
        return;
    }
    if (ctrl1 == 0 && ctrl2 == 0) {
        bwd_fwd = 0
        lft_rgt = 0
        lft = 0
        rgt = 0
        stop()
        writePinsAnalog()
        setDeactivated()
        return;
    }
}
function setDirectionIndicators2() {
    basic.clearScreen()
    if (fwd > 0) {
        led.plot(2, 0)
        led.plot(2, 1)
    }
    if (bwd > 0) {
        led.plot(2, 3)
        led.plot(2, 4)
    }
    if (lft > 0) {
        led.plot(0, 2)
        led.plot(1, 2)
    }
    if (rgt > 0) {
        led.plot(4, 2)
        led.plot(3, 2)
    }
}
function setConnectedIndicator2() {
    if (connected == 1) {
        basic.showString("C")
    } else {
        basic.showString("D")
    }
}
function writePinsDigital2() {
    pins.digitalWritePin(DigitalPin.P0, pin0)
    pins.digitalWritePin(DigitalPin.P8, pin8)
    pins.digitalWritePin(DigitalPin.P1, pin1)
    pins.digitalWritePin(DigitalPin.P12, pin12)
}
// function resetPinValues() { pin0 = 0 pin8 = 0 pin1
// = 0 pin12 = 0 }
function stop2() {
    fwd = 0
    bwd = 0
    // resetPinValues()
    drive = 0
}
ev = 0
analog = 0
let TOUCHPAD_MOTION = 0
let TOUCHPAD_CONTROL = 0
let lft_rgt = 0
let bwd_fwd = 0
p0_val = 0
p1_val = 0
p8_val = 0
p12_val = 0
TOUCHPAD_MOTION = 9011
TOUCHPAD_CONTROL = 9012
TOUCHPAD_MOTION = 9011
TOUCHPAD_CONTROL = 9012
OTHER_CONTROL = 9013
CONTROL_TARGET_1_ON = 1
CONTROL_TARGET_1_OFF = 2
drive = 0
lft_rgt_lvl = 0
bwd_fwd_lvl = 0
lft_rgt = 0
bwd_fwd = 0
connected = 0
rgt = 0
lft = 0
bwd = 0
fwd = 0
pin12 = 0
pin1 = 0
pin8 = 0
pin0 = 0
basic.showString("BC-NP TP")
control.onEvent(TOUCHPAD_CONTROL, 0, touchpadControl)
control.onEvent(TOUCHPAD_MOTION, EventBusValue.MICROBIT_EVT_ANY, onTouchpadMotionEvent2)