Skip to content

Instantly share code, notes, and snippets.

@SyncChannel
Created March 30, 2016 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SyncChannel/9391d5c73a6b27b99562998c4a75a8fb to your computer and use it in GitHub Desktop.
Save SyncChannel/9391d5c73a6b27b99562998c4a75a8fb to your computer and use it in GitHub Desktop.
FeatherClock for Adafruit Feather
/* FeatherClock
* Quick and easy clock using the Adafruit Feather M0, OLED FeatherWing, and RTC FeatherWing
*
* By: Dan Watson
* syncchannel.blogspot.com
* 3-29-2016
*
* List of hardware you need:
* - Adafruit Feather (I used Feather M0 here, others will work with mods to the program)
* (https://www.adafruit.com/products/2772)
* - Adafruit OLED FeatherWing
* (https://www.adafruit.com/products/2900)
* - RTC + SD Card Adalogger FeatherWing
* (https://www.adafruit.com/products/2922)
* - FeatherWing Doubler
* (https://www.adafruit.com/products/2890)
* - CR1220 Battery if desired to keep time when the USB cable is unplugged
* - Possibly extra headers, depending on how you want to assemble the boards on the Doubler
*
* Required Libraries:
* RTClib (https://github.com/adafruit/RTClib)
* SSD1306 Library (https://github.com/adafruit/Adafruit_SSD1306)
* Adafruit-GFX Library (https://github.com/adafruit/Adafruit-GFX-Library)
*/
#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Solder a wire between the "INT" pad on the Adalogger FeatherWing and the pad for Pin D5
// You can change this pin if you like. Nearly every pin on the M0 can be an interrupt
#define INTPIN 5
RTC_PCF8523 rtc;
Adafruit_SSD1306 display;
// Fix the OLED screen height
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
// Serial is only used for debugging
Serial.begin(9600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
while (1);
}
// We need a pull-up on the open-collector interrupt pin of the PCF8523
pinMode(INTPIN, INPUT_PULLUP);
// Turn on the 1 Hz square wave output to trigger updates of the time on the screen
rtc.writeSqwPinMode(PCF8523_SquareWave1HZ);
// Updates of the screen will be performed in the ISR
attachInterrupt(INTPIN, ISR, RISING);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Use this to set the date/time
// My prefered method is to type in a time a few seconds in the future and hit upload to set it
// Then comment this line out and re-upload the program
// As long as you have a Li-Po or CR1220 battery attached, the RTC will hold the date/time
// Values are (year, month, day, hour, minute, second)
rtc.adjust(DateTime(2016, 3, 29, 22, 45, 50));
}
void loop ()
{
// Do nothing!
}
/* This ISR is triggered at the start of each second by the Interrupt pin on the PCF8523. I'm just
* going to call my updateDisplay() routine directly. If you add more to this program, you might need
* to do it a bit smarter so we don't hang out in the ISR for too long.
*/
void ISR()
{
updateDisplay();
}
// This function updates the date and time on the OLED display
void updateDisplay()
{
// Get the date/time from the RTC
DateTime now = rtc.now();
// Buffers to assemble some strings to update the display
char timeBuffer[10];
char dateBuffer[24];
//Assemble the two strings needed to update the display
sprintf(timeBuffer, "%02d:%02d:%02d",now.hour(),now.minute(), now.second());
sprintf(dateBuffer, "%s %02d/%02d/%4d",daysOfTheWeek[now.dayOfTheWeek()],now.month(),now.day(),now.year());
// Time is displayed in larger text on the first line
display.setTextColor(WHITE);
display.setTextSize(2);
display.clearDisplay();
display.setCursor(4,2);
display.print(timeBuffer);
// The date is displayed in smaller text on the second line
display.setTextSize(1);
display.setCursor(4,22);
display.print(dateBuffer);
// Send it!
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment