Skip to content

Instantly share code, notes, and snippets.

@mohitbhoite
Created February 27, 2019 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohitbhoite/10d9e2f0ab787a15e5c19150e4cb0e9c to your computer and use it in GitHub Desktop.
Save mohitbhoite/10d9e2f0ab787a15e5c19150e4cb0e9c to your computer and use it in GitHub Desktop.
#include <RTClib.h>
#include <Adafruit_DotStar.h>
#include <MAX7313.h>
#include "Wire.h"
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial Serial
#endif
#define NUMPIXELS 1 // Number of LEDs in strip
// Here's how to control the LEDs from any two pins:
#define DATAPIN 7
#define CLOCKPIN 8
RTC_PCF8523 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Adafruit_DotStar dot = Adafruit_DotStar(
NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
WCN_MAX7313 maxim0;
WCN_MAX7313 maxim1;
WCN_MAX7313 maxim2;
WCN_MAX7313 maxim3;
byte segment[] = { 0b11011110, //0
0b00000110, //1
0b11101010, //2
0b01101110, //3
0b00110110, //4
0b01111100, //5
0b11111100, //6
0b00001110, //7
0b11111110, //8
0b01111110};//9
int decimalState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
//Serial.begin(9600);
Serial.begin(57600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized())
{
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
maxim0.begin(0x27);
maxim1.begin(0x20);
maxim2.begin(0x21);
maxim3.begin(0x25);
delay(100);
maxim0.pinMode(0,OUTPUT);
maxim0.pinMode(1,OUTPUT);
maxim0.pinMode(2,OUTPUT);
maxim0.pinMode(3,OUTPUT);
maxim0.pinMode(4,OUTPUT);
maxim0.pinMode(5,OUTPUT);
maxim0.pinMode(6,OUTPUT);
maxim0.pinMode(7,OUTPUT);
maxim1.pinMode(0,OUTPUT);
maxim1.pinMode(1,OUTPUT);
maxim1.pinMode(2,OUTPUT);
maxim1.pinMode(3,OUTPUT);
maxim1.pinMode(4,OUTPUT);
maxim1.pinMode(5,OUTPUT);
maxim1.pinMode(6,OUTPUT);
maxim1.pinMode(7,OUTPUT);
maxim2.pinMode(0,OUTPUT);
maxim2.pinMode(1,OUTPUT);
maxim2.pinMode(2,OUTPUT);
maxim2.pinMode(3,OUTPUT);
maxim2.pinMode(4,OUTPUT);
maxim2.pinMode(5,OUTPUT);
maxim2.pinMode(6,OUTPUT);
maxim2.pinMode(7,OUTPUT);
maxim3.pinMode(0,OUTPUT);
maxim3.pinMode(1,OUTPUT);
maxim3.pinMode(2,OUTPUT);
maxim3.pinMode(3,OUTPUT);
maxim3.pinMode(4,OUTPUT);
maxim3.pinMode(5,OUTPUT);
maxim3.pinMode(6,OUTPUT);
maxim3.pinMode(7,OUTPUT);
maxim0.allOutputOff();
maxim1.allOutputOff();
maxim2.allOutputOff();
maxim3.allOutputOff();
dot.begin(); // Initialize pins for output
dot.show(); // Turn all LEDs off ASAP
dot.setPixelColor(0,150,0,255);
dot.show(); // Turn all LEDs off ASAP
}
void loop() {
DateTime now = rtc.now();
uint8_t hour = now.hour();
uint8_t hour12 = now.hour()%12 == 0? 12 : now.hour()%12;
uint8_t minute = now.minute();
//display_hour((hour/10%10),(hour%10));
display_hour((hour12/10%10),(hour12%10));
display_minute((minute/10%10),(minute%10));
//delay(1000);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (decimalState == LOW) {
decimalState = HIGH;
maxim0.analogWrite(0,220);
maxim1.analogWrite(0,220);
} else {
decimalState = LOW;
maxim0.analogWrite(0,50);
maxim1.analogWrite(0,50);
}
}
//
// Serial.print(now.year(), DEC);
// Serial.print('/');
// Serial.print(now.month(), DEC);
// Serial.print('/');
// Serial.print(now.day(), DEC);
// Serial.print(" (");
// Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Serial.print(") ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
// Serial.println();
}
void display_hour(uint8_t hourL,uint8_t hourR)
{
for (int bits = 7; bits > 0; bits--) {
// Compare bits 7-0 in byte
if (segment[hourL] & (1 << bits)) {
maxim3.analogWrite(bits,220);
}
else {
maxim3.analogWrite(bits,50);
}
}
for (int bits = 7; bits > 0; bits--) {
// Compare bits 7-0 in byte
if (segment[hourR] & (1 << bits)) {
maxim2.analogWrite(bits,220);
}
else {
maxim2.analogWrite(bits,50);
}
}
}
void display_minute(uint8_t minuteL, uint8_t minuteR)
{
for (int bits = 7; bits > 0; bits--) {
// Compare bits 7-0 in byte
if (segment[minuteL] & (1 << bits)) {
maxim1.analogWrite(bits,220);
}
else {
maxim1.analogWrite(bits,50);
}
}
for (int bits = 7; bits > 0; bits--) {
// Compare bits 7-0 in byte
if (segment[minuteR] & (1 << bits)) {
maxim0.analogWrite(bits,220);
}
else {
maxim0.analogWrite(bits,50);
}
}
}
void display_digit(byte digit)
{
for (int bits = 7; bits > -1; bits--) {
// Compare bits 7-0 in byte
if (segment[digit] & (1 << bits)) {
maxim0.analogWrite(bits,200);
maxim1.analogWrite(bits,200);
maxim2.analogWrite(bits,200);
maxim3.analogWrite(bits,200);
}
else {
maxim0.analogWrite(bits,0);
maxim1.analogWrite(bits,0);
maxim2.analogWrite(bits,0);
maxim3.analogWrite(bits,0);
}
}
}
void breathe(uint8_t wait)
{
for (int i = 0;i<255;i++)
{
maxim0.analogWrite(1,i);
maxim1.analogWrite(1,i);
maxim2.analogWrite(1,i);
maxim3.analogWrite(1,i);
delay(wait);
}
for (int j = 255;j>-1;j--)
{
maxim0.analogWrite(1,j);
maxim1.analogWrite(1,j);
maxim2.analogWrite(1,j);
maxim3.analogWrite(1,j);
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<dot.numPixels(); i++) {
dot.setPixelColor(i, Wheel((i+j) & 255));
}
dot.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return dot.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return dot.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return dot.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment