Timed Events w/ Arduino

So I am charting new territories with my latest project… timing. I asked Adafruit to make a 60 LED NeoPixel ring and so now I can build my clock! I am working through some concepts dealing with timing and animation before I add Wifi or GPS to capture time.

Currently I am just using the Arduino time library to show the hours, minutes, seconds on the ring. Pretty basic. I just keep pushing the data to the Neopixel in every loop and the time just correlates directly.

#include <Adafruit_NeoPixel.h>   // http://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library
#include <Time.h>                // http://playground.arduino.cc/Code/Time

// NeoPixel Strip Data Out
#define PIN 6  

// NeoPixel Decleration
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// Color Definitions
uint32_t black = strip.Color(0, 0, 0);
uint32_t red   = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue  = strip.Color(0, 0, 255);
uint32_t white = strip.Color(255, 255, 255);

void setup() {
  // Initialize the NeoPixel
  strip.begin();
  strip.show(); 
  
  // Too damn bright
  strip.setBrightness(64);

  // Set internal time keeping as handled by the Time Library
  setTime(23,13,20,24,3,2014);
}

void loop() {
  // Set the layers of the clock and repeat
  clockFace(black,white);
  strip.setPixelColor(second(), blue);
  strip.setPixelColor(minute(), green);
  strip.setPixelColor(hourFormat12()*5, red);
  strip.show();
}

void clockFace(uint32_t back, uint32_t mark){
  for (uint16_t i=0; i < strip.numPixels(); i++) {
    if(i%5 == 0) {
      strip.setPixelColor(i, mark);
    } else {
      strip.setPixelColor(i, back);
    }
  }
}

https://www.dropbox.com/s/sqv97v7z2600h … .47.18.jpg

What I need some guidance is how to do animation, these are timed events. For example, if I want to have the seconds hand fade out and fade into the next position. How do I make the event happen exactly in that second? It seems to in-deterministic of a loop to appropriately do this because I would have to know how fast my loops should occur and will occur to add and subtract the appropriate fade in and fade out.

Suggestions?

Thanks,

Frank

Say the pixel has to fade from 0% to 100% brightness in 2 seconds. You assign the current timer value to a variable. The next time the loop comes around, you check how much time has passed since you set that variable. Let’s say that’s 0.09s - then you have to set your pixel’s brightness to 0.09 / 2 *100% = 0.045 * 100% = 4.5% of brightness. So basically, rather than having an inner loop setting absolute increments of brightness, use a loop (could be an outer loop, depending) that checks how much time has actually passed, and adjusts brightness based on that. Modulo can help streamline things there :slight_smile:

I thought of that but I couldn’t get past something that needed sleeping on. I need to start the timer when I detect that the seconds value has changed.

Hi,

I have been wanting to build a NeoPixel 60 ring clock myself.

I kind of gave up doing my own code, because I am a crappy programmer.

I found this great clock that was written to run on one of the adafruit trinkets.

I am using it with the DS3231 clock chip, which blows away the DS1307 in accuracy, and the library seems compatible so far. It is dead on as far as accuracy.

The only problem with this great clock, is there is no allowance for DST adjustments, or manual time setting, not that you need that much because fo the accuracy.

The animation of the pixels though is awesome, and is probably what you are looking for. I would forgo the great minimal nature of the code, to have some time setting features.

Check it out:

https://github.com/mchughj/ClockNeopixel60

I have written the author, and he would like to have some DST features, but I don’t think he has done any updates since this code.

I hope that you find it of interest!

Kirby Heintzelman