Created
June 2, 2018 13:17
-
-
Save davedarko/445c8a93e67facfebbdba4f223dc1fb7 to your computer and use it in GitHub Desktop.
Unexpected Maker Neo7Segment binary clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Time.h> | |
#include <TimeLib.h> | |
#include <DS3232RTC.h> | |
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
#define PIN 6 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(29, PIN, NEO_GRB + NEO_KHZ800); | |
//uint8_t posfix[] = { | |
// 3, 4, 11, 12, | |
// 2, 5, 10, 13, | |
// 1, 6, 9, 14, | |
// 0, 7, 8, 15 | |
//}; | |
//// 4x4 matrix | |
//uint8_t posfix[] = { | |
// 0, 1, 2, 3, | |
// 7, 6, 5, 4, | |
// 8, 9, 10, 11, | |
// 15, 14, 13, 12 | |
//}; | |
// unexpected maker | |
uint8_t posfix[] = { | |
7, 6, 5, 4, | |
11, 10, 9, 8, | |
20, 21, 22, 23, | |
16, 17, 18, 19 | |
}; | |
uint32_t colors[] = { | |
0x00000000, // black | |
0x00040100, // brown | |
0x00080000, // red | |
0x000B0300, // orange | |
0x000A0900, // yellow | |
0x00000800, // green | |
0x00000008, // blue | |
0x00040008, // purple | |
0x00020202, // grey | |
0x000C0A0A, // white | |
}; | |
char formatted[] = "00-00-00 00:00:00x"; | |
int ft_year = 2014; | |
int ft_month = 01; | |
int ft_day = 24; | |
int ft_hours = 23; | |
int ft_minut = 45; | |
int ft_seconds = 40; | |
void setup() { | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() | |
{ | |
if(Serial.available()) { | |
processCommand(); | |
} | |
time_t myTime; | |
myTime = RTC.get(); | |
tmElements_t tm; | |
RTC.read(tm); | |
ft_year = 1970+tm.Year; | |
ft_month = tm.Month; | |
ft_day = tm.Day; | |
ft_hours = tm.Hour; | |
ft_minut = tm.Minute; | |
ft_seconds = tm.Second; | |
print_data(); | |
setClockColor(); | |
delay(1000); | |
} | |
void setClockColor() | |
{ | |
uint8_t dh = ft_hours/10; | |
uint8_t h = ft_hours%10; | |
uint8_t dm = ft_minut/10; | |
uint8_t m = ft_minut%10; | |
uint8_t digits[] = {dh,h,dm,m}; | |
for (uint8_t digit=0; digit<4; digit++) | |
{ | |
for (uint8_t bitpos=0; bitpos<4; bitpos++) | |
{ | |
uint8_t pos = digit*4 + bitpos; | |
if (digits[digit] & 1<<bitpos) | |
{ | |
strip.setPixelColor( | |
posfix[pos], | |
colors[digits[digit]] | |
); | |
} | |
else | |
{ | |
strip.setPixelColor( | |
posfix[pos], | |
0 | |
); | |
} | |
} | |
} | |
strip.show(); | |
} | |
// Fill the dots one after the other with a color | |
void colorWipe(uint32_t c, uint8_t wait) { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(posfix[i], c); | |
strip.show(); | |
delay(wait); | |
} | |
} | |
void processCommand() { | |
if(!Serial.available()) { return; } | |
char command = Serial.read(); | |
tmElements_t tm; | |
int in,in2; | |
switch(command) | |
{ | |
case 'q': | |
RTC.squareWave(SQWAVE_1_HZ); | |
Serial.println("Square wave output set to 1Hz"); | |
break; | |
case 'h': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Hour = in; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 'i': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Minute = in; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 's': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Second = in; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 'y': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Year = in+ 30; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 'm': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Month = in; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 'd': | |
in=SerialReadPosInt(); | |
RTC.read(tm); | |
tm.Day = in; | |
RTC.write(tm); | |
print_data(); | |
break; | |
case 'g': | |
print_data(); | |
break; | |
default: | |
Serial.println(" h## - set Hours d## - set Date"); | |
Serial.println(" i## - set mInutes m## - set Month"); | |
Serial.println(" s## - set Seconds y## - set Year"); | |
Serial.println(" q - SQW/OUT = 1Hz"); | |
Serial.println(); | |
break; | |
}//switch on command | |
} | |
//read in numeric characters until something else | |
//or no more data is available on serial. | |
int SerialReadPosInt() | |
{ | |
int i = 0; | |
boolean done=false; | |
while(Serial.available() && !done) | |
{ | |
char c = Serial.read(); | |
if (c >= '0' && c <='9') | |
{ | |
i = i * 10 + (c-'0'); | |
} | |
else | |
{ | |
done = true; | |
} | |
} | |
return i; | |
} | |
void print_data() | |
{ | |
Serial.println(); | |
Serial.print(ft_year); | |
Serial.print("-"); | |
Serial.print(ft_month); | |
Serial.print("-"); | |
Serial.print(ft_day); | |
Serial.print(" "); | |
Serial.print(ft_hours); | |
Serial.print(":"); | |
Serial.print(ft_minut); | |
Serial.print(":"); | |
Serial.print(ft_seconds); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment