Menu
CoddyTech

NeoPixel Clock: Minutes

Part of the Projects section of Coddy's Arduino journey. Lesson 22 of 24.

The Uno does not know the time of day: millis() only counts from power-on. So the sketch starts the clock at a time written into it and adds the seconds since power-on. The last step adds buttons to set it.

Keep the time as one number, the seconds since midnight. 10:24:50 is 10 hours, 24 minutes and 50 seconds:

const unsigned long startTime = 10 * 3600UL + 24 * 60 + 50;
unsigned long now = startTime + millis() / 1000;

The UL after 3600 makes the sum an unsigned long. Without it the board multiplies in int, which stops at 32767, and 10 times 3600 does not fit.

The seconds and the minutes are remainders, so each starts again at 0 when it reaches 60:

int seconds = now % 60;
int minutes = (now / 60) % 60;

The minutes hand also moves one pixel every 5 minutes, minutes / 5, and is drawn in green.

challenge icon

Challenge

Medium

Add the minutes hand:

  • the clock starts at 10:24:50: startTime in seconds since midnight
  • now is startTime plus the seconds since power-on
  • seconds hand: (now % 60) / 5, blue
  • minutes hand: ((now / 60) % 60) / 5, green ring.Color(0, 255, 0)

Press Run: at 10:25 the minutes hand steps on. Then Check.

Try it yourself

#include <Adafruit_NeoPixel.h>

const int ringPin = 6;
const int pixelCount = 12;
Adafruit_NeoPixel ring(pixelCount, ringPin, NEO_GRB + NEO_KHZ800);

void setup() {
  ring.begin();
}

void loop() {
  unsigned long seconds = millis() / 1000;  // seconds since power-on
  int secondPixel = (seconds % 60) / 5;     // one pixel for every 5 seconds

  ring.clear();
  ring.setPixelColor(secondPixel, ring.Color(0, 0, 255));
  ring.show();
  delay(100);  // redraw ten times a second
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Projects

Practice on your own: Arduino playground