| 1 | #ifndef SENSORDEMO_H |
|---|
| 2 | #define SENSORDEMO_H |
|---|
| 3 | |
|---|
| 4 | #include "Demo.h" |
|---|
| 5 | |
|---|
| 6 | // Define small, medium, large range of light sensor values. |
|---|
| 7 | static int minLight[] = { 0, 0, 0}; |
|---|
| 8 | static int maxLight[] = { 50, 255, 1023}; |
|---|
| 9 | |
|---|
| 10 | // Define small, medium, large range of temp sensor values (in Fahrenheit). |
|---|
| 11 | static float minTempF[] = { 80.0, 70.0, -32.0}; |
|---|
| 12 | static float maxTempF[] = { 85.0, 90.0, 212.0}; |
|---|
| 13 | |
|---|
| 14 | // Define color for light sensor pixels. |
|---|
| 15 | #define LIGHT_RED 0xFF |
|---|
| 16 | #define LIGHT_GREEN 0x00 |
|---|
| 17 | #define LIGHT_BLUE 0x00 |
|---|
| 18 | |
|---|
| 19 | // Define color for temp sensor pixels. |
|---|
| 20 | #define TEMP_RED 0x00 |
|---|
| 21 | #define TEMP_GREEN 0x00 |
|---|
| 22 | #define TEMP_BLUE 0xFF |
|---|
| 23 | |
|---|
| 24 | class SensorDemo: public Demo { |
|---|
| 25 | public: |
|---|
| 26 | SensorDemo() { mode = 0; } |
|---|
| 27 | ~SensorDemo() {} |
|---|
| 28 | |
|---|
| 29 | virtual void loop() { |
|---|
| 30 | // Reset all lights to off. |
|---|
| 31 | for (int i=0; i<10; ++i) { |
|---|
| 32 | CircuitPlayground.strip.setPixelColor(i, 0); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | // Measure the light level and use it to light up its LEDs (right half). |
|---|
| 36 | uint16_t light = CircuitPlayground.lightSensor(); |
|---|
| 37 | int level = (int)lerp(light, minLight[mode], maxLight[mode], 0.0, 5.0); |
|---|
| 38 | for (int i=9; i>9-level; --i) { |
|---|
| 39 | CircuitPlayground.strip.setPixelColor(i, LIGHT_RED, LIGHT_GREEN, LIGHT_BLUE); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // Measure the temperatue and use it to light up its LEDs (left half). |
|---|
| 43 | float tempF = CircuitPlayground.temperatureF(); |
|---|
| 44 | level = (int)lerp(tempF, minTempF[mode], maxTempF[mode], 0.0, 5.0); |
|---|
| 45 | for (int i=0; i<level; ++i) { |
|---|
| 46 | CircuitPlayground.strip.setPixelColor(i, TEMP_RED, TEMP_GREEN, TEMP_BLUE); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | // Light up the pixels! |
|---|
| 50 | CircuitPlayground.strip.show(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | virtual void modePress() { |
|---|
| 54 | // Switch to one of three modes for small, medium, big ranges of measurements. |
|---|
| 55 | mode += 1; |
|---|
| 56 | if (mode > 2) { |
|---|
| 57 | mode = 0; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | private: |
|---|
| 62 | int mode; |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | #endif |
|---|