Skip to content

Instantly share code, notes, and snippets.

@jimblom
Last active October 5, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimblom/41d501d683e820b28b686168a540618c to your computer and use it in GitHub Desktop.
Save jimblom/41d501d683e820b28b686168a540618c to your computer and use it in GitHub Desktop.
Use a SparkFun DeadOn RTC and three analog panel meters to display the time
/******************************************************************************
RTC_Analog_Gauge_Clock.ino
Jim Lindblom @ SparkFun Electronics
original creation date: October 4, 2016
Uses a DeadOn RTC Module -- based around the DS3234 -- to
count time. Displays the hours, minutes, and seconds on a
trio of analog panel meters:
https://www.sparkfun.com/products/10285
A potentiometers and three switches can be used to set the
time.
Hardware Hookup:
DeadOn RTC: SS-10, MOSI-11, MISO-12, CLK-13, VCC-5V, GND-GND
(DeadOn RTC uses SPI to communicate)
Analog Panel Meters:
Hour: 6 (Must be PWM-capable)
Minute: 5 (Must be PWM-capable)
Second: 3 (Must be PWM-capable)
Buttons: (All active-low -- other end connected to GND)
Hour: 8
Minute: 7
Second: 4
Potentiometer: A0 (Other 2 pins connected to 5V and GND)
To set the time, hold one of the buttons down and twist
the potentiometer. Release the button to set.
Resources:
SparkFunDS3234RTC Arduino Library
SPI.h - Arduino SPI Library
Development environment specifics:
Arduino 1.6.11
SparkFun RedBoard
SparkFun Real Time Clock Module (v14)
*****************************************************************************/
#include <SPI.h>
#include <SparkFunDS3234RTC.h>
// Comment out the line below if you want month printed before date.
// E.g. October 31, 2016: 10/31/16 vs. 31/10/16
#define PRINT_USA_DATE
#define SEC_OUT 3 // Second Panel Meter on D3
#define MIN_OUT 5 // Minute panel meter on D5
#define HOUR_OUT 6 // Hour panel meter on D6
#define SEC_IN 4 // Second button-set on D4
#define MIN_IN 7 // Minute button-set on D7
#define HOUR_IN 8 // Hour button-set on D8
#define A_IN A0 // Potentiometer on A0
// Put those pins into arrays, so we can access them in
// for loops
int analogOutputPins[3] = {SEC_OUT, MIN_OUT, HOUR_OUT};
int inputSetPins[3] = {SEC_IN, MIN_IN, HOUR_IN};
//////////////////////////////////
// Configurable Pin Definitions //
//////////////////////////////////
#define DS13074_CS_PIN 10 // DeadOn RTC Chip-select pin
void setup()
{
// Setup button, panel meter, and potentiometer pins
for (int i=0; i<3; i++)
{
pinMode(analogOutputPins[i], OUTPUT);
pinMode(inputSetPins[i], INPUT_PULLUP);
}
pinMode(A_IN, INPUT);
// Use the serial monitor to debug time/date output
Serial.begin(9600);
// Call rtc.begin([cs]) to initialize the library
// The chip-select pin should be sent as the only parameter
rtc.begin(DS13074_CS_PIN);
rtc.set24Hour(); // Set to 24-hour mode
//rtc.autoTime(); // Can set time to compiler time
}
void loop()
{
static int8_t lastSecond = -1;
// Call rtc.update() to update all rtc.seconds(),
// rtc.minutes(), etc. return functions.
rtc.update();
// If the second has changed, update displays, print time
if (rtc.second() != lastSecond)
{
printTime(); // Print the new time
float midHour = (float) rtc.hour() + ((float) rtc.minute() / 60.0);
analogWrite(SEC_OUT, ((float)rtc.second() / 60.0) * 255);
analogWrite(MIN_OUT, ((float)rtc.minute() / 60.0) * 255);
analogWrite(HOUR_OUT, (midHour / 24.0) * 255);
lastSecond = rtc.second(); // Update lastSecond value
}
// Check time-setting buttons to update the time:
setNewTime();
}
void setNewTime()
{
// Run through all three buttons:
for (int i=0; i<3; i++)
{
if (!digitalRead(inputSetPins[i])) // If button is pressed
{
int newValue = 0;
while (!digitalRead(inputSetPins[i]))
{ // While button is held, continue to read in
// analog value and update on the meter:
newValue = analogRead(A_IN);
newValue = map(newValue, 0, 1023, 0, 255);
analogWrite(analogOutputPins[i], newValue);
}
// Once the button is released, update the proper
// time and display it on the meter
switch (i)
{
case 0:
newValue = map(newValue, 0, 255, 0, 59);
rtc.setSecond(newValue);
Serial.println("Set seconds to : " + String(newValue));
break;
case 1:
newValue = map(newValue, 0, 255, 0, 59);
rtc.setMinute(newValue);
Serial.println("Set minutes to : " + String(newValue));
break;
case 2:
newValue = map(newValue, 0, 255, 0, 24);
rtc.setHour(newValue);
Serial.println("Set hours to : " + String(newValue));
break;
}
}
}
}
void printTime()
{
Serial.print(String(rtc.hour()) + ":"); // Print hour
if (rtc.minute() < 10)
Serial.print('0'); // Print leading '0' for minute
Serial.print(String(rtc.minute()) + ":"); // Print minute
if (rtc.second() < 10)
Serial.print('0'); // Print leading '0' for second
Serial.print(String(rtc.second())); // Print second
if (rtc.is12Hour()) // If we're in 12-hour mode
{
// Use rtc.pm() to read the AM/PM state of the hour
if (rtc.pm()) Serial.print(" PM"); // Returns true if PM
else Serial.print(" AM");
}
Serial.print(" | ");
// Few options for printing the day, pick one:
Serial.print(rtc.dayStr()); // Print day string
//Serial.print(rtc.dayC()); // Print day character
//Serial.print(rtc.day()); // Print day integer (1-7, Sun-Sat)
Serial.print(" - ");
#ifdef PRINT_USA_DATE
Serial.print(String(rtc.month()) + "/" + // Print month
String(rtc.date()) + "/"); // Print date
#else
Serial.print(String(rtc.date()) + "/" + // (or) print date
String(rtc.month()) + "/"); // Print month
#endif
Serial.println(String(rtc.year())); // Print year
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment