Skip to content

Instantly share code, notes, and snippets.

@danricho
Last active July 12, 2020 10:42
Show Gist options
  • Save danricho/42a5ce996bde3d7e731314fc23f730d6 to your computer and use it in GitHub Desktop.
Save danricho/42a5ce996bde3d7e731314fc23f730d6 to your computer and use it in GitHub Desktop.
danricho.com example code
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
byte column[8] = {128,64,32,16,8,4,2,1};
byte row[8];
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
for (int character = 0; character < 11; character ++){
switch (character) {
case 0:
row[0] = 0b00000000;
row[1] = 0b00011000;
row[2] = 0b00100100;
row[3] = 0b00101100;
row[4] = 0b00110100;
row[5] = 0b00100100;
row[6] = 0b00011000;
row[7] = 0b00000000;
break;
case 1:
row[0] = 0b00000000;
row[1] = 0b00010000;
row[2] = 0b00110000;
row[3] = 0b00010000;
row[4] = 0b00010000;
row[5] = 0b00010000;
row[6] = 0b00111000;
row[7] = 0b00000000;
break;
case 2:
row[0] = 0b00000000;
row[1] = 0b00011000;
row[2] = 0b00100100;
row[3] = 0b00000100;
row[4] = 0b00011000;
row[5] = 0b00100000;
row[6] = 0b00111100;
row[7] = 0b00000000;
break;
case 3:
row[0] = 0b00000000;
row[1] = 0b00111000;
row[2] = 0b00000100;
row[3] = 0b00011000;
row[4] = 0b00000100;
row[5] = 0b00000100;
row[6] = 0b00111000;
row[7] = 0b00000000;
break;
case 4:
row[0] = 0b00000000;
row[1] = 0b00100000;
row[2] = 0b00101000;
row[3] = 0b00101000;
row[4] = 0b00111100;
row[5] = 0b00001000;
row[6] = 0b00001000;
row[7] = 0b00000000;
break;
case 5:
row[0] = 0b00000000;
row[1] = 0b00111100;
row[2] = 0b00100000;
row[3] = 0b00111000;
row[4] = 0b00000100;
row[5] = 0b00000100;
row[6] = 0b00111000;
row[7] = 0b00000000;
break;
case 6:
row[0] = 0b00000000;
row[1] = 0b00011100;
row[2] = 0b00100000;
row[3] = 0b00111000;
row[4] = 0b00100100;
row[5] = 0b00100100;
row[6] = 0b00011000;
row[7] = 0b00000000;
break;
case 7:
row[0] = 0b00000000;
row[1] = 0b00111100;
row[2] = 0b00000100;
row[3] = 0b00001000;
row[4] = 0b00010000;
row[5] = 0b00010000;
row[6] = 0b00010000;
row[7] = 0b00000000;
break;
case 8:
row[0] = 0b00000000;
row[1] = 0b00011000;
row[2] = 0b00100100;
row[3] = 0b00011000;
row[4] = 0b00100100;
row[5] = 0b00100100;
row[6] = 0b00011000;
row[7] = 0b00000000;
break;
case 9:
row[0] = 0b00000000;
row[1] = 0b00011000;
row[2] = 0b00100100;
row[3] = 0b00100100;
row[4] = 0b00011100;
row[5] = 0b00000100;
row[6] = 0b00111000;
row[7] = 0b00000000;
break;
case 10:
row[0] = 0b00000000;
row[1] = 0b00100100;
row[2] = 0b00100100;
row[3] = 0b00000000;
row[4] = 0b00000000;
row[5] = 0b00100100;
row[6] = 0b00011000;
row[7] = 0b00000000;
break;
}
for (int refreshed = 0; refreshed < 255; refreshed++){
for (int count = 0; count < 8; count ++){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, column[count]);
shiftOut(dataPin, clockPin, MSBFIRST, 0b11111111-row[count]);
digitalWrite(latchPin, HIGH);
}
}
}
}
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
void setup() {
Serial.begin(9600); // start the serial connectio
n to the PC.
pinMode(2, INPUT); // set pin 2 as an input
digitalWrite(2, HIGH); // activate internal pullup resistor on pin 2
pinMode(3, INPUT); // set pin 3 as an input
digitalWrite(3, HIGH); // activate internal pullup resistor on pin 3
pinMode(4, INPUT); // set pin 4 as an input
digitalWrite(4, HIGH); // activate internal pullup resistor on pin 4
setSyncProvider(RTC.get); // the function to get the time from the RTC
setSyncInterval(60); // set the number of seconds between re-sync
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC.");
else
Serial.println("RTC has been read.");
}
void loop()
{
if (digitalRead(2) == LOW) { // Zero the minutes!
// setting the onboard time to 0 minutes.
setTime(hour(),0,second(),day(),month(), year());
RTC.set(now()); // sending the onboard time to the RTC.
Serial.println("Minutes Reset."); // send confirmation to the PC.
}
// *** had to adjust the adjustTime function in the Time library.
// Inside Time.cpp, the adjustTime method was replaced with:
//
// void adjustTime(long adjustment){
// setTime(sysTime + adjustment);
// }
//
if (digitalRead(3) == LOW) { // add an hour!
adjustTime(3600); // adjust onboard time by +3600 seconds (1hr) ***
RTC.set(now()); // sending the onboard time to the RTC.
Serial.println("Added an hour."); // send confirmation to the PC.
}
if (digitalRead(4) == LOW) { // add a minute!
adjustTime(60); // adjust onboard time by +60 seconds (1min) ***
RTC.set(now()); // sending the onboard time to the RTC.
Serial.println("Added a minute."); // send confirmation to the PC.
}
// this section is for setting the rtc chip from the serial terminal.
if(Serial.available()){
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
digitalClockDisplay(); // sends an update of the time to the PC.
delay(1000); // wait for a bit.
}
void digitalClockDisplay(){
// digital clock display of the time over serial
Serial.print(hour());
Serial.print(":");
printDigits(minute());
Serial.print(":");
printDigits(second());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment