source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/Adafruit_CircuitPlayground/examples/analog_sensors/analog_sensors.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 3.0 KB
Line 
1// Circuit Playground Analog Sensor Demo
2// Shows how to read an analog sensor like temperature, light,
3// sound, or even external inputs and convert the analog value
4// to color and sound on the board.  Will light up NeoPixel 4 and 5
5// with a color proportional to the analog value, and if the slide
6// switch is turned to the left will play a music tone proportional
7// to the value.
8#include <Adafruit_CircuitPlayground.h>
9#include <Wire.h>
10#include <SPI.h>
11
12// Change the analog input value below to try different sensors:
13#define ANALOG_INPUT  A5  // Specify the analog input to read.
14                          // Circuit Playground has the following
15                          // inputs available:
16                          //  - A0  = temperature sensor / thermistor
17                          //  - A4  = sound sensor / microphone
18                          //  - A5  = light sensor
19                          //  - A7  = pin #6 on board
20                          //  - A9  = pin #9 on board
21                          //  - A10 = pin #10 on board
22                          //  - A11 = pin #12 on board
23
24// These defines set the range of expected analog values.
25// This is used to scale the NeoPixels, sound, etc.
26#define VALUE_MIN     0
27#define VALUE_MAX     200
28
29// These defines set the range of pixel color when mapping
30// to the sensor value.
31#define COLOR_RED_MIN    255 
32#define COLOR_GREEN_MIN  0
33#define COLOR_BLUE_MIN   0
34
35#define COLOR_RED_MAX    0
36#define COLOR_GREEN_MAX  0
37#define COLOR_BLUE_MAX   255
38
39// These defines set the range of sound frequencies when
40// mapping to the sensor value.
41#define TONE_FREQ_MIN    523  // C5 note
42#define TONE_FREQ_MAX    988  // B5 note
43
44
45void setup() {
46  // Setup serial port.
47  Serial.begin(115200);
48  Serial.println("Circuit Playground Analog Sensor Demos!");
49
50  // Setup Circuit Playground library.
51  CircuitPlayground.begin();
52}
53
54void loop() {
55  // Get the sensor sensor value and print it out (can use serial plotter
56  // to view realtime graph!).
57  uint16_t value = analogRead(ANALOG_INPUT);
58  Serial.println(value, DEC);
59
60  // Map the sensor value to a color.
61  // Use the range of minimum and maximum sensor values and
62  // min/max colors to do the mapping.
63  int red = map(value, VALUE_MIN, VALUE_MAX, COLOR_RED_MIN, COLOR_RED_MAX);
64  int green = map(value, VALUE_MIN, VALUE_MAX, COLOR_GREEN_MIN, COLOR_GREEN_MAX);
65  int blue = map(value, VALUE_MIN, VALUE_MAX, COLOR_BLUE_MIN, COLOR_BLUE_MAX);
66
67  // Light up pixel #4 and 5 with the color.
68  CircuitPlayground.clearPixels();
69  CircuitPlayground.setPixelColor(4, red, green, blue);
70  CircuitPlayground.setPixelColor(5, red, green, blue);
71
72  // Map the sensor value to a tone frequency.
73  int frequency = map(value, VALUE_MIN, VALUE_MAX, TONE_FREQ_MIN, TONE_FREQ_MAX);
74
75  // Play the tone if the slide switch is turned on (to the left).
76  if (CircuitPlayground.slideSwitch()) {
77    // Play tone of the mapped frequency value for 100 milliseconds.
78    CircuitPlayground.playTone(frequency, 100);
79  }
80
81  // Delay for a bit and repeat the loop.
82  delay(100);
83}
Note: See TracBrowser for help on using the repository browser.