pxt-calliope/docs/lessons/glowing-pendulum/activity.md

109 lines
2.4 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# glowing pendulum block activity
Construct a pendulum that glows using acceleration.
Welcome! This activity will teach how to construct a pendulum that glows using acceleration. Let's get started!
2016-05-09 19:32:02 +02:00
Turn on all the LEDs.
```blocks
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
```
2016-03-26 00:47:20 +01:00
Create a **forever** loop that will constantly display the appropriate brightness on the LED display.
```blocks
2016-05-09 19:32:02 +02:00
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
2016-03-26 00:47:20 +01:00
basic.forever(() => {
})
```
Now let's measure the acceleration on the `y` axis and store that value in a variable. The `acceleration(y)` function will provide the value.
```blocks
2016-05-09 19:32:02 +02:00
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
2016-03-26 00:47:20 +01:00
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
});
```
2016-11-02 01:44:37 +01:00
Since the @boardname@ will be swinging back and forth, the acceleration will only be positive half of the time. Thus, to always get a positive value, we want to take the absolute value of the acceleration.
2016-03-26 00:47:20 +01:00
```blocks
2016-05-09 19:32:02 +02:00
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
2016-03-26 00:47:20 +01:00
basic.forever(() => {
2016-05-09 19:32:02 +02:00
let acceleration = input.acceleration(Dimension.Y);
2016-03-26 00:47:20 +01:00
acceleration = Math.abs(acceleration)
});
```
2016-11-02 01:44:37 +01:00
The function `acceleration(y)` returns a number between 0 and 1024. We want to use this value for the brightness of the @boardname@, but the `set brightness()` only accepts a value between 0 and 256. Thus, we need to divide the acceleration by 4 to ensure we will be in the appropriate range.
2016-03-26 00:47:20 +01:00
2016-04-25 20:31:06 +02:00
```blocks
2016-05-09 19:32:02 +02:00
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
2016-04-25 20:31:06 +02:00
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
});
```
2016-03-26 00:47:20 +01:00
2016-11-02 01:44:37 +01:00
Now let's use our acceleration value to set the brightness on the @boardname@.
2016-03-26 00:47:20 +01:00
2016-04-25 20:31:06 +02:00
```blocks
2016-05-09 19:32:02 +02:00
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
2016-04-25 20:31:06 +02:00
basic.forever(() => {
let acceleration = input.acceleration(Dimension.Y);
acceleration = Math.abs(acceleration);
acceleration = acceleration / 4;
led.setBrightness(acceleration)
});
```
2016-03-26 00:47:20 +01:00
### ~avatar avatar
2016-04-13 17:27:45 +02:00
Excellent, you're ready to continue with the [challenges](/lessons/glowing-pendulum/challenges)!
2016-03-26 00:47:20 +01:00
### ~