Consolidate 'coding' activities into single pages (#354)
This commit is contained in:
parent
eb45a76928
commit
6d07d5bd23
@ -41,12 +41,8 @@
|
|||||||
* [Reverse Beeper 1](/coding/reverse-beeper-1)
|
* [Reverse Beeper 1](/coding/reverse-beeper-1)
|
||||||
* [Reverse Beeper 2](/coding/reverse-beeper-2)
|
* [Reverse Beeper 2](/coding/reverse-beeper-2)
|
||||||
* [Reverse Beeper 3](/coding/reverse-beeper-3)
|
* [Reverse Beeper 3](/coding/reverse-beeper-3)
|
||||||
* [Ignition 1](/coding/ignition-1)
|
* [Ignition](/coding/ignition)
|
||||||
* [Ignition 2](/coding/ignition-2)
|
* [Cruise Control](/coding/cruise-control)
|
||||||
* [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)
|
|
||||||
* [Roaming 1](/coding/roaming-1)
|
* [Roaming 1](/coding/roaming-1)
|
||||||
* [Roaming 2](/coding/roaming-2)
|
* [Roaming 2](/coding/roaming-2)
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
})
|
|
||||||
```
|
|
@ -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);
|
|
||||||
})
|
|
||||||
```
|
|
@ -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()
|
|
||||||
})
|
|
||||||
```
|
|
65
docs/coding/cruise-control.md
Normal file
65
docs/coding/cruise-control.md
Normal 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()
|
||||||
|
})
|
||||||
|
```
|
@ -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)
|
|
||||||
```
|
|
@ -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);
|
|
||||||
}
|
|
||||||
```
|
|
@ -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
48
docs/coding/ignition.md
Normal 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);
|
||||||
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user