[136] | 1 | /*
|
---|
| 2 | Esplora Led/Microphone
|
---|
| 3 |
|
---|
| 4 | This simple sketch reads the microphone, light sensor, and slider.
|
---|
| 5 | Then it uses those readings to set the brightness of red, green and blue
|
---|
| 6 | channels of the RGB LED. The red channel will change with the loudness
|
---|
| 7 | "heared" by the microphone, the green channel changes as the
|
---|
| 8 | amount of light in the room and the blue channel will change
|
---|
| 9 | with the position of the slider.
|
---|
| 10 |
|
---|
| 11 | Created on 22 november 2012
|
---|
| 12 | By Enrico Gueli <enrico.gueli@gmail.com>
|
---|
| 13 | Modified 24 Nov 2012
|
---|
| 14 | by Tom Igoe
|
---|
| 15 | */
|
---|
| 16 |
|
---|
| 17 | #include <Esplora.h>
|
---|
| 18 |
|
---|
| 19 | void setup() {
|
---|
| 20 | // initialize the serial communication:
|
---|
| 21 | Serial.begin(9600);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | int lowLight = 400; // the light sensor reading when it's covered
|
---|
| 25 | int highLight = 1023; // the maximum light sensor reading
|
---|
| 26 | int minGreen = 0; // minimum brightness of the green LED
|
---|
| 27 | int maxGreen = 100; // maximum brightness of the green LED
|
---|
| 28 |
|
---|
| 29 | void loop() {
|
---|
| 30 | // read the sensors into variables:
|
---|
| 31 | int mic = Esplora.readMicrophone();
|
---|
| 32 | int light = Esplora.readLightSensor();
|
---|
| 33 | int slider = Esplora.readSlider();
|
---|
| 34 |
|
---|
| 35 | // convert the sensor readings to light levels:
|
---|
| 36 | byte red = constrain(mic, 0, 255);
|
---|
| 37 | byte green = constrain(
|
---|
| 38 | map(light, lowLight, highLight, minGreen, maxGreen),
|
---|
| 39 | 0, 255);
|
---|
| 40 | byte blue = slider / 4;
|
---|
| 41 |
|
---|
| 42 | // print the light levels (to see what's going on):
|
---|
| 43 | Serial.print(red);
|
---|
| 44 | Serial.print(' ');
|
---|
| 45 | Serial.print(green);
|
---|
| 46 | Serial.print(' ');
|
---|
| 47 | Serial.println(blue);
|
---|
| 48 |
|
---|
| 49 | // write the light levels to the LED.
|
---|
| 50 | // note that the green value is always 0:
|
---|
| 51 | Esplora.writeRGB(red, green, blue);
|
---|
| 52 |
|
---|
| 53 | // add a delay to keep the LED from flickering:
|
---|
| 54 | delay(10);
|
---|
| 55 | }
|
---|