Line | |
---|
1 | /*
|
---|
2 | Esplora Sound Sensor
|
---|
3 |
|
---|
4 | This sketch shows you how to read the microphone sensor. The microphone
|
---|
5 | will range from 0 (total silence) to 1023 (really loud).
|
---|
6 | When you're using the sensor's reading (for example, to set the brightness
|
---|
7 | of the LED), you map the sensor's reading to a range between the minimum
|
---|
8 | and the maximum.
|
---|
9 |
|
---|
10 | Created on 22 Dec 2012
|
---|
11 | by Tom Igoe
|
---|
12 |
|
---|
13 | This example is in the public domain.
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include <Esplora.h>
|
---|
17 |
|
---|
18 | void setup() {
|
---|
19 | // initialize the serial communication:
|
---|
20 | Serial.begin(9600);
|
---|
21 | }
|
---|
22 |
|
---|
23 | void loop() {
|
---|
24 | // read the sensor into a variable:
|
---|
25 | int loudness = Esplora.readMicrophone();
|
---|
26 |
|
---|
27 | // map the sound level to a brightness level for the LED:
|
---|
28 | int brightness = map(loudness, 0, 1023, 0, 255);
|
---|
29 | // write the brightness to the green LED:
|
---|
30 | Esplora.writeGreen(brightness);
|
---|
31 |
|
---|
32 |
|
---|
33 | // print the microphone levels and the LED levels (to see what's going on):
|
---|
34 | Serial.print("sound level: ");
|
---|
35 | Serial.print(loudness);
|
---|
36 | Serial.print(" Green brightness: ");
|
---|
37 | Serial.println(brightness);
|
---|
38 | // add a delay to keep the LED from flickering:
|
---|
39 | delay(10);
|
---|
40 | }
|
---|
41 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.