Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Particle Photon + Neopixels countdown
/**
Parts:
- Particle Photon (wireless Arduino):
https://store.particle.io/collections/wifi/products/photon
- RGB LEDs:
https://www.amazon.com/gp/product/B00ZHB9M6A/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1
- Logic Level shifter (makes the 3.3v Photon play nice w/ the 5V RGB LEDs)
https://www.amazon.com/gp/product/B00NAY3J7O/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1
- 5V power supply
https://www.amazon.com/gp/product/B0852HL336/ref=ppx_yo_dt_b_asin_title_o08_s01?ie=UTF8&psc=1
- Lots of jumper cable
https://www.amazon.com/gp/product/B01EV70C78/ref=ppx_yo_dt_b_asin_title_o05_s00?ie=UTF8&psc=1
**/
// Most of this is code-thieved from the sample code @
// https://github.com/technobly/Particle-NeoPixel/blob/master/examples/a-rainbow/a-rainbow.cpp
#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;
unsigned short hue = 0;
unsigned long lastSync = millis(); // When was the last time we synched our local clock?
float ledBrightness = 1;
long timeDiffSec;
int timeDiffDays;
String diffAsString; // The number of remaining days, as a string.
String char1;
String char2;
// 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] = {false, false, true, 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};
#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 + 1. This should make it flip @ midnight every night CST.
unsigned long INAUGURATION_SEC = 1611208860;
void setup() {
Time.zone(-6);
strip.begin();
strip.show();
}
// I couldn't get memcpy working, so I walked around the hurdle like an adult.
// Ideally, we'd copy the array in the switch & iterate over it below.
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);
}
}
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();
// Wait five seconds before checking if the day has changed, and cycling the color slowly.
delay(5000);
}
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