Now let's see how to turn off the NeoPixels one at a time during the count time. The basic idea is shown in the figure below.
Our timer starts at time zero (t=0) and counts for a total amount of time T (t=T). Since we have 10 NeoPixels on the Circuit Playground, we can break that time period up in to 10 equal slices. Each slice has a wait time of DT, which is equal to the total time T, divide by the number of NeoPixels, thus:
DT = T / 10
The idea now is to loop over each NeoPixel and wait this smaller amount of time DT. Once that time has elapsed, turn the NeoPixel off and move on to the next NeoPixel, etc.
Here's the previous code modified to do this:
/////////////////////////////////////////////////////////////////////////////// // Circuit Playground Less Basic Timer // // 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 button press to reset timer while (!CircuitPlayground.leftButton() && !CircuitPlayground.rightButton()) { // Do nothing, just waiting for a button press... } }
As you can see, the delay()
command has been moved inside a loop. The loop runs 10 times, so the delay()
command gets called 10 times. However, the total amount of time counted remains the same 5 seconds as before.
Next Step
Hourglasses typically don't have buttons that are pressed to reset them like our Circuit Playground code is doing. An hourglass is reset by simply flipping it over and letting the sand run through in the other direction. How can we simulate this action with the Circuit Playground? Well, maybe we can use the accelerometer. Let's give that a try.
Text editor powered by tinymce.