Para que este proyecto funcione, la micro:bit necesita el programa RECEPTOR. Recibe las órdenes desde la web y las ejecuta. Vale para micro:bit V1 y V2. Solo hay que hacerlo una vez:
let cola = ""
bluetooth.startUartService()
bluetooth.onBluetoothConnected(function () {
basic.showIcon(IconNames.Yes)
})
bluetooth.onBluetoothDisconnected(function () {
cola = ""
basic.showIcon(IconNames.No)
})
basic.showIcon(IconNames.Heart)
bluetooth.onUartDataReceived(serial.delimiters(Delimiters.NewLine), function () {
let linea = bluetooth.uartReadUntil(serial.delimiters(Delimiters.NewLine))
linea = linea.trim()
if (linea.indexOf("SEQ:") == 0) {
// secuencia completa: se encola y se ejecuta paso a paso
cola = linea.substr(4)
} else if (linea.length > 0) {
// comando suelto: se ejecuta al momento
ejecutar(linea.charAt(0))
}
})
// La cola se va ejecutando paso a paso (1 orden cada 800 ms)
basic.forever(function () {
if (cola.length > 0) {
let c = cola.charAt(0)
cola = cola.substr(1)
ejecutar(c)
basic.pause(800)
if (cola.length == 0) {
// fin de secuencia: carita contenta
basic.showIcon(IconNames.Happy)
}
}
})
function ejecutar(c: string) {
if (c == "F") {
basic.showArrow(ArrowNames.North)
// AQUI TU ROBOT: motores adelante
} else if (c == "B") {
basic.showArrow(ArrowNames.South)
// AQUI TU ROBOT: motores atras
} else if (c == "L") {
basic.showArrow(ArrowNames.West)
// AQUI TU ROBOT: girar a la izquierda
} else if (c == "R") {
basic.showArrow(ArrowNames.East)
// AQUI TU ROBOT: girar a la derecha
} else if (c == "S") {
basic.showIcon(IconNames.No)
// AQUI TU ROBOT: parar motores
} else if (c == "D") {
// Baile!
for (let i = 0; i < 4; i++) {
basic.showIcon(IconNames.Diamond)
basic.pause(120)
basic.showIcon(IconNames.SmallDiamond)
basic.pause(120)
}
basic.showIcon(IconNames.Happy)
// AQUI TU ROBOT: giro de baile
}
}