1
2
3
4#include "Wire.h"
5#define
6 DS3231_I2C_ADDRESS 0x68
7
8
9
10
11 pin A4 => SDA
12
13
14int mode = 0;
15 0 = run, 1 = set, 2 = show minimum, 3 = show maximum
16
17int hoursPin = 5;
18 pin for hours meter
19int minutesPin = 6;
20int secondsPin
21 = 9;
22
23float hoursValue = 0;
24 current hours
25float minutesValue = 0;
26float
27 secondsValue = 0;
28
29float hoursMinimum
30 = 14;
31float minutesMinimum = 14;
32
33float secondsMinimum = 22;
34 value to set zero position of meter
35
36float hoursMaximum = 230;
37 value to set maximum position of meter
38float minutesMaximum = 230;
39 to set maximum position of meter
40float secondsMaximum = 236;
41 set maximum position of meter
42
43void setup() {
44 pinMode(hoursPin, OUTPUT);
45
46 pinMode(minutesPin, OUTPUT);
47 pinMode(secondsPin, OUTPUT);
48
49 Wire.begin();
50
51 Serial.begin(9600);
52
53 if(mode == 1){
54
55 on the numbers, they will be read as octal numbers
56
57 day d m y
58 setDS3231time(30, 46, 16, 7, 12, 9, 20);
59 }
60}
61
62void
63 loop() {
64 byte second, minute, hour, weekday, day, month, year;
65
66 switch(mode){
67
68 case 0:
69 case 1:
70 readDS3231time(&second, &minute, &hour, &weekday,
71 &day, &month, &year);
72 secondsValue = second;
73 minutesValue = minute;
74
75 hoursValue = hour;
76
77 if(hoursValue > 11){
78 hoursValue
79 -= 12;
80 }
81
82 analogWrite(secondsPin, secondsMinimum + (((secondsMaximum
83 - secondsMinimum) / 60) * secondsValue));
84 analogWrite(minutesPin, minutesMinimum
85 + (((minutesMaximum - minutesMinimum) / 60) * minutesValue));
86 analogWrite(hoursPin,
87 hoursMinimum + (((hoursMaximum - hoursMinimum) / 12) * hoursValue));
88 break;
89
90 case 2:
91
92 for the meters when you first set up your clock
93 analogWrite(secondsPin,
94 secondsMinimum);
95 analogWrite(minutesPin, minutesMinimum);
96 analogWrite(hoursPin,
97 hoursMinimum);
98 break;
99 case 3:
100 analogWrite(secondsPin, secondsMaximum);
101
102 analogWrite(minutesPin, minutesMaximum);
103 analogWrite(hoursPin, hoursMaximum);
104
105 break;
106 }
107
108 delay(100);
109}
110
111byte decToBcd(byte val){
112
113 return((val / 10 * 16) + (val % 10));
114}
115
116byte bcdToDec(byte val){
117
118 return ((val / 16 * 10) + (val % 16));
119}
120
121void setDS3231time(byte second,
122 byte minute, byte hour, byte weekday, byte day, byte month, byte year){
123 Wire.beginTransmission(DS3231_I2C_ADDRESS);
124
125 Wire.write(0);
126 Wire.write(decToBcd(second));
127 Wire.write(decToBcd(minute));
128
129 Wire.write(decToBcd(hour));
130 Wire.write(decToBcd(weekday));
131 Wire.write(decToBcd(day));
132
133 Wire.write(decToBcd(month));
134 Wire.write(decToBcd(year));
135 Wire.endTransmission();
136}
137
138void
139 readDS3231time(byte *second, byte *minute, byte *hour, byte *weekday, byte *day,
140 byte *month, byte *year){
141 Wire.beginTransmission(DS3231_I2C_ADDRESS);
142 Wire.write(0);
143
144 Wire.endTransmission();
145 Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
146 *second
147 = bcdToDec(Wire.read() & 0x7f);
148 *minute = bcdToDec(Wire.read());
149 *hour
150 = bcdToDec(Wire.read() & 0x3f);
151 *weekday = bcdToDec(Wire.read());
152 *day
153 = bcdToDec(Wire.read());
154 *month = bcdToDec(Wire.read());
155 *year = bcdToDec(Wire.read());
156}
157
mcraft34
a year ago
How to resize the dial overlays? I have tried paint with no success.