| 1 | #ifndef CAPTOUCHDEMO_H |
|---|
| 2 | #define CAPTOUCHDEMO_H |
|---|
| 3 | |
|---|
| 4 | #include "Demo.h" |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #define CAP_SAMPLES 20 // Number of samples to take for a capacitive touch read. |
|---|
| 8 | #define TONE_DURATION_MS 100 // Duration in milliseconds to play a tone when touched. |
|---|
| 9 | |
|---|
| 10 | class CapTouchDemo: public Demo { |
|---|
| 11 | public: |
|---|
| 12 | uint16_t CAP_THRESHOLD = 200; // Threshold for a capacitive touch (higher = less sensitive). |
|---|
| 13 | |
|---|
| 14 | CapTouchDemo() { |
|---|
| 15 | playSound = false; |
|---|
| 16 | if (CircuitPlayground.isExpress()) { |
|---|
| 17 | CAP_THRESHOLD = 800; |
|---|
| 18 | } else { |
|---|
| 19 | CAP_THRESHOLD = 200; |
|---|
| 20 | } |
|---|
| 21 | } |
|---|
| 22 | ~CapTouchDemo() {} |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | virtual void loop() { |
|---|
| 26 | // Clear all the neopixels. |
|---|
| 27 | for (int i=0; i<10; ++i) { |
|---|
| 28 | CircuitPlayground.strip.setPixelColor(i, 0); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | // Check if any of the cap touch inputs are pressed and turn on those pixels. |
|---|
| 32 | // Also play a tone if in tone playback mode. |
|---|
| 33 | if (CircuitPlayground.readCap(0, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 34 | CircuitPlayground.strip.setPixelColor(3, CircuitPlayground.colorWheel(256/10*3)); |
|---|
| 35 | if (playSound) { |
|---|
| 36 | CircuitPlayground.playTone(330, TONE_DURATION_MS); // 330hz = E4 |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | if (CircuitPlayground.readCap(1, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 40 | CircuitPlayground.strip.setPixelColor(4, CircuitPlayground.colorWheel(256/10*4)); |
|---|
| 41 | if (playSound) { |
|---|
| 42 | CircuitPlayground.playTone(349, TONE_DURATION_MS); // 349hz = F4 |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | if (CircuitPlayground.readCap(2, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 46 | CircuitPlayground.strip.setPixelColor(1, CircuitPlayground.colorWheel(256/10)); |
|---|
| 47 | if (playSound) { |
|---|
| 48 | CircuitPlayground.playTone(294, TONE_DURATION_MS); // 294hz = D4 |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | if (CircuitPlayground.readCap(3, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 52 | CircuitPlayground.strip.setPixelColor(0, CircuitPlayground.colorWheel(0)); |
|---|
| 53 | if (playSound) { |
|---|
| 54 | CircuitPlayground.playTone(262, TONE_DURATION_MS); // 262hz = C4 |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | if (CircuitPlayground.readCap(6, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 58 | CircuitPlayground.strip.setPixelColor(6, CircuitPlayground.colorWheel(256/10*6)); |
|---|
| 59 | if (playSound) { |
|---|
| 60 | CircuitPlayground.playTone(440, TONE_DURATION_MS); // 440hz = A4 |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | if (CircuitPlayground.readCap(9, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 64 | CircuitPlayground.strip.setPixelColor(8, CircuitPlayground.colorWheel(256/10*8)); |
|---|
| 65 | if (playSound) { |
|---|
| 66 | CircuitPlayground.playTone(494, TONE_DURATION_MS); // 494hz = B4 |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | if (CircuitPlayground.readCap(10, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 70 | CircuitPlayground.strip.setPixelColor(9, CircuitPlayground.colorWheel(256/10*9)); |
|---|
| 71 | if (playSound) { |
|---|
| 72 | CircuitPlayground.playTone(523, TONE_DURATION_MS); // 523hz = C5 |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | if (CircuitPlayground.readCap(12, CAP_SAMPLES) >= CAP_THRESHOLD) { |
|---|
| 76 | CircuitPlayground.strip.setPixelColor(5, CircuitPlayground.colorWheel(256/10*5)); |
|---|
| 77 | if (playSound) { |
|---|
| 78 | CircuitPlayground.playTone(392, TONE_DURATION_MS); // 392hz = G4 |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // Light up the pixels. |
|---|
| 83 | CircuitPlayground.strip.show(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | virtual void modePress() { |
|---|
| 87 | // Turn sound on/off. |
|---|
| 88 | playSound = !playSound; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | private: |
|---|
| 92 | bool playSound; |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | #endif |
|---|