NeoPixel Clock: Seconds
Part of the Projects section of Coddy's Arduino journey. Lesson 21 of 24.
The last project is a clock with no numbers: a ring of 12 NeoPixels, like the 12 hours on a clock face, with each hand a coloured light. Pixel 0 is at the top, at 12 o'clock, and the numbers go round clockwise. Two buttons will set the time.
First the seconds hand. A minute has 60 seconds and the ring has 12 pixels, so the hand moves one pixel every 5 seconds.
seconds % 60 counts 0 to 59 and then starts again at 0, like the seconds on a clock. Whole-number division by 5 turns 0 to 59 into 0 to 11:
unsigned long seconds = millis() / 1000;
int secondPixel = (seconds % 60) / 5;On each pass of loop(), draw the ring from scratch: ring.clear(), set the hand's pixel, ring.show(). Without the clear, old positions stay lit and the ring fills up. Ten redraws a second are plenty, so the starter ends with delay(100).
Challenge
MediumThe ring is on pin 6. Draw the seconds hand:
- work out the seconds since power-on with
millis() / 1000 - the hand's pixel is
(seconds % 60) / 5 - clear the ring, light that pixel blue with
ring.Color(0, 0, 255), and show it
Keep the delay(100). Press Run and watch the hand go round, 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() {
// work out where the seconds hand is, and draw it in blue
delay(100); // redraw ten times a second
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
3Parking Sensor
Parking Sensor: DistanceParking Sensor: LightsParking Sensor: BeepsParking Sensor: Display6NeoPixel Clock
NeoPixel Clock: SecondsNeoPixel Clock: MinutesNeoPixel Clock: Mixing HandsNeoPixel Clock: Set the TimePractice on your own: Arduino playground