Skip to content

Instantly share code, notes, and snippets.

@pbexe
Created February 4, 2017 20:38
Show Gist options
  • Save pbexe/17144e4680bacf3a301c0a420cda3533 to your computer and use it in GitHub Desktop.
Save pbexe/17144e4680bacf3a301c0a420cda3533 to your computer and use it in GitHub Desktop.
LED controller
#include <DS3231.h>
#include <LedControl.h>
#include <NewPing.h>
#define TRIGGER_PIN 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
DS3231 clock;
RTCDateTime dt;
int DIN = 12;
int CS = 11;
int CLK = 10;
byte quarter[8] = {0x41, 0x42, 0x44, 0x48, 0x15, 0x25, 0x47, 0x81};
byte to[8] = {0x0, 0x0, 0xe7, 0x45, 0x47, 0x0, 0x0, 0x0};
byte it[8] = {0x0, 0x77, 0x22, 0x22, 0x72, 0x0, 0x0, 0x0};
byte is[8] = {0x0, 0xee, 0x48, 0x4e, 0x42, 0xee, 0x0, 0x0};
byte a[8] = {0x18, 0x24, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x0};
byte p[8] = {0x3c, 0x42, 0x42, 0x42, 0x7c, 0x40, 0x40, 0x40};
byte s[8] = {0x3e, 0x40, 0x40, 0x3c, 0x2, 0x2, 0x7c, 0x0};
byte t[8] = {0x7f, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8};
byte half[8] = {0x21, 0x22, 0x24, 0x8, 0x17, 0x21, 0x42, 0x87};
byte one[8] = {0x18, 0x28, 0x8, 0x8, 0x8, 0x8, 0x8, 0x1c};
byte two[8] = {0x3c, 0x42, 0x44, 0x8, 0x10, 0x20, 0x40, 0x7e};
byte three[8] = {0x3c, 0x42, 0x42, 0xc, 0x2, 0x2, 0x42, 0x3c};
byte four[8] = {0x42, 0x42, 0x42, 0x42, 0x7e, 0x2, 0x2, 0x2};
byte five[8] = {0x3e, 0x20, 0x20, 0x2c, 0x32, 0x2, 0x2, 0x3c};
byte six[8] = {0x1c, 0x20, 0x40, 0x5c, 0x62, 0x42, 0x22, 0x1c};
byte seven[8] = {0x7e, 0x2, 0x4, 0x8, 0x8, 0x8, 0x8, 0x8};
byte eight[8] = {0x3c, 0x42, 0x42, 0x42, 0x3c, 0x42, 0x42, 0x3c};
byte nine[8] = {0x3c, 0x42, 0x42, 0x42, 0x3e, 0x2, 0x2, 0x2};
byte zero[8] = {0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c};
LedControl lc = LedControl(DIN, CLK, CS, 0);
void setup() {
Serial.begin(9600);
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
// Set sketch compiling time
//clock.setDateTime(__DATE__, __TIME__);
lc.shutdown(0, false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0, 5); // Set the brightness to 1/3
lc.clearDisplay(0); // and clear the display
}
void print_each_digit(int x)
{
if (x >= 10)
print_each_digit(x / 10);
int digit = x % 10;
printNumber(digit);
}
void printNumber(int number) {
if (number == 1) {
printByte(one);
}
else if (number == 2) {
printByte(two);
}
else if (number == 3) {
printByte(three);
}
else if (number == 4) {
printByte(four);
}
else if (number == 5) {
printByte(five);
}
else if (number == 6) {
printByte(six);
}
else if (number == 7) {
printByte(seven);
}
else if (number == 8) {
printByte(eight);
}
else if (number == 9) {
printByte(nine);
}
else if (number == 0) {
printByte(zero);
}
}
void runClock() {
dt = clock.getDateTime();
int hour = dt.hour;
int mins = dt.minute;
Serial.print("Raw data: ");
Serial.print(dt.year); Serial.print("-");
Serial.print(dt.month); Serial.print("-");
Serial.print(dt.day); Serial.print(" ");
Serial.print(dt.hour); Serial.print(":");
Serial.print(dt.minute); Serial.print(":");
Serial.print(dt.second); Serial.println("");
printByte(it);
printByte(is);
if (mins < 5)
{
print_each_digit(dt.hour % 12);
}
else if (mins < 10)
{
//1:05 IT IS FIVE MINUTES PAST ONE OCLOCK
print_each_digit(5);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 15)
{
print_each_digit(10);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 20)
{
printByte(a);
printByte(quarter);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 25)
{
print_each_digit(20);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 30)
{
print_each_digit(25);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 35)
{
printByte(half);
printByte(p);
printByte(a);
printByte(s);
printByte(t);
print_each_digit(dt.hour % 12);
}
else if (mins < 40)
{
print_each_digit(25);
printByte(to);
print_each_digit((dt.hour % 12) + 1);
}
else if (mins < 45)
{
print_each_digit(20);
printByte(to);
print_each_digit((dt.hour % 12) + 1);
}
else if (mins < 50)
{
printByte(a);
printByte(quarter);
printByte(to);
print_each_digit((dt.hour % 12) + 1);
}
else if (mins < 55)
{
print_each_digit(10);
printByte(to);
print_each_digit((dt.hour % 12) + 1);
}
else if (mins < 60)
{
print_each_digit(5);
printByte(to);
print_each_digit((dt.hour % 12) + 1);
}
byte smile[8] = {0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C};
printByte(smile);
lc.clearDisplay(0);
}
void loop() {
delay(100); // Wait 500ms between pings (about 2 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
int distance = uS / US_ROUNDTRIP_CM;
if (distance != 0) {
runClock();
}
}
void printByte(byte character [])
{
int i = 0;
for (i = 0; i < 8; i++)
{
lc.setRow(0, i, character[i]);
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment