| [3] | 1 | // Adafruit Circuit Playground Fidget Spinner Tachometer |
|---|
| 2 | // |
|---|
| 3 | // This sketch uses the light sensor built in to Circuit Playground |
|---|
| 4 | // to detect the speed (in revolutions per second) of a fidget spinner. |
|---|
| 5 | // Load the sketch on a Circuit Playground (either classic or express) |
|---|
| 6 | // and notice the first three NeoPixels will turn on bright white. |
|---|
| 7 | // Hold a spinning fidget spinner very close to (but not touching) the |
|---|
| 8 | // light sensor (look for the eye graphic on the board, it's right |
|---|
| 9 | // below the three lit NeoPixels) and look at the serial monitor |
|---|
| 10 | // to see it print out the speed of the spinner. This works best |
|---|
| 11 | // holding the spinner perpendicular to the sensor, like: |
|---|
| 12 | // || |
|---|
| 13 | // || <- Spinner |
|---|
| 14 | // || |
|---|
| 15 | // ________ <- Circuit Playground |
|---|
| 16 | // |
|---|
| 17 | // Author: Tony DiCola |
|---|
| 18 | // License: MIT License (https://en.wikipedia.org/wiki/MIT_License) |
|---|
| 19 | #include <Adafruit_CircuitPlayground.h> |
|---|
| 20 | |
|---|
| 21 | // Configuration values. You don't have to change these but they're |
|---|
| 22 | // interesting to adjust the logic. If your spinner has more or less |
|---|
| 23 | // than three arms be sure to change the SPINNER_ARMS value below: |
|---|
| 24 | #define SPINNER_ARMS 3 // Number of arms on the fidget spinner. |
|---|
| 25 | // This is used to calculate the true |
|---|
| 26 | // revolutions per second of the spinner |
|---|
| 27 | // as one full revolution of the spinner |
|---|
| 28 | // will actually see this number of cycles |
|---|
| 29 | // pass by the light sensor. Set this to |
|---|
| 30 | // the value 1 to ignore this calculation |
|---|
| 31 | // and just see the raw cycles / second. |
|---|
| 32 | |
|---|
| 33 | #define SAMPLE_DEPTH 512 // How many samples to take when measuring |
|---|
| 34 | // the spinner speed. The larger this value |
|---|
| 35 | // the more memory that will be consumed but |
|---|
| 36 | // the slower a spinner speed that can be |
|---|
| 37 | // detected (larger sample depths mean longer |
|---|
| 38 | // period waves can be detected). You're |
|---|
| 39 | // limited by the amount of memory on the |
|---|
| 40 | // board for this value. On a classic |
|---|
| 41 | // Circuit Playground with 2kb of memory |
|---|
| 42 | // you can only go up to about 512 or so (each |
|---|
| 43 | // sample is 2 bytes, so 1kb of space total). |
|---|
| 44 | // On an express board you can go much higher, |
|---|
| 45 | // like up to 10240 for 20kb of sample data. |
|---|
| 46 | |
|---|
| 47 | #define SAMPLE_PERIOD_US 1500 // Amount of time in microseconds to delay |
|---|
| 48 | // between each light sensor sample. This is |
|---|
| 49 | // a balance between how fast and slow of |
|---|
| 50 | // a spinner reading that can be measured. |
|---|
| 51 | // The higher this value the slower a spinner |
|---|
| 52 | // you can measure, but at the tradeoff of |
|---|
| 53 | // not accurately measuring fast spinners. |
|---|
| 54 | // Low values (even 0) mean very fast speeds |
|---|
| 55 | // can be detected but slow speeds (below 10hz) |
|---|
| 56 | // are harder to detect. You can increase the |
|---|
| 57 | // sample depth to help improve the range |
|---|
| 58 | // of detection speeds, but there's a limit |
|---|
| 59 | // based on the memory available. |
|---|
| 60 | |
|---|
| 61 | #define THRESHOLD 400 // How big the amplitude of a cyclic |
|---|
| 62 | // signal has to be before the measurement |
|---|
| 63 | // logic kicks in. This is a value from |
|---|
| 64 | // 0 to 1023 and might need to be adjusted |
|---|
| 65 | // up or down if the detection is too |
|---|
| 66 | // sensitive or not sensitive enough. |
|---|
| 67 | // Raising this value will make the detection |
|---|
| 68 | // less sensitive and require a very large |
|---|
| 69 | // difference in amplitude (i.e. a very close |
|---|
| 70 | // or highly reflective spinner), and lowering |
|---|
| 71 | // the value will make the detection more |
|---|
| 72 | // sensitive and potentially pick up random |
|---|
| 73 | // noise from light in the room. |
|---|
| 74 | |
|---|
| 75 | #define MEASURE_PERIOD_MS 1000 // Number of milliseconds to wait |
|---|
| 76 | // between measurements. Default is |
|---|
| 77 | // one second (1000 milliseconds). |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | void setup() { |
|---|
| 81 | // Initialize serial monitor at 115200 baud. |
|---|
| 82 | Serial.begin(115200); |
|---|
| 83 | |
|---|
| 84 | // Initialize CircuitPlayground library and turn on the first 3 pixels (near the light sensor) |
|---|
| 85 | // to pure white at maximum brightness. |
|---|
| 86 | CircuitPlayground.begin(255); // 255 means set pixel colors to max brightness. |
|---|
| 87 | CircuitPlayground.clearPixels(); |
|---|
| 88 | CircuitPlayground.setPixelColor(0, 255, 255, 255); // Set pixel 0, 1, 2 to white. |
|---|
| 89 | CircuitPlayground.setPixelColor(1, 255, 255, 255); |
|---|
| 90 | CircuitPlayground.setPixelColor(2, 255, 255, 255); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | void loop() { |
|---|
| 95 | // Pause between measurements. |
|---|
| 96 | delay(MEASURE_PERIOD_MS); |
|---|
| 97 | |
|---|
| 98 | // Collect samples from the light sensor as quickly as possible. |
|---|
| 99 | // Keep track of the amount of time (in microseconds) between samples |
|---|
| 100 | // so the sampling frequency can later be determined. |
|---|
| 101 | uint16_t samples[SAMPLE_DEPTH] = {0}; |
|---|
| 102 | uint32_t start = micros(); |
|---|
| 103 | for (int i=0; i<SAMPLE_DEPTH; ++i) { |
|---|
| 104 | samples[i] = CircuitPlayground.lightSensor(); |
|---|
| 105 | delayMicroseconds(SAMPLE_PERIOD_US); |
|---|
| 106 | } |
|---|
| 107 | uint32_t elapsed_uS = micros() - start; |
|---|
| 108 | float elapsed = elapsed_uS / 1000000.0; // Elapsed time in seconds. |
|---|
| 109 | |
|---|
| 110 | // Find the min and max values in the collected samples. |
|---|
| 111 | uint16_t minval = samples[0]; |
|---|
| 112 | uint16_t maxval = samples[0]; |
|---|
| 113 | for (int i=1; i<SAMPLE_DEPTH; ++i) { |
|---|
| 114 | minval = min(minval, samples[i]); |
|---|
| 115 | maxval = max(maxval, samples[i]); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | // Check the amplitude of the signal (difference between min and max) |
|---|
| 119 | // is greater than the threshold to continue detecting speed. |
|---|
| 120 | uint16_t amplitude = maxval - minval; |
|---|
| 121 | if (amplitude < THRESHOLD) { |
|---|
| 122 | // Didn't make it past the threshold so start over with another measurement attempt. |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | // Compute midpoint of the signal (halfway between min and max values). |
|---|
| 127 | uint16_t midpoint = minval + (amplitude/2); |
|---|
| 128 | |
|---|
| 129 | // Count how many midpoint crossings were found in the signal. |
|---|
| 130 | // These are instances where two readings either straddle or land on |
|---|
| 131 | // the midpoint. The midpoint crossings will happen twice for every |
|---|
| 132 | // complete sine wave cycle (once going up and again coming down). |
|---|
| 133 | int crossings = 0; |
|---|
| 134 | for (int i=1; i<SAMPLE_DEPTH; ++i) { |
|---|
| 135 | uint16_t p0 = samples[i-1]; |
|---|
| 136 | uint16_t p1 = samples[i]; |
|---|
| 137 | if ((p1 == midpoint) || |
|---|
| 138 | ((p0 < midpoint) && (p1 > midpoint)) || |
|---|
| 139 | ((p0 > midpoint) && (p1 < midpoint))) { |
|---|
| 140 | crossings += 1; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | // Compute signal frequency, RPM, and period. |
|---|
| 145 | // The period is the amount of time it takes for a complete |
|---|
| 146 | // sine wave cycle to occur. You can calculate this by dividing the |
|---|
| 147 | // amount of time that elapsed during the measurement period by the |
|---|
| 148 | // number of midpoint crossings cut in half (because each complete |
|---|
| 149 | // sine wave cycle will have 2 midpoint crossings). However since |
|---|
| 150 | // fidget spinners have multiple arms you also divide by the number |
|---|
| 151 | // of arms to normalize the period into a value that represents the |
|---|
| 152 | // time taken for a complete revolution of the entire spinner, not |
|---|
| 153 | // just the time between one arm and the next. |
|---|
| 154 | float period = elapsed / (crossings / SPINNER_ARMS / 2.0); |
|---|
| 155 | |
|---|
| 156 | // Once the period is calculated it can be converted into a frequency |
|---|
| 157 | // value (i.e revolutions per second, how many times the spinner spins |
|---|
| 158 | // around per second) and more common RPM value (revolutions per minute, |
|---|
| 159 | // just multiply frequency by 60 since there are 60 seconds in a minute). |
|---|
| 160 | float frequency = 1.0 / period; |
|---|
| 161 | float rpm = frequency * 60.0; |
|---|
| 162 | |
|---|
| 163 | // Print out the measured values! |
|---|
| 164 | Serial.print("Frequency: "); |
|---|
| 165 | Serial.print(frequency, 3); |
|---|
| 166 | Serial.print(" (hz)\t\tRPM: "); |
|---|
| 167 | Serial.print(rpm, 3); |
|---|
| 168 | Serial.print("\t\tPeriod: "); |
|---|
| 169 | Serial.print(period, 3); |
|---|
| 170 | Serial.println(" (seconds)"); |
|---|
| 171 | } |
|---|