forked from ajithvasudevan/ArduinoCLock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock2.ino
203 lines (175 loc) · 5.9 KB
/
Clock2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Clock with light leak bugs fixed, and with time setting feature
*/
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int colonPin = 9;
int modePin = 14;
int setPin = 15;
int MODE_BUTTON_PREVSTATE, MODE_BUTTON_STATE;
int SET_BUTTON_PREVSTATE, SET_BUTTON_STATE;
int MODE = 0;
int hr, mt;
int SET_DELAY = 50;
int setDelayCtr = 0;
int timeChanged = 0;
int baseDelay = 1000;
int bias = 300;
/*
* Arduino 4x7 LED Display
* Displays numbers ranging from 0 through 1023
* Test Code displays the value of a sensor connected to the analog input A0
* Inspired by a code found in the Arduino Cookbook
* Tested at TechNode Protolabz/July 2014
*/
// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// DP,G,F,E,D,C,B,A
const int segmentPins[] = { 5,5,4,6,7,8,2,3 };
const int nbrDigits= 4; // the number of digits in the LED display
//dig 0 1 2 3
const int digitPins[nbrDigits] = { 13,12,11,10 };
void setup()
{
Serial.begin(115200);
delay(3000); // wait for console opening
if (! rtc.begin()) {
// Serial.println("Couldn't find RTC");
while (1);
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//rtc.adjust(DateTime(2017, 12, 27, 21, 57, 10));
if (rtc.lostPower()) {
//Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
//rtc.adjust(DateTime(2017, 12, 27, 22, 6, 10));
}
for(int i=0; i < 8; i++) {
pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
}
for(int i=0; i < nbrDigits; i++) {
pinMode(digitPins[i], OUTPUT);
}
pinMode(colonPin, OUTPUT);
pinMode(modePin, INPUT);
pinMode(setPin, INPUT);
}
void loop()
{
DateTime now = rtc.now();
int rem = now.second() % 2;
if(MODE == 0) {
if(rem == 0) digitalWrite(colonPin, HIGH);
else digitalWrite(colonPin, LOW);
} else digitalWrite(colonPin, LOW);
MODE_BUTTON_STATE = digitalRead(modePin);
if(MODE_BUTTON_STATE == HIGH && MODE_BUTTON_PREVSTATE == LOW) {
MODE++;
if(MODE > 2) {
MODE = 0;
if(timeChanged == 1) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hr, mt, 0));
timeChanged = 0;
}
}
}
MODE_BUTTON_PREVSTATE = MODE_BUTTON_STATE;
SET_BUTTON_STATE = digitalRead(setPin);
/*Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(now.dayOfTheWeek());
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
*/
if(MODE == 0) {
hr = now.hour();
mt = now.minute();
} else if(MODE == 1) {
if(SET_BUTTON_STATE == HIGH && setDelayCtr == 0) { mt++; if(mt > 59) mt = 0; timeChanged = 1; }
} else if(MODE == 2) {
if(SET_BUTTON_STATE == HIGH && setDelayCtr == 0) { hr++; if(hr > 23) hr = 0; timeChanged = 1; }
}
/* if(SET_BUTTON_STATE == HIGH && setDelayCtr == 0) {
baseDelay = 100 + baseDelay;
if (baseDelay > 13000) baseDelay = 100;
Serial.print("BaseDelay = "); Serial.println(baseDelay);
}
*/
if(hr > 12) hr = hr - 12;
if(hr == 0) hr = 12;
int value = hr * 100 + mt;
showNumber(value);
setDelayCtr++; if(setDelayCtr == SET_DELAY) setDelayCtr = 0;
}
void showNumber( int number)
{
// if(number == 0) {
// showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
// } else {
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = 0; digit <nbrDigits; digit++) {
if(number > 0) {
showDigit( number % 10, digit) ;
number = number / 10;
}
}
// }
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int number, int digit)
{
digitalWrite( digitPins[digit], LOW );
for(int segment = 1; segment < 8; segment++) {
boolean isBitSet = bitRead(numeral[number], segment);
// isBitSet will be true if given bit is 1
// isBitSet = ! isBitSet; // Code Option*
// uncomment the above Code Option line for common anode display
digitalWrite( segmentPins[segment], isBitSet);
}
if(MODE == 1 && ((digit == 0) || (digit == 1))) {
if(setDelayCtr < (SET_DELAY / 2)) digitalWrite( digitPins[digit], HIGH );
else digitalWrite( digitPins[digit], LOW );
}
else if(MODE == 2 && ((digit == 2) || (digit == 3))) {
if(setDelayCtr < (SET_DELAY / 2)) digitalWrite( digitPins[digit], HIGH );
else digitalWrite( digitPins[digit], LOW );
} else digitalWrite( digitPins[digit], HIGH );
if(number == 1) delayMicroseconds(baseDelay);
else if(number == 7) delayMicroseconds(baseDelay + bias);
else if(number == 4) delayMicroseconds(baseDelay + 2*bias);
else if(number == 3) delayMicroseconds(baseDelay + 3*bias);
else if(number == 2) delayMicroseconds(baseDelay + 3*bias);
else if(number == 5) delayMicroseconds(baseDelay + 3*bias);
else if(number == 6) delayMicroseconds(baseDelay + 4*bias);
else if(number == 9) delayMicroseconds(baseDelay + 4*bias);
else if(number == 0) delayMicroseconds(baseDelay + 4*bias);
else delayMicroseconds(baseDelay + 5*bias);
digitalWrite( digitPins[digit], LOW );
}