2018-01-06 06:49:04 +01:00
|
|
|
# Cruise Control Activity 3
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
let speed = 0
|
|
|
|
function decelerate() {
|
|
|
|
if (speed > -100) {
|
|
|
|
speed = speed - 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function accelerate() {
|
|
|
|
if (speed < 100) {
|
|
|
|
speed = speed + 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function update() {
|
|
|
|
brick.clearScreen()
|
2018-01-09 21:46:48 +01:00
|
|
|
brick.showString("speed: " + speed, 1)
|
2018-02-19 16:35:08 +01:00
|
|
|
motors.largeBC.run(speed)
|
2018-01-06 06:49:04 +01:00
|
|
|
}
|
2018-01-31 17:28:00 +01:00
|
|
|
sensors.touch2.onEvent(ButtonEvent.Pressed, function () {
|
2018-01-06 06:49:04 +01:00
|
|
|
accelerate()
|
|
|
|
update()
|
|
|
|
})
|
2018-01-31 17:28:00 +01:00
|
|
|
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
|
2018-01-06 06:49:04 +01:00
|
|
|
decelerate()
|
|
|
|
update()
|
|
|
|
})
|
|
|
|
```
|