Micro:bit krúžok: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
Riadok 46: | Riadok 46: | ||
}) | }) | ||
</source> | |||
<source lang="javascript"> | |||
input.onButtonPressed(Button.A, () => { | |||
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 100) | |||
basic.showLeds(` | |||
. . . . . | |||
. . # . . | |||
. . # . . | |||
. . . . . | |||
. . . . . | |||
`) | |||
}) | |||
input.onButtonPressed(Button.B, () => { | |||
motobit.enable(MotorPower.On) | |||
basic.showLeds(` | |||
# . . . . | |||
. # . . . | |||
. . # . . | |||
. . . # . | |||
. . . . # | |||
`) | |||
}) | |||
function ON() { | |||
motobit.enable(MotorPower.On) | |||
} | |||
function OFF() { | |||
motobit.enable(MotorPower.Off) | |||
} | |||
function 前進() { | |||
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 30) | |||
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 30) | |||
} | |||
radio.onDataPacketReceived( ({ receivedString }) => { | |||
if (receivedString == "ON") { | |||
ON() | |||
} else if (receivedString == "OFF") { | |||
OFF() | |||
} else if (receivedString == "FORWARD") { | |||
前進() | |||
} else if (receivedString == "BACK") { | |||
後進() | |||
} else if (receivedString == "RIGHT") { | |||
右折() | |||
} else if (receivedString == "LEFT") { | |||
左折() | |||
} else { | |||
OFF() | |||
} | |||
}) | |||
function 左折() { | |||
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 30) | |||
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 30) | |||
} | |||
function 右折() { | |||
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 30) | |||
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 30) | |||
} | |||
function 後進() { | |||
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 30) | |||
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 30) | |||
} | |||
radio.setGroup(1) | |||
basic.showLeds(` | |||
. . # . . | |||
. . . . . | |||
# # # # # | |||
. . . . . | |||
. . # . . | |||
`) | |||
</source> | </source> |
Verzia z 13:59, 7. marec 2018
7. marca 2018
- Robili sme toto..
Ovládač na Moto:bit
radio.setGroup(1)
basic.forever(() => {
if (input.buttonIsPressed(Button.AB)) {
radio.sendString("FORWARD")
basic.showLeds(`
. . # . .
. # # # .
# . # . #
. . # . .
. . # . .
`)
} else if (input.buttonIsPressed(Button.B)) {
radio.sendString("LEFT")
basic.showLeds(`
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .
`)
} else if (input.buttonIsPressed(Button.A)) {
radio.sendString("RIGHT")
basic.showLeds(`
. . # . .
. . . # .
. # # # #
. . . # .
. . # . .
`)
} else {
radio.sendString("OFF")
basic.showLeds(`
. # # # .
# # . # #
# . # . #
# # . # #
. # # # .
`)
}
})
input.onButtonPressed(Button.A, () => {
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 100)
basic.showLeds(`
. . . . .
. . # . .
. . # . .
. . . . .
. . . . .
`)
})
input.onButtonPressed(Button.B, () => {
motobit.enable(MotorPower.On)
basic.showLeds(`
# . . . .
. # . . .
. . # . .
. . . # .
. . . . #
`)
})
function ON() {
motobit.enable(MotorPower.On)
}
function OFF() {
motobit.enable(MotorPower.Off)
}
function 前進() {
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 30)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 30)
}
radio.onDataPacketReceived( ({ receivedString }) => {
if (receivedString == "ON") {
ON()
} else if (receivedString == "OFF") {
OFF()
} else if (receivedString == "FORWARD") {
前進()
} else if (receivedString == "BACK") {
後進()
} else if (receivedString == "RIGHT") {
右折()
} else if (receivedString == "LEFT") {
左折()
} else {
OFF()
}
})
function 左折() {
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 30)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 30)
}
function 右折() {
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 30)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 30)
}
function 後進() {
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 30)
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 30)
}
radio.setGroup(1)
basic.showLeds(`
. . # . .
. . . . .
# # # # #
. . . . .
. . # . .
`)