pxt-calliope/docs/projects/guitar/accelerometer.md

116 lines
3.3 KiB
Markdown
Raw Normal View History

2016-09-27 06:55:39 +02:00
# Accelerometer Beat control
### @description micro:bit guitar: using accelerometer to control tempo
### ~avatar avatar
Use the Accelerometer to control guitar tempo
* Concepts:
* Gravity
* Acceleration
* X, Y, Z coordinates
* Tempo
* Beat
* Mapping
* Graphing
* Absolute value
### ~
Updated guitar lessons (#268) * initial notes * updated code samples * splitting the page and preparing layout * missing gamepad api * added basic guitar activty md files added initial draft of guitar lesson - in progress * word smithing and adding detail for concepts listed * word smithing and adding detail for concepts listed * typo in file name * missing macro * replaced guitar.png with image from Julia Carlson * adding a few pictures * adding cards macros * few tweaks * image for accellerometer lesson xyz axis * fixed the numbering buttons page -fixed the numbering to restart for each section and asdded spaces after so they don't wrap * adding a few video links * pin-press - added circuit info * more vids * fixing latest commit * edits to light sensor and pin-press * updated descriptions, linking lesson * pin-press and display-buttons updates -added dode samples to pin press * updated making video * light-sensor updated numbering * Clean up Clean up of headings to template style, other fixes * putting display in template use h2 with "Step 1:" and image before text * removed numbered lists for display & light these topics are code complete * more consistent formating Acceleromenter, buttons, light sensosr * Acceleration Lesson, added art Lesson steps for Acceleration, art for degree F to C for Map anology+clean up of light sensor topic * Pin Press code complete some other basic fomats and edits * Removed dashes in lesson names renamed and fixed references to files with "-" in names * rest of fles w/ dash in name changes * fixed links to guitar activities * updated acceleraomter section * transparency 4 art, image text added transarency and renamed art, added descriptive text for video and still images * renamed art * fixing the merge * fixing hint * moved step 2 outside ~hint block * fixing snippets * remove guitar from project list * Duration/Materials consistent format moved out of ~avatar block
2016-10-03 23:59:58 +02:00
## Duration: 30 - 45 minutes
2016-09-27 06:55:39 +02:00
*accelerometer controlled tempo*
https://youtu.be/h_gPkBaVkoo
TODO: add sound to video
## Blocks
```cards
input.acceleration(Dimension.Y)
music.setTempo(120)
pins.map(0, 0, 1023,60, 320)
Math.abs(1)
```
## Accelerometer, gravity and tilting!
The micro:bit contains an **accelerometer** sensor that is able to measure forces applied to the board.
On earth, we are subject to the **gravity force** which pulls us to the ground!
https://youtu.be/0SULoTKmkhI
When the micro:bit is flat on a table, with the screen pointing up, the gravity force is aligned
with the **Z** axis of the micro:bit.
![micro:bit x, y, z axis image](/static/mb/projects/guitar/accelleration_axis.png)
If you tilt it up and down, the force will align with the **Y** axis -- this is how we can detect tilting!!!
If the force along **Y** grows, the micro:bit is tilting more and more vertically!
## Measuring Acceleration along different coordinates (X, Y, Z axis)
The acceleration block approximately measures **milli-g**, which is 1/1000 of a **g** or the
acceleration of gravity.
### Step 1: Graphing acceleration
```blocks
basic.forever(() => {
led.plotBarGraph(input.acceleration(Dimension.Y), 1023)
})
```
**Create the code** that measures the change in the Y axis acceleration as a graph on the LEDs
**Dowload the code** to the micro:bit
**Test the movements that move the graph from 1 to 5 bars on the LEDs**
### Extra
Try graphing the acceleration along the **X** and **Z** axis. Can you explain the differences?
### ~hint
## Mapping
**It is common to map one standard to another - such as with temperature**
![Fahrenheit to Celsius](/static/mb/projects/guitar/map_analogy.png "Fahrenheit to Celsius")
### ~
### Step 2: Mapping acceleration to Beat
**micro:bit sensors produce signal values between 0 to 1023. The *[map block](/reference/pins/map)* converts the signal to a desired range.**
```blocks
basic.forever(() => {
music.setTempo(pins.map(Math.abs(input.acceleration(Dimension.Y)),
0, 1023,
60, 320))
music.playTone(Note.C, music.beat(BeatFraction.Quarter));
})
```
**Create the code** that *Maps* Y axis acceleration as *tempo*
**Download the code** to the micro:bit on the guitar
**Test the movements that speed and slow the tempo**
### Step 3: Combine with light sensor tone control
**Put it all together!**
```blocks
basic.forever(() => {
music.setTempo(pins.map(Math.abs(input.acceleration(Dimension.Y)),
0, 1023,
60, 320))
music.playTone(
input.lightLevel() * 25,
music.beat(BeatFraction.Quarter)
);
})
```
**Combine the code above with the light sensor tone control code from the previous activity**
**Download the code** to the micro:bit on the guitar
### Now play the guitar adjusting tone and tempo using the light sensor and accelerometer!
### ~button /projects/guitar/pinpress
NEXT: Pin Press on/off
### ~