Consolidate 'coding' activities into single pages (#354)

This commit is contained in:
Galen Nickel 2018-02-27 17:15:12 -08:00 committed by Peli de Halleux
parent eb45a76928
commit 6d07d5bd23
9 changed files with 115 additions and 95 deletions

View File

@ -41,12 +41,8 @@
* [Reverse Beeper 1](/coding/reverse-beeper-1)
* [Reverse Beeper 2](/coding/reverse-beeper-2)
* [Reverse Beeper 3](/coding/reverse-beeper-3)
* [Ignition 1](/coding/ignition-1)
* [Ignition 2](/coding/ignition-2)
* [Ignition 3](/coding/ignition-3)
* [Cruise Control 1](/coding/cruise-control-1)
* [Cruise Control 2](/coding/cruise-control-2)
* [Cruise Control 3](/coding/cruise-control-3)
* [Ignition](/coding/ignition)
* [Cruise Control](/coding/cruise-control)
* [Roaming 1](/coding/roaming-1)
* [Roaming 2](/coding/roaming-2)

View File

@ -1,10 +0,0 @@
# Cruise Control Activity 1
```blocks
let speed = 0;
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
if (speed < 100)
speed = speed + 10;
motors.largeBC.run(speed);
})
```

View File

@ -1,15 +0,0 @@
# Cruise Control Activity 2
```blocks
let speed = 0;
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
if (speed < 100)
speed = speed + 10;
motors.largeBC.run(speed);
})
sensors.touch2.onEvent(ButtonEvent.Pressed, function () {
if (speed > -100)
speed = speed - 10;
motors.largeBC.run(speed);
})
```

View File

@ -1,28 +0,0 @@
# 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()
brick.showString("speed: " + speed, 1)
motors.largeBC.run(speed)
}
sensors.touch2.onEvent(ButtonEvent.Pressed, function () {
accelerate()
update()
})
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
decelerate()
update()
})
```

View File

@ -0,0 +1,65 @@
# Cruise Control
Learn how to set and adjust motor speeds.
## Activity 1
Increase motor speed when touch sensor `1` is pressed.
```blocks
let speed = 0;
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
if (speed < 100)
speed = speed + 10;
motors.largeBC.run(speed);
})
```
## Activity 2
Add a "reduce" motor speed action when touch sensor `2` is pressed.
```blocks
let speed = 0;
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
if (speed < 100)
speed = speed + 10;
motors.largeBC.run(speed);
})
sensors.touch2.onEvent(ButtonEvent.Pressed, function () {
if (speed > -100)
speed = speed - 10;
motors.largeBC.run(speed);
})
```
## Activity 3
Refactor your code by moving the speed increase and speed decrease code into ``||functions:accelerate||`` and ``||functions:decelerate||`` functions. Run the motors at the new speed in an ``||functions:update||`` function.
```blocks
let speed = 0
function decelerate() {
if (speed > -100) {
speed = speed - 10
}
}
function accelerate() {
if (speed < 100) {
speed = speed + 10
}
}
function update() {
brick.clearScreen()
brick.showString("speed: " + speed, 1)
motors.largeBC.run(speed)
}
sensors.touch2.onEvent(ButtonEvent.Pressed, function () {
accelerate()
update()
})
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
decelerate()
update()
})
```

View File

@ -1,11 +0,0 @@
# Ignition Activity 1
```blocks
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
brick.showImage(images.eyesDizzy)
})
sensors.ultrasonic4.onEvent(UltrasonicSensorEvent.ObjectDetected, function () {
brick.showImage(images.eyesTiredMiddle)
})
brick.showImage(images.eyesSleeping)
```

View File

@ -1,12 +0,0 @@
# Ignition Activity 2
```blocks
while (true) {
if (sensors.touch1.wasPressed() &&
sensors.ultrasonic4.distance() < 10) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
}
pause(1);
}
```

View File

@ -1,13 +0,0 @@
# Ignition Activity 3
```blocks
while (true) {
if (sensors.ultrasonic4.distance() < 10 &&
sensors.touch1.wasPressed() &&
brick.buttonEnter.wasPressed()) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
}
pause(1);
}
```

48
docs/coding/ignition.md Normal file
View File

@ -0,0 +1,48 @@
# Ignition
Explore sensor events and sensor status.
## Activity 1
Wait for a touch sensor press or ultrasonic object detection. Show an expression on the screen when they happen.
```blocks
sensors.touch1.onEvent(ButtonEvent.Pressed, function () {
brick.showImage(images.eyesDizzy)
})
sensors.ultrasonic4.onEvent(UltrasonicSensorEvent.ObjectDetected, function () {
brick.showImage(images.eyesTiredMiddle)
})
brick.showImage(images.eyesSleeping)
```
## Activity 2
Play some motor sounds if touch sensor `1` is pressed at the same moment when and object comes close.
```blocks
while (true) {
if (sensors.touch1.wasPressed() &&
sensors.ultrasonic4.distance() < 10) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
}
pause(1);
}
```
## Activity 3
Play some motor sounds if touch sensor `1` is pressed when both the `enter` button is pressed on the brick and an object comes close.
```blocks
while (true) {
if (sensors.ultrasonic4.distance() < 10 &&
sensors.touch1.wasPressed() &&
brick.buttonEnter.wasPressed()) {
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
}
pause(1);
}
```