An hourglass does one thing, count a fixed period time. We can do the same thing very easily with the Circuit Playground using the delay()
function from the core Arduino library. This function simply takes an amount of time, in milliseconds, and waits that amount of time.
So we can make a basic timer by turning on all of the NeoPixels, waiting the specified amount of time using delay()
, and then turning off all of the NeoPixels to indicate the time has elapsed.
Easy peasy lemony squeezy, here's the code to do that:
/////////////////////////////////////////////////////////////////////////////// // Circuit Playground 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); } // Wait 5 seconds (5000 milliseconds) delay(5000); // Turn OFF all the NeoPixels CircuitPlayground.clearPixels(); // Wait for button press to reset timer while (!CircuitPlayground.leftButton() && !CircuitPlayground.rightButton()) { // Do nothing, just waiting for a button press... } }
In the code above, the timer was hard coded for 5 seconds. Since there are 1000 milliseconds in a second, we need to multiply 5 by 1000 to get the value for delay()
.
5 seconds X 1000 milliseconds/second = 5000 milliseconds
Text editor powered by tinymce.