1 |
/**
|
2 |
* An Arduino Volt Meter Analog Clock based on a TinyRTC real time clock.
|
3 |
*
|
4 |
* For more info about this project, check out my blog for the full story
|
5 |
* images, and the endresult of the project:
|
6 |
* http://michaelteeuw.nl/post/174972004187/what-time-is-it- fathers-day
|
7 |
*
|
8 |
* LICENSE: Permission is hereby granted, free of charge, to any person
|
9 |
* obtaining a copy of this software and associated documentation files
|
10 |
* (the "Software"). For more info see LICENSE file.
|
11 |
*
|
12 |
* @category CategoryName
|
13 |
* @package PackageName
|
14 |
* @author Michael Teeuw
|
15 |
* @copyright 2018
|
16 |
* @license https://opensource.org/licenses/MIT
|
17 |
* @link http://michaelteeuw.nl
|
18 |
*/
|
19 |
|
20 |
#include <Arduino.h>
|
21 |
#include <Wire.h>
|
22 |
|
23 |
#include "RTClib.h" // https://github.com/adafruit/RTClib
|
24 |
|
25 |
// Define the Pins we are using.
|
26 |
#define PIN_BUTTON_HOUR 7
|
27 |
#define PIN_BUTTON_MIN 8
|
28 |
#define PIN_METER_HOUR 3
|
29 |
#define PIN_METER_MIN 5
|
30 |
#define PIN_METER_SEC 6
|
31 |
|
32 |
// Define the minimum and maximum PWM values for all the meters.
|
33 |
// These values can be used to calibrate the meters.
|
34 |
#define MINIMUM_PWM_VALUE_HOUR 15
|
35 |
#define MAXIMUM_PWM_VALUE_HOUR 240
|
36 |
#define MINIMUM_PWM_VALUE_MIN 15
|
37 |
#define MAXIMUM_PWM_VALUE_MIN 254
|
38 |
#define MINIMUM_PWM_VALUE_SEC 15
|
39 |
#define MAXIMUM_PWM_VALUE_SEC 240
|
40 |
|
41 |
// Create the real time clock instance.
|
42 |
RTC_DS1307 rtc;
|
43 |
|
44 |
/**
|
45 |
* Slowly animate one meter to the maximum value and back.
|
46 |
* This animation is used in the start sequence. Not only does it look
|
47 |
* cool. It's also a way to test the meters.
|
48 |
*
|
49 |
* @param uint8_t pin The pin of the meter.
|
50 |
* @param uint8_t min The meter's minimum PWM value.
|
51 |
* @param uint8_t max The meter's maximum PWM value.
|
52 |
*/
|
53 |
void animateMeter(uint8_t pin, uint8_t min, uint8_t max) {
|
54 |
uint8_t v;
|
55 |
for (v = min; v <= max; v++) {
|
56 |
analogWrite(pin, v);
|
57 |
delay(2);
|
58 |
}
|
59 |
delay(100);
|
60 |
for (v = max; v >= min; v--) {
|
61 |
analogWrite(pin, v);
|
62 |
delay(2);
|
63 |
}
|
64 |
delay(100);
|
65 |
}
|
66 |
|
67 |
/**
|
68 |
* Run the startup sequence by animating all three meters sequentially.
|
69 |
*/
|
70 |
void startSequence() {
|
71 |
animateMeter(PIN_METER_HOUR, MINIMUM_PWM_VALUE_HOUR, MAXIMUM_PWM_VALUE_HOUR);
|
72 |
animateMeter(PIN_METER_MIN, MINIMUM_PWM_VALUE_MIN, MAXIMUM_PWM_VALUE_MIN);
|
73 |
animateMeter(PIN_METER_SEC, MINIMUM_PWM_VALUE_SEC, MAXIMUM_PWM_VALUE_SEC);
|
74 |
}
|
75 |
|
76 |
/**
|
77 |
* The method to update the time displayed on the the meter.
|
78 |
*/
|
79 |
void displayTime() {
|
80 |
// Retrieve the time from the real time clock.
|
81 |
DateTime now = rtc.now();
|
82 |
|
83 |
// Since the real time clock returns a 24 hour format,
|
84 |
// we need to convert the hour value to 12 hour format.
|
85 |
// For minutes and seconds we can just directly use the
|
86 |
// properties on the 'now' object. No need to store those
|
87 |
// in a temporary variable.
|
88 |
int8_t h = (now.hour() == 0 || now.hour() == 12) ? 12 : now.hour() % 12;
|
89 |
|
90 |
// Convert the time values to PWM values by using map ...
|
91 |
// and set that value as the PWM value using analogWrite.
|
92 |
analogWrite(PIN_METER_HOUR, map(h, 1, 12, MINIMUM_PWM_VALUE_HOUR, MAXIMUM_PWM_VALUE_HOUR));
|
93 |
analogWrite(PIN_METER_MIN, map(now.minute(), 0, 60, MINIMUM_PWM_VALUE_MIN, MAXIMUM_PWM_VALUE_MIN));
|
94 |
analogWrite(PIN_METER_SEC, map(now.second(), 0, 60, MINIMUM_PWM_VALUE_SEC, MAXIMUM_PWM_VALUE_SEC));
|
95 |
|
96 |
// For debuging purposes, write the time to the
|
97 |
// Serial port as well.
|
98 |
Serial.print(h);
|
99 |
Serial.print(":");
|
100 |
Serial.print(now.minute());
|
101 |
Serial.print(":");
|
102 |
Serial.println(now.second());
|
103 |
}
|
104 |
|
105 |
/**
|
106 |
* The setup method used by the Arduino.
|
107 |
*/
|
108 |
void setup () {
|
109 |
// Initiate the serial port for debugging purposes.
|
110 |
Serial.begin(9600);
|
111 |
|
112 |
// Configure all the pin modes for all connected pins.
|
113 |
pinMode(PIN_BUTTON_HOUR, INPUT_PULLUP);
|
114 |
pinMode(PIN_BUTTON_MIN, INPUT_PULLUP);
|
115 |
pinMode(PIN_METER_HOUR, OUTPUT);
|
116 |
pinMode(PIN_METER_MIN, OUTPUT);
|
117 |
pinMode(PIN_METER_SEC, OUTPUT);
|
118 |
|
119 |
// Start the real time clock.
|
120 |
rtc.begin();
|
121 |
|
122 |
// If the real time clock is not running,
|
123 |
// for example after the battery was replaced,
|
124 |
// start it by setting the time.
|
125 |
if (!rtc.isrunning()) {
|
126 |
rtc.adjust(DateTime(2018, 6, 17, 0, 0, 0));
|
127 |
}
|
128 |
|
129 |
// Initiate the power-on self test by running the start sequence.
|
130 |
startSequence();
|
131 |
}
|
132 |
|
133 |
/**
|
134 |
* The main runloop used by the Arduino.
|
135 |
*/
|
136 |
void loop () {
|
137 |
// Update the meters by calling the `displayTime()` method.
|
138 |
displayTime();
|
139 |
|
140 |
// Check if we pressed the button to adjust the hour ...
|
141 |
// Repeat the following loop for as long as we hold this button.
|
142 |
while (!digitalRead(PIN_BUTTON_HOUR)) {
|
143 |
// Grab the current time.
|
144 |
DateTime now = rtc.now();
|
145 |
|
146 |
// Set the RTC time by using the current time and increaing the hour by one.
|
147 |
// By using the % 24, it will automaticly overflow to 0 when it reaches 24.
|
148 |
// The seconds are simply reset.
|
149 |
rtc.adjust(DateTime(now.year(), now.month(), now.day(), (now.hour() + 1) % 24, now.minute(), 0));
|
150 |
|
151 |
// Update the display to reflect the changes.
|
152 |
displayTime();
|
153 |
|
154 |
// Wait half a second before we check again to see
|
155 |
// if the button is still pressed.
|
156 |
delay(500);
|
157 |
}
|
158 |
|
159 |
// Check if we pressed the button to adjust the minutes ...
|
160 |
// Repeat the following loop for as long as we hold this button.
|
161 |
while (!digitalRead(PIN_BUTTON_MIN)) {
|
162 |
// Grab the current time.
|
163 |
DateTime now = rtc.now();
|
164 |
|
165 |
// Set the RTC time by using the current time and increaing the minutes by one.
|
166 |
// By using the % 60, it will automaticly overflow to 0 when it reaches 60.
|
167 |
// The seconds are simply reset.
|
168 |
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), (now.minute() + 1) % 60, 0));
|
169 |
|
170 |
// Update the display to reflect the changes.
|
171 |
displayTime();
|
172 |
|
173 |
// Wait a quarter of a second before we check again to see
|
174 |
// if the button is still pressed.
|
175 |
delay(250);
|
176 |
}
|
177 |
|
178 |
// Wait for 1 second before we start all over again.
|
179 |
delay(1000);
|
180 |
}
|