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

116 lines
3.6 KiB
Markdown
Raw Normal View History

2016-09-27 06:55:39 +02:00
# Light Sensor Tone control
## @description @boardname@ guitar: using light sensor to control tone
2016-09-27 06:55:39 +02:00
## ~avatar avatar
2016-09-27 06:55:39 +02:00
2016-10-04 01:37:13 +02:00
Use the Light Sensor to the control tone for this [Theremin](https://en.wikipedia.org/wiki/Theremin) inspired guitar
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
2016-09-27 06:55:39 +02:00
* **Concepts:**
* Inputs
* Light Intensity
* Tone/Frequency
* Ratio Mapping
* Forever Loop
2016-10-04 01:37:13 +02:00
* Math (multiplication) with code properties
## ~
2016-10-04 01:37:13 +02:00
## Duration: 30 - 45 minutes
2016-09-27 06:55:39 +02:00
2016-10-04 01:37:13 +02:00
*playing tones with light sensor*
2016-09-27 06:55:39 +02:00
https://youtu.be/2cKg9pokVC4
2016-11-02 01:44:37 +01:00
## The @boardname@ LEDs Light Sensors
2016-09-27 06:55:39 +02:00
2016-11-02 01:44:37 +01:00
- the @boardname@ can detect external light level intensity reaching the LEDs
2016-09-27 06:55:39 +02:00
- the light level block reports a reading of values 0 (*dark*) to 255 (*bright*)
2016-10-04 01:37:13 +02:00
- a **Forever Loop** is required to continually measure the current light level and control the tone
2016-09-27 06:55:39 +02:00
## Forever Loop
The forever loop really does run forever. The forever loop is useful when there is a need to continually check for an event or use a changing value in code.
## Blocks
```cards
basic.forever(() => {})
input.lightLevel()
led.plotBarGraph(0, 255)
music.playTone(Note.C, music.beat(BeatFraction.Quarter))
```
## Step 1: Create a light level detector
```blocks
basic.forever(() => {
led.plotBarGraph(input.lightLevel(), 255)
})
```
**Build the blocks**
2016-10-04 01:37:13 +02:00
* From **Basic** drag a **forever loop** block into the coding area
* From **Led** drag a **plot bar graph** block into the **forever loop**
* From **Input** drag a **light level** block into **plot bar graph *of***
**Set the *plot bar graph* value *up to* = *255* **
2016-09-27 06:55:39 +02:00
2016-10-04 01:37:13 +02:00
## Step 2: Test the light required to move the bar graph height
2016-09-27 06:55:39 +02:00
2016-10-04 01:37:13 +02:00
*graphing light input*
https://youtu.be/pqU7bTcfQ_s
Experiment to see the effect on graph height when the **plot bar graph** value ***up to*** is changed
2016-09-27 06:55:39 +02:00
2016-10-04 01:37:13 +02:00
**255 is the maximum light input reading**, try numbers smaller than 255
**Find a value** that allows the graph to show 1 - 5 bars
2016-09-27 06:55:39 +02:00
## ~hint
## Frequency
2016-10-04 01:37:13 +02:00
**Frequency** measured in Hz which are cycles per second or vibrations per second
* A healthy human ear can detect frequencies in the range of 20Hz to 20,000Hz.
2016-11-02 01:44:37 +01:00
* The @boardname@ + headphones reliably produce detectable output ~50Hz - 6,000Hz.
2016-09-27 06:55:39 +02:00
**261Hz** represents a C note
```blocks
music.playTone(261, music.beat(BeatFraction.Half))
```
2016-10-04 01:37:13 +02:00
**play tone** blocks can specify a numeric **Frequency**
by replacing the letter **C** note with a **number** block that has the value it represents
2016-09-27 06:55:39 +02:00
```blocks
music.playTone(261, music.beat(BeatFraction.Half))
```
## ~
2016-09-27 06:55:39 +02:00
## Step 3: Multiply Frequency using Math blocks
Cherry picked fixes from v0 to v1 (#1476) * Fix example and do a few edits (#850) * Small fixes to the Karel project text (#862) * fix missing shadow type * add calibration info (#897) microbit-support:6348 User reported bug, but didn't realie micro:bit compass had to be calibrated on first run of the program. * Cherry pick adding various cross-editor compat APIs (#863) * fix nudge * Fix a typo ("screent") in the documentation (#1012) * Fix a typo "screent" -> "the LED screen" * Update showArrow doc string for consistency with other methods * Doc bugs found by xlators (#899) * Update rotation.md (#998) reflects actual behaviour of the board * fixed layout corruption (#1073) * Updating the new bug filer with the right link * Update challenges.md (#1325) For what I'm taught in school, 2 follows 1 :wink::wink:. * Update simulator.md (#1209) * Update plant watering.md (#1264) the connection to servo are male not female * support for MIDI simulator in v0 (#1331) * Doc improvements (#1337) * update grammar in radio-dashboard sample code * Chanllenge 2 follows 1 I open all challenges.md without "Challenge 2" and review the file `grep -r "Challenge 2" -L | grep challenge | xargs -I@ code @` Changes to be committed: modified: docs/lessons/counter/challenges.md modified: docs/lessons/game-counter/challenges.md modified: docs/lessons/night-light/challenges.md modified: docs/lessons/snowflake-fall/challenges.md * Revert "support for MIDI simulator in v0 (#1331)" This reverts commit 30a9c411fb80762656e7a636feff8e77b7fd9e67. * Revert "Cherry pick adding various cross-editor compat APIs (#863)" This reverts commit 7308dbef23e9ee402bebb7e721d7014d8252c9e5. * Fixed pin needed to replace button A (#1385) * Cherry pick part of the signature update in in2cWriteBuffer (#863): 7308dbe
2018-10-22 19:00:57 +02:00
```blocks
2016-09-27 06:55:39 +02:00
input.onButtonPressed(Button.A, () => {
music.playTone(261 * 2, music.beat(BeatFraction.Half))
})
2016-10-04 01:37:13 +02:00
```
Create a **play tone** block using a **Math** section, **multiplication** block to set *tone*
2016-09-27 06:55:39 +02:00
## Next
2016-10-04 01:37:13 +02:00
**Add** a **B** button block that multiplies the **261** tone by a number other than 2 to set tone
2016-11-02 01:44:37 +01:00
**Download the code to the @boardname@**
2016-10-04 01:37:13 +02:00
**Test the sound for multiples of the 261Hz *C* frequency**
2016-09-27 06:55:39 +02:00
## Step 4: Control the Frequency with the light input
```blocks
basic.forever(() => {
music.playTone(input.lightLevel() * 25, music.beat(BeatFraction.Quarter))
})
```
2016-10-04 01:37:13 +02:00
**Create a *forever loop* containing a *play tone* block**
**Set *tone*, using *Math* multiplication block that multiplies *light level* input by 25**
or experiment with multipliers larger and smaller than 25
**Test light tone control on the guitar**
Cover the LEDs with your hand to vary light detected to control the tone
2016-09-27 06:55:39 +02:00
## Good work, this guitar is sounding good!
2016-10-04 01:37:13 +02:00
**Challenge:** Create a variable for the light level multiplier that you can change using buttons (optional)
2016-09-27 06:55:39 +02:00
## ~button /projects/guitar/accelerometer
2016-10-04 01:37:13 +02:00
NEXT: Accelerometer Beat control
## ~