| 1 | // Circuit Playground Digital Fidget Spinner |
|---|
| 2 | // ----------------------------------------- |
|---|
| 3 | // Uses the Circuit Playground accelerometer to detect when the board |
|---|
| 4 | // is flicked from one side or the other and animates the pixels spinning |
|---|
| 5 | // around accordingly (with decay in speed to simulate friction). |
|---|
| 6 | // Press one button to cycle through different color combinations, |
|---|
| 7 | // and another button to cycle through 4 different types of animations |
|---|
| 8 | // (single dot, dual dot, single sine wave, dual sine wave). |
|---|
| 9 | // See the FidgetSpinner.h and PeakDetector.h files as tabs at the top |
|---|
| 10 | // for more of the sketch code! |
|---|
| 11 | // Author: Tony DiCola |
|---|
| 12 | // License: MIT License (https://opensource.org/licenses/MIT) |
|---|
| 13 | #include <Adafruit_CircuitPlayground.h> |
|---|
| 14 | #include "FidgetSpinner.h" |
|---|
| 15 | #include "PeakDetector.h" |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | // Configure which accelerometer axis to check: |
|---|
| 19 | // This should be one of the following values (uncomment only ONE): |
|---|
| 20 | //#define AXIS CircuitPlayground.motionX() // X axis, good for Circuit Playground express (SAMD21). |
|---|
| 21 | #define AXIS CircuitPlayground.motionY() // Y axis, good for Circuit Playground classic (32u4). |
|---|
| 22 | //#define AXIS CircuitPlayground.motionZ() // Z axis, wacky--try it! |
|---|
| 23 | |
|---|
| 24 | // Configure how the axis direction compares to pixel movement direction: |
|---|
| 25 | #define INVERT_AXIS true // Set to true or 1 to invert the axis direction vs. |
|---|
| 26 | // pixel movement direction, or false/0 to disable. |
|---|
| 27 | // If the pixels spin in the opposite direction of your |
|---|
| 28 | // flick all the time then try changing this value. |
|---|
| 29 | |
|---|
| 30 | // Configure pixel brightness. |
|---|
| 31 | // Set this to a value from 0 to 255 (minimum to maximum brightness). |
|---|
| 32 | #define BRIGHTNESS 255 |
|---|
| 33 | |
|---|
| 34 | // Configure peak detector behavior: |
|---|
| 35 | #define LAG 30 // Lag, how large is the buffer of filtered samples. |
|---|
| 36 | // Must be an integer value! |
|---|
| 37 | #define THRESHOLD 20.0 // Number of standard deviations above average a |
|---|
| 38 | // value needs to be to be considered a peak. |
|---|
| 39 | #define INFLUENCE 0.1 // Scale down peak values to this percent influence |
|---|
| 40 | // when storing them back in the filtered values. |
|---|
| 41 | // Should be a value from 0 to 1.0 where smaller |
|---|
| 42 | // values mean peaks have less influence. |
|---|
| 43 | |
|---|
| 44 | // Configure spinner decay, i.e. how much it slows down. This should |
|---|
| 45 | // be a value from 0 to 1 where smaller values cause the spinner to |
|---|
| 46 | // slow down faster. |
|---|
| 47 | #define DECAY 0.66 |
|---|
| 48 | |
|---|
| 49 | // Debounce times for peak detection and button presses. |
|---|
| 50 | // You probably don't need to change these. |
|---|
| 51 | #define PEAK_DEBOUNCE_MS 500 |
|---|
| 52 | #define BUTTON_DEBOUNCE_MS 20 |
|---|
| 53 | |
|---|
| 54 | // Define combinations of colors. Each combo has a primary and secondary color |
|---|
| 55 | // that are defined as 24-bit RGB values (like HTML colors, set them using hex |
|---|
| 56 | // values). First define a struct to specify the types colors. Then define |
|---|
| 57 | // the list of color combos. If you want to change color combos (like add or |
|---|
| 58 | // remove them, skip down to the colors[] array below and change it. |
|---|
| 59 | typedef struct { |
|---|
| 60 | uint32_t primary; |
|---|
| 61 | uint32_t secondary; |
|---|
| 62 | } colorCombo; |
|---|
| 63 | // Add or remove color combos here: |
|---|
| 64 | colorCombo colors[] = { |
|---|
| 65 | // Each color combo has the following form: |
|---|
| 66 | // { .primary = 24-bit RGB color (use hex), .secondary = 24-bit RGB color }, |
|---|
| 67 | // Make sure each combo ends in a comma EXCEPT for the last one! |
|---|
| 68 | { .primary = 0xFF0000, .secondary = 0x000000 }, // Red to black |
|---|
| 69 | { .primary = 0x00FF00, .secondary = 0x000000 }, // Green to black |
|---|
| 70 | { .primary = 0x0000FF, .secondary = 0x000000 }, // Blue to black |
|---|
| 71 | { .primary = 0xFF0000, .secondary = 0x00FF00 }, // Red to green |
|---|
| 72 | { .primary = 0xFF0000, .secondary = 0x0000FF }, // Red to blue |
|---|
| 73 | { .primary = 0x00FF00, .secondary = 0x0000FF } // Green to blue |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | // Uncomment this to output a stream of debug information |
|---|
| 77 | // from the accelerometer and peak detector. Use the serial |
|---|
| 78 | // plotter to view the graph of these 4 values: |
|---|
| 79 | // - Accelerometer y axis value (in m/s^2) |
|---|
| 80 | // - Peak detector filtered y axis average |
|---|
| 81 | // - Peak detector filtered y axis standard deviation |
|---|
| 82 | // - Peak detector result, 0=no peak 1=positive peak, -1=negative peak |
|---|
| 83 | #define DEBUG |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | // Global state used by the sketch (you don't need to change this): |
|---|
| 87 | PeakDetector peakDetector(LAG, THRESHOLD, INFLUENCE); |
|---|
| 88 | FidgetSpinner fidgetSpinner(DECAY); |
|---|
| 89 | uint32_t lastMS = millis(); |
|---|
| 90 | uint32_t peakDebounce = 0; |
|---|
| 91 | uint32_t buttonDebounce = 0; |
|---|
| 92 | bool lastButton1; |
|---|
| 93 | bool lastButton2; |
|---|
| 94 | int currentAnimation = 0; |
|---|
| 95 | int currentColor = 0; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | void setup() { |
|---|
| 99 | // Initialize serial output for debugging and plotting. |
|---|
| 100 | Serial.begin(115200); |
|---|
| 101 | // Initialize Circuit Playground library and set accelerometer |
|---|
| 102 | // to its widest +/-16G range. |
|---|
| 103 | CircuitPlayground.begin(BRIGHTNESS); |
|---|
| 104 | CircuitPlayground.setAccelRange(LIS3DH_RANGE_16_G); |
|---|
| 105 | // Initialize starting button state. |
|---|
| 106 | lastButton1 = CircuitPlayground.leftButton(); |
|---|
| 107 | lastButton2 = CircuitPlayground.rightButton(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void loop() { |
|---|
| 111 | // Update time since last loop call. |
|---|
| 112 | uint32_t currentMS = millis(); |
|---|
| 113 | uint32_t deltaMS = currentMS - lastMS; // Time in milliseconds. |
|---|
| 114 | float deltaS = deltaMS / 1000.0; // Time in seconds. |
|---|
| 115 | lastMS = currentMS; |
|---|
| 116 | |
|---|
| 117 | // Grab the current accelerometer axis value and look for a sudden peak. |
|---|
| 118 | float accel = AXIS; |
|---|
| 119 | int result = peakDetector.detect(accel); |
|---|
| 120 | |
|---|
| 121 | // If in debug mode, print out the current acceleration and peak detector |
|---|
| 122 | // state (average, standard deviation, and peak result). Use the serial |
|---|
| 123 | // plotter to view this over time. |
|---|
| 124 | #ifdef DEBUG |
|---|
| 125 | Serial.print(accel); |
|---|
| 126 | Serial.print(","); |
|---|
| 127 | Serial.print(peakDetector.getAvg()); |
|---|
| 128 | Serial.print(","); |
|---|
| 129 | Serial.print(peakDetector.getStd()); |
|---|
| 130 | Serial.print(","); |
|---|
| 131 | Serial.println(result); |
|---|
| 132 | #endif |
|---|
| 133 | |
|---|
| 134 | // If there was a peak and enough time has elapsed since the last peak |
|---|
| 135 | // (i.e. to 'debounce' the noisey peak signal a bit) then start the spinner |
|---|
| 136 | // moving at a velocity proportional to the accelerometer value. |
|---|
| 137 | if ((result != 0) && (currentMS >= peakDebounce)) { |
|---|
| 138 | peakDebounce = currentMS + PEAK_DEBOUNCE_MS; |
|---|
| 139 | // Invert accel because accelerometer axis positive/negative is flipped |
|---|
| 140 | // with respect to pixel positive/negative movement. |
|---|
| 141 | if (INVERT_AXIS) { |
|---|
| 142 | fidgetSpinner.spin(-accel); |
|---|
| 143 | } |
|---|
| 144 | else { |
|---|
| 145 | fidgetSpinner.spin(accel); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // Check if enough time has passed to test for button releases. |
|---|
| 150 | if (currentMS >= buttonDebounce) { |
|---|
| 151 | buttonDebounce = currentMS + BUTTON_DEBOUNCE_MS; |
|---|
| 152 | // Check if any button was released. I.e. the last known button |
|---|
| 153 | // state was pressed (true) and the current state is released (false). |
|---|
| 154 | bool button1 = CircuitPlayground.leftButton(); |
|---|
| 155 | bool button2 = CircuitPlayground.rightButton(); |
|---|
| 156 | if (!button1 && lastButton1) { |
|---|
| 157 | // Button 1 released! |
|---|
| 158 | // Change to the next animation (looping back to start after 3 since |
|---|
| 159 | // there are 4 total animations). |
|---|
| 160 | currentAnimation = (currentAnimation + 1) % 4; |
|---|
| 161 | } |
|---|
| 162 | if (!button2 && lastButton2) { |
|---|
| 163 | // Button 2 released! |
|---|
| 164 | // Change to the next color index. |
|---|
| 165 | currentColor = (currentColor + 1) % (sizeof(colors)/sizeof(colorCombo)); |
|---|
| 166 | } |
|---|
| 167 | lastButton1 = button1; |
|---|
| 168 | lastButton2 = button2; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | // Update the spinner position and draw the current animation frame. |
|---|
| 172 | float pos = fidgetSpinner.getPosition(deltaS); |
|---|
| 173 | switch (currentAnimation) { |
|---|
| 174 | case 0: |
|---|
| 175 | // Single dot. |
|---|
| 176 | animateDots(pos, 1); |
|---|
| 177 | break; |
|---|
| 178 | case 1: |
|---|
| 179 | // Two opposite dots. |
|---|
| 180 | animateDots(pos, 2); |
|---|
| 181 | break; |
|---|
| 182 | case 2: |
|---|
| 183 | // Sine wave with one peak. |
|---|
| 184 | animateSine(pos, 1.0); |
|---|
| 185 | break; |
|---|
| 186 | case 3: |
|---|
| 187 | // Sine wave with two peaks. |
|---|
| 188 | animateSine(pos, 2.0); |
|---|
| 189 | break; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | // Helper functions: |
|---|
| 195 | void fillPixels(const uint32_t color) { |
|---|
| 196 | // Set all the pixels on CircuitPlayground to the specified color. |
|---|
| 197 | for (int i=0; i<CircuitPlayground.strip.numPixels();++i) { |
|---|
| 198 | CircuitPlayground.strip.setPixelColor(i, color); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | float constrainPosition(const float pos) { |
|---|
| 203 | // Take a continuous positive or negative value and map it to its relative positon |
|---|
| 204 | // within the range 0...<10 (so it's valid as an index to CircuitPlayground pixel |
|---|
| 205 | // position). |
|---|
| 206 | float result = fmod(pos, CircuitPlayground.strip.numPixels()); |
|---|
| 207 | if (result < 0.0) { |
|---|
| 208 | result += CircuitPlayground.strip.numPixels(); |
|---|
| 209 | } |
|---|
| 210 | return result; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | float lerp(const float x, const float x0, const float x1, const float y0, const float y1) { |
|---|
| 214 | // Linear interpolation of value y within y0...y1 given x and range x0...x1. |
|---|
| 215 | return y0 + (x-x0)*((y1-y0)/(x1-x0)); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | uint32_t primaryColor() { |
|---|
| 219 | return colors[currentColor].primary; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | uint32_t secondaryColor() { |
|---|
| 223 | return colors[currentColor].secondary; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | uint32_t colorLerp(const float x, const float x0, const float x1, const uint32_t c0, const uint32_t c1) { |
|---|
| 227 | // Perform linear interpolation of 24-bit RGB color values. |
|---|
| 228 | // Will return a color within the range of c0...c1 proportional to the value x within x0...x1. |
|---|
| 229 | uint8_t r0 = (c0 >> 16) & 0xFF; |
|---|
| 230 | uint8_t g0 = (c0 >> 8) & 0xFF; |
|---|
| 231 | uint8_t b0 = c0 & 0xFF; |
|---|
| 232 | uint8_t r1 = (c1 >> 16) & 0xFF; |
|---|
| 233 | uint8_t g1 = (c1 >> 8) & 0xFF; |
|---|
| 234 | uint8_t b1 = c1 & 0xFF; |
|---|
| 235 | uint32_t r = int(lerp(x, x0, x1, r0, r1)); |
|---|
| 236 | uint32_t g = int(lerp(x, x0, x1, g0, g1)); |
|---|
| 237 | uint32_t b = int(lerp(x, x0, x1, b0, b1)); |
|---|
| 238 | return (r << 16) | (g << 8) | b; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | // Animation functions: |
|---|
| 242 | void animateDots(float pos, int count) { |
|---|
| 243 | // Simple discrete dot animation. Spins dots around the board based on the specified |
|---|
| 244 | // spinner position. Count specifies how many dots to display, each one equally spaced |
|---|
| 245 | // around the pixels (in practice any count that 10 isn't evenly divisible by will look odd). |
|---|
| 246 | // Count should be from 1 to 10 (inclusive)! |
|---|
| 247 | fillPixels(secondaryColor()); |
|---|
| 248 | // Compute each dot's position and turn on the appropriate pixel. |
|---|
| 249 | for (int i=0; i<count; ++i) { |
|---|
| 250 | float dotPos = constrainPosition(pos + i*(float(CircuitPlayground.strip.numPixels())/float(count))); |
|---|
| 251 | CircuitPlayground.strip.setPixelColor(int(dotPos), primaryColor()); |
|---|
| 252 | } |
|---|
| 253 | CircuitPlayground.strip.show(); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | void animateSine(float pos, float frequency) { |
|---|
| 257 | // Smooth sine wave animation. Sweeps a sine wave of primary to secondary color around |
|---|
| 258 | // the board pixels based on the specified spinner position. |
|---|
| 259 | // Compute phase based on spinner position. As the spinner position changes the phase will |
|---|
| 260 | // move the sine wave around the pixels. |
|---|
| 261 | float phase = 2.0*PI*(constrainPosition(pos)/10.0); |
|---|
| 262 | for (int i=0; i<CircuitPlayground.strip.numPixels();++i) { |
|---|
| 263 | // Use a sine wave to compute the value of each pixel based on its position for time |
|---|
| 264 | // (and offset by the global phase that depends on fidget spinner position). |
|---|
| 265 | float x = sin(2.0*PI*frequency*(i/10.0)+phase); |
|---|
| 266 | CircuitPlayground.strip.setPixelColor(i, colorLerp(x, -1.0, 1.0, primaryColor(), secondaryColor())); |
|---|
| 267 | } |
|---|
| 268 | CircuitPlayground.strip.show(); |
|---|
| 269 | } |
|---|