-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlarmClock.ino
More file actions
186 lines (147 loc) · 4.45 KB
/
AlarmClock.ino
File metadata and controls
186 lines (147 loc) · 4.45 KB
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
/*
7 segment diy radio clock with buttons and buzzer
Nico Heid 2014 (nicoheid.com) | CC by-sa-nc
7 segment code based on:
Example 39.1 - NXP SAA1064 I2C LED Driver IC Demo I
Demonstrating display of digits
http://tronixstuff.com/tutorials > chapter 39
John Boxall July 2011 | CC by-sa-nc
*/
#include "Wire.h" // enable I3C bus
#include "DCF77.h"
#include "Time.h"
#include "Utils.h"
#include <LiquidCrystal.h>
#define DCF_PIN 2 // Connection pin to DCF 77 device
#define DCF_INTERRUPT 0 // Interrupt number associated with pin
// display state, display can be disabled
boolean displayOn = true;
// buzzer state
boolean buzzerOn = false;
boolean buzzerMode = false;
int buzzerHour = 0;
int buzzerMinute = 0;
byte saa1064 = 0x70 >> 1; // define the I2C bus address for our SAA1064 (pin 1 to GND) ****
int digits[16]={ 63, 6, 91, 79, 102, 109, 125,7, 127, 111, 119, 124, 57, 94, 121, 113};
// these are the byte representations of pins required to display each digit 0~9 then A~F
DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT, false);
boolean synced = false;
int buttonPin = 3;
int lc = 0;
void setup(){
Wire.begin(); // start up I2C bus
delay(500);
initDisplay();
DCF.Start();
Serial.begin(9600);
Serial.println("Waiting for DCF77 time ... ");
Serial.println("It will take at least 2 minutes before a first time update.");
// set buttons to input and activate pull up
// reading: 1 = button up, 0 button down
for(int i = 4; i<8; i++){
pinMode(i, INPUT);
digitalWrite(i,HIGH);
}
// set up buzzer
pinMode(9, OUTPUT);
}
// turns on dynamic mode and adjusts segment current to 12mA
void initDisplay(){
Wire.beginTransmission(saa1064);
Wire.write(B00000000); // this is the instruction byte. Zero means the next byte is the control byte
Wire.write(B00010111); // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, 12mA segment current
Wire.endTransmission();
}
void displayOnOff(boolean on){
Wire.beginTransmission(saa1064);
Wire.write(B00000000); // this is the instruction byte. Zero means the next byte is the control byte
if(on){
Wire.write(B00010111); // control byte (dynamic mode on, digits 1+3 on, digits 2+4 on, 12mA segment current
}else{
Wire.write(B00000111);
}
Wire.endTransmission();
}
void displayTime(int hour, int minute){
Wire.beginTransmission(saa1064);
Wire.write(1); // instruction byte - first digit to control is 1 (right hand side)
Wire.write(digits[hour/10]);
if(buzzerOn){
Wire.write(digits[hour%10] + 128);
}else{
Wire.write(digits[hour%10]);
}
Wire.write(digits[minute/10]);
Wire.write(digits[minute%10]);
Wire.endTransmission();
}
void buzzer(){
analogWrite(9, 20); // Almost any value can be used except 0 and 255 experiment to get the best tone
delay(100);
analogWrite(9, 0); // 0 turns it off
}
int readButtons(){
int result = 0;
if(digitalRead(4)==0)
result+=8;
if(digitalRead(5)==0)
result+=4;
if(digitalRead(6)==0)
result+=2;
if(digitalRead(7)==0)
result+=1;
return result;
}
void loop(){
if(lc==99){
time_t DCFtime = DCF.getTime(); // Check if new DCF77 time is available
if (DCFtime!=0) {
Serial.println("Time is updated");
setTime(DCFtime);
synced=true;
}
}
if(!buzzerMode){
if(!synced){
displayTime(99,99);
}else{
displayTime(hour(), minute());
}
}
int buttons = readButtons();
if((buttons & 8) == 8){
if(buzzerMode){
buzzerMode = false;
displayTime(hour(), minute());
}else{
displayOn = !displayOn;
displayOnOff(displayOn);
}
}
if((buttons & 1) == 1){
buzzerMode = true;
displayOn = true;
displayOnOff(displayOn);
displayTime(buzzerHour, buzzerMinute);
}
if(buzzerMode){
if((buttons & 2) == 2){
buzzerMinute = (buzzerMinute+1) %60;
displayTime(buzzerHour, buzzerMinute);
}
if((buttons & 4) == 4){
buzzerHour = (buzzerHour+1) %24;
displayTime(buzzerHour, buzzerMinute);
}
if((buttons & 1) == 1){
buzzerOn = !buzzerOn;
displayTime(buzzerHour, buzzerMinute);
}
}
lc=(lc+1)%100;
// sound alarm?
if(buzzerOn && hour() == buzzerHour && minute() == buzzerMinute){
buzzer();
}
delay(100);
}