| 1 | // Circuit Playground MEGA Demo |
|---|
| 2 | // |
|---|
| 3 | // Showcases interesting demos of all the components on Circuit Playground. |
|---|
| 4 | // This is a somewhat advanced sketch that's broken across multiple files (see |
|---|
| 5 | // the tabs above for all the files). When run on Circuit Playground you can |
|---|
| 6 | // press the left button to cycle through each demo, and the right button to |
|---|
| 7 | // cycle through each mode inside a demo. When the slide switch is in the -/right |
|---|
| 8 | // position the board will 'turn off' and go into deep sleep (pullin around 5mA |
|---|
| 9 | // of current), and when moved into the +/left position it will turn on and run |
|---|
| 10 | // the demos. Make sure the switch is on the +/left side after you upload or else |
|---|
| 11 | // the board won't do anything! |
|---|
| 12 | // |
|---|
| 13 | // NOTE: This example requires the Adafruit SleepyDog sleep & watchdog library |
|---|
| 14 | // be installed. Use the library manager to install, or grab it from its home: |
|---|
| 15 | // - https://github.com/adafruit/Adafruit_SleepyDog |
|---|
| 16 | // - https://github.com/adafruit/Adafruit_ASFcore (for Express) |
|---|
| 17 | // |
|---|
| 18 | // The following demos are available (in order): |
|---|
| 19 | // - NeoPixel color rainbow cycle |
|---|
| 20 | // This animates all 10 neopixels with a rainbow cycle. Changing the mode |
|---|
| 21 | // changes the speed of animation. |
|---|
| 22 | // - Volume level meter |
|---|
| 23 | // This uses the microphone to display a volume level meter. The louder the |
|---|
| 24 | // volume the more lights light up (and a peak level dot animates back down). |
|---|
| 25 | // Changing mode changes the sensitivity from medium to very high to very low |
|---|
| 26 | // and back to medium again (3 levels). |
|---|
| 27 | // - Capacitive touch instrument |
|---|
| 28 | // This uses the capacitive touch pads (0, 1, 2, 3, 6, 9, 10, 12) and lights up |
|---|
| 29 | // a nearby pixel when a pad is touched. Pressing the mode toggles between |
|---|
| 30 | // playing a different tone for each pad too (8 notes total covering the basic |
|---|
| 31 | // C4 scale). |
|---|
| 32 | // - Accelerometer tilt demo |
|---|
| 33 | // This lights up all the pixels to a color between blue and red depending on |
|---|
| 34 | // the value of the accelerometer. Try tilting the board around axes to see |
|---|
| 35 | // how it behaves. Pressing mode toggles between the X, Y, Z axis as the one |
|---|
| 36 | // under measurements (starts with X axis). |
|---|
| 37 | // - Temperature & light sensor demo |
|---|
| 38 | // The left half of the pixels light up blue proportional to the temperature |
|---|
| 39 | // of the thermistor, and the right half of the pixels light up red proportional |
|---|
| 40 | // to the light sensor. Pressing mode toggles between small, medium, and big |
|---|
| 41 | // ranges of measurements for each sensor. |
|---|
| 42 | // |
|---|
| 43 | // Author: Tony DiCola |
|---|
| 44 | // License: MIT License (https://opensource.org/licenses/MIT) |
|---|
| 45 | #include <Adafruit_CircuitPlayground.h> |
|---|
| 46 | #include <Wire.h> |
|---|
| 47 | #include <SPI.h> |
|---|
| 48 | #include "Adafruit_SleepyDog.h" |
|---|
| 49 | |
|---|
| 50 | // Include all the demos, note that each demo is defined in a separate class to keep the sketch |
|---|
| 51 | // code below clean and simple. |
|---|
| 52 | #include "Demo.h" |
|---|
| 53 | #include "RainbowCycleDemo.h" |
|---|
| 54 | #include "VUMeterDemo.h" |
|---|
| 55 | #include "CapTouchDemo.h" |
|---|
| 56 | #include "TiltDemo.h" |
|---|
| 57 | #include "SensorDemo.h" |
|---|
| 58 | |
|---|
| 59 | // Create an instance of each demo class. |
|---|
| 60 | RainbowCycleDemo rainbowCycleDemo; |
|---|
| 61 | VUMeterDemo vuMeterDemo; |
|---|
| 62 | CapTouchDemo capTouchDemo; |
|---|
| 63 | TiltDemo tiltDemo; |
|---|
| 64 | SensorDemo sensorDemo; |
|---|
| 65 | |
|---|
| 66 | // Make a list of all demo class instances and keep track of the currently selected one. |
|---|
| 67 | int currentDemo = 0; |
|---|
| 68 | Demo* demos[] = { |
|---|
| 69 | &rainbowCycleDemo, |
|---|
| 70 | &vuMeterDemo, |
|---|
| 71 | &capTouchDemo, |
|---|
| 72 | &tiltDemo, |
|---|
| 73 | &sensorDemo |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | void setup() { |
|---|
| 77 | // Initialize serial port and circuit playground library. |
|---|
| 78 | Serial.begin(115200); |
|---|
| 79 | Serial.println("Circuit Playground MEGA Demo!"); |
|---|
| 80 | CircuitPlayground.begin(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void loop() { |
|---|
| 84 | // Check if slide switch is on the left (false) and go to sleep. |
|---|
| 85 | while (!CircuitPlayground.slideSwitch()) { |
|---|
| 86 | // Turn off the pixels, then go into deep sleep for a second. |
|---|
| 87 | CircuitPlayground.clearPixels(); |
|---|
| 88 | Watchdog.sleep(1000); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // Check for any button presses by checking their state twice with |
|---|
| 92 | // a delay inbetween. If the first press state is different from the |
|---|
| 93 | // second press state then something was pressed/released! |
|---|
| 94 | bool leftFirst = CircuitPlayground.leftButton(); |
|---|
| 95 | bool rightFirst = CircuitPlayground.rightButton(); |
|---|
| 96 | delay(10); |
|---|
| 97 | |
|---|
| 98 | // Run current demo's main loop. |
|---|
| 99 | demos[currentDemo]->loop(); |
|---|
| 100 | |
|---|
| 101 | // Now check for buttons that were released. |
|---|
| 102 | bool leftSecond = CircuitPlayground.leftButton(); |
|---|
| 103 | bool rightSecond = CircuitPlayground.rightButton(); |
|---|
| 104 | |
|---|
| 105 | // Left button will change the current demo. |
|---|
| 106 | if (leftFirst && !leftSecond) { |
|---|
| 107 | // Turn off all the pixels when entering new mode. |
|---|
| 108 | CircuitPlayground.clearPixels(); |
|---|
| 109 | // Increment the current demo (looping back to zero if at end). |
|---|
| 110 | currentDemo += 1; |
|---|
| 111 | if (currentDemo >= (sizeof(demos)/sizeof(Demo*))) { |
|---|
| 112 | currentDemo = 0; |
|---|
| 113 | } |
|---|
| 114 | Serial.print("Changed to demo: "); Serial.println(currentDemo, DEC); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | // Right button will change the mode of the current demo. |
|---|
| 118 | if (rightFirst && !rightSecond) { |
|---|
| 119 | demos[currentDemo]->modePress(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|