Skip to content

Instantly share code, notes, and snippets.

@Meandmybadself
Created December 2, 2020 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meandmybadself/bbd1a26446f9f95f89dabbd07ab4b830 to your computer and use it in GitHub Desktop.
Save Meandmybadself/bbd1a26446f9f95f89dabbd07ab4b830 to your computer and use it in GitHub Desktop.
Inauguration Countdown Clock Photon + Neopixels.
// See https://github.com/technobly/Particle-NeoPixel
#include <neopixel.h>
// Needed for ceil / floor
#include <math.h>
#define ONE_DAY_MILLIS (24 * 60 * 60 * 1000)
unsigned short PIXELS_PER_SEGMENT = 9;
unsigned short SEGMENTS_PER_CHAR = 7;
// The on/off states for each number segment.
boolean NUMBER_0[7] = {true, true, true, false, true, true, false};
boolean NUMBER_1[7] = {false, false, true, false, false, false, true};
boolean NUMBER_2[7] = {true, true, false, true, true, true, false};
boolean NUMBER_3[7] = {false, true, true, true, false, true, true};
boolean NUMBER_4[7] = {false, false, true, true, true, false, true};
boolean NUMBER_5[7] = {false, true, true, true, true, true, false};
boolean NUMBER_6[7] = {true, true, true, true, true, true, false};
boolean NUMBER_7[7] = {true, false, false, false, false, true, true};
boolean NUMBER_8[7] = {true, true, true, true, true, true, true};
boolean NUMBER_9[7] = {false, true, true, true, true, true, true};
// When was the last time we synched our local clock?
unsigned long lastSync = millis();
String diffAsString; // The number of remaining days, as a string.
String char1;
String char2;
long timeDiffSec;
int timeDiffDays;
#define PIXEL_COUNT 126
#define PIXEL_PIN A2
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// January 20th, 2021 @ 12pm CST
unsigned long INAUGURATION_SEC = 1611165600;
void setup() {
strip.begin();
strip.show();
}
void setNumber(String num, unsigned short charIndex, uint32_t color) {
if (num.equals("0")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_0[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("1")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_1[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("2")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_2[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("3")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_3[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("4")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_4[i]) {
// l0 += "Turning on seg " + String(i);
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("5")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_5[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("6")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_6[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("7")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_7[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("8")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_8[i]) {
setSegmentColor(i, charIndex, color);
}
}
} else if (num.equals("9")) {
for(short i=0; i < SEGMENTS_PER_CHAR; i++) {
if (NUMBER_9[i]) {
setSegmentColor(i, charIndex, color);
}
}
}
}
void setSegmentColor(unsigned short segmentIndex, unsigned short charIndex, uint32_t color) {
unsigned short start = (PIXELS_PER_SEGMENT * segmentIndex) + (SEGMENTS_PER_CHAR * charIndex * PIXELS_PER_SEGMENT);
unsigned short end = start + PIXELS_PER_SEGMENT;
for (short i = start; i < end; i++) {
strip.setPixelColor(i, color);
}
}
unsigned short hue = 0;
void loop() {
if (millis() - lastSync > ONE_DAY_MILLIS) {
Spark.publish("Synching time");
Particle.syncTime();
lastSync = millis();
}
uint32_t color = Wheel(hue);
hue += 1;
hue = hue % 255;
int nowSec = Time.now();
long timeDiffMS = INAUGURATION_SEC - nowSec;
int timeDiffDays = ceil(timeDiffMS / 86400);
diffAsString = String(timeDiffDays);
char1 = diffAsString.charAt(0);
char2 = diffAsString.charAt(1);
setNumber(char1, 0, color);
setNumber(char2, 1, color);
strip.show();
delay(5000);
}
float ledBrightness = 1;
uint32_t Wheel(byte WheelPos) {
int red, green, blue;
if(WheelPos < 85) {
red = WheelPos * 3;
green = 255 - WheelPos * 3;
blue = 0;
}
else if(WheelPos < 170) {
WheelPos -= 85;
red = 255 - WheelPos * 3;
green = 0;
blue = WheelPos * 3;
}
else {
WheelPos -= 170;
red = 0;
green = WheelPos * 3;
blue = 255 - WheelPos * 3;
}
return strip.Color(ledBrightness * red, ledBrightness * green, ledBrightness * blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment