Let's add the flip detect logic from the previous section to the less basic timer code to make a basic hourglass.
Here's the code:
/////////////////////////////////////////////////////////////////////////////// // Circuit Playground Basic Hourglass // // Author: Carter Nelson // MIT License (https://opensource.org/licenses/MIT) #include <Adafruit_CircuitPlayground.h> /////////////////////////////////////////////////////////////////////////////// void setup() { // Initialize the Circuit Playground CircuitPlayground.begin(); } /////////////////////////////////////////////////////////////////////////////// void loop() { // Turn ON all the NeoPixels for (int p=0; p<10; p++) { CircuitPlayground.setPixelColor(p, 255, 255, 255); } // Compute DT unsigned long DT = 5000/10; // Turn OFF the NeoPixels one at a time, waiting DT each time for (int p=0; p<10; p++) { delay(DT); CircuitPlayground.setPixelColor(p, 0, 0, 0); } // Wait for Circuit Playground to be flipped over (face down) while (CircuitPlayground.motionZ() > 0) {}; // A little debounce delay(500); // Wait for Circuit Playground to be flipped back over (face up) while (CircuitPlayground.motionZ() < 0) {}; }
So at this point we have a basic working hourglass. It counts for a period of 5 seconds, turning off the NeoPixels one at a time, and can be reset by flipping the Circuit Playground over.
Changing Count Time
If you want to set the timer to a different amount of time, simply change this line of code:
unsigned long DT = 5000/10;
and replace 5000 with whatever value you want. However, a better way to do this is to define this value globally at the top of the code. Try this - first, modify the code by adding a #define statement near the top as shown below.
#include <Adafruit_CircuitPlayground.h> #define COUNT_TIME 5000 // milliseconds
Now change the line that computes DT as follows:
unsigned long DT = COUNT_TIME / 10;
This makes it easier to find and change the value in the future.
Next Step
Try setting COUNT_TIME to something longer than 5 seconds, like 30 seconds:
#define COUNT_TIME 30000 // milliseconds
Run this and watch what happens when the count gets to the end. It works fine, but since the total time (COUNT_TIME) is now longer, so is the delay (DT) at each NeoPixel. This makes it difficult to see the last bit of time elapsing. The last NeoPixel is on, then suddenly off.
So how can we indicate the time progress for each NeoPixel? How about fading them? That might work, let's give it a try.
Page last edited December 05, 2016
Text editor powered by tinymce.