Created
April 13, 2018 13:45
-
-
Save netom/72425aca90aa86ade51a8c8e4fbbf47c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let steering = 0 | |
let led2 = 0 | |
let distance = 0 | |
bluetooth.onBluetoothConnected(() => { | |
basic.showLeds(` | |
. # # . . | |
. # . # . | |
. # # . . | |
. # . # . | |
. # # . . | |
`) | |
}) | |
bluetooth.onBluetoothDisconnected(() => { | |
basic.showLeds(` | |
. # . # . | |
. # . # . | |
. . . . . | |
# . . . # | |
. # # # . | |
`) | |
}) | |
sbrick.onConnected(() => { | |
basic.showLeds(` | |
. # # # . | |
. # . . . | |
. . # . . | |
. . . # . | |
. # # # . | |
`) | |
sbrick.setDevice(SBConnectedDevice.Wedo1Motion, SBPort.C) | |
}) | |
sbrick.onMeasurement(() => { | |
distance = sbrick.measuredValue() - 200 | |
led.plotBarGraph( | |
distance, | |
220 | |
) | |
}) | |
input.onButtonPressed(Button.A, () => { | |
led2 = 1 - led2 | |
sbrick.drive(255 * led2, SBPort.A, SBDirection.Forward) | |
}) | |
basic.showLeds(` | |
. # . # . | |
. # . # . | |
. . . . . | |
# . . . # | |
. # # # . | |
`) | |
led2 = 0 | |
steering = 0 | |
sbrick.connect("SBrick2") | |
basic.forever(() => { | |
if (input.acceleration(Dimension.Y) < 500) { | |
if (input.buttonIsPressed(Button.B)) { | |
sbrick.drive(255, SBPort.D, SBDirection.Forward) | |
} else { | |
sbrick.brake(SBPort.D) | |
} | |
} else { | |
if (input.buttonIsPressed(Button.B)) { | |
sbrick.drive(255, SBPort.D, SBDirection.Backward) | |
} else { | |
sbrick.brake(SBPort.D) | |
} | |
} | |
steering = input.acceleration(Dimension.X) / 4 | |
if (steering > 255) { | |
steering = 255 | |
} | |
if (steering < -255) { | |
steering = -255 | |
} | |
if (steering < 50 && steering > -50) { | |
steering = 0 | |
} | |
if (steering < 0) { | |
sbrick.drive(0 - steering, SBPort.B, SBDirection.Forward) | |
} else { | |
sbrick.drive(steering, SBPort.B, SBDirection.Backward) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment