-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path192L_Project_Clock.ino
More file actions
246 lines (190 loc) · 4.71 KB
/
Copy path192L_Project_Clock.ino
File metadata and controls
246 lines (190 loc) · 4.71 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <Arduino.h>
//Function declarations
void iterateTime();
void setUserAlarm();
void displayTime(int hour, int minute, int sec);
void changeRoom();
void buttonPressed();
//Volatile vars for interrupts
volatile int state = LOW;
volatile int changeRoomState = LOW;
volatile int completedCycles = LOW;
int buttonValue = analogRead(2);
//Ints used in program
volatile int secs = 0;
int mins = 30;
int hours = 12;
int alarmMatrix[4][2];
int lcdButton = 0;
int roomNumber = 0;
bool setAlarm = 0;
int lightAmount = 0;
const int room0 = 8;
const int room1 = 9;
const int room2 = 10;
const int room3 = 11;
bool roomState[4];
//Setup
void setup() {
Serial.begin(9600);
cli(); // disable interrupts
TCCR1A = 0; // reset Timer1 Control registers
TCCR1B = 0; //Set WGM_2:0 = 000
TCNT1 = 0;
TIMSK1 = 0x6; //Enable OCR Interrupt bits
OCR1A = 8000; // excutes interrupt when events is reached. Frequency of the arduino, adjusted by the prescalar (16 000 000 / 1024) 15625
// set prescaler for Timer 1 to 1024
TCCR1B |= _BV(CS02);
TCCR1B &= ~_BV(CS01);
TCCR1B |= _BV(CS00);
sei(); //enable interrupts
attachInterrupt(0, buttonPressedInterruptHandler, CHANGE); // trigger interrupt when button is pressed (set alarm, hour, min, cancel)
attachInterrupt(1, changeRoomInterruptHandler, RISING); // trigger interrupt when button is pressed (change room)
pinMode(room0, OUTPUT);
pinMode(room1, OUTPUT);
pinMode(room2, OUTPUT);
pinMode(room3, OUTPUT);
}
//Main
void loop() {
iterateTime();
//On Button Click
if (state) {
buttonPressed();
}
//On change room button click
if (changeRoomState) {
changeRoom();
}
for ( int i = 0; i <= 3; i++) {
if (checkAlarm(i)) {
roomState[i] = true;
}
}
lightAmount = analogRead(3);
if (lightAmount > 500) {
for ( int i = 0; i <= 3; i++) {
roomState[i] = true;
}
}
for (int i = 0; i < 4; i++) {
if (roomState[i]){
digitalWrite(i + 8, HIGH);
} else {
digitalWrite(i+8, LOW);
}
}
}
bool checkAlarm(int roomNumber) {
if (hours == alarmMatrix[roomNumber][0] && mins == alarmMatrix[roomNumber][1]) {
return true;
} else {
return false;
}
}
void iterateTime() {
if (completedCycles) {
if (secs >= 60) {
mins++;
}
secs = secs % 60;
if (mins == 60) {
mins = 0;
hours++;
}
displayTime(hours, mins, secs);
completedCycles = LOW;
}
}
void displayTime(int hours, int mins, int secs) {
if (hours < 10) {
Serial.print("0");
}
Serial.print(hours);
Serial.print(":");
if (mins < 10) {
Serial.print("0");
}
Serial.print(mins);
Serial.print(":");
if (secs < 10) {
Serial.print("0");
}
Serial.println(secs);
}
void setUserAlarm() {
displayTime(12, 0, 0);
}
void buttonPressed() {
buttonValue = analogRead(2); //Read button value (0 - 1023)
if (buttonValue <= 50) {
delay(500);
Serial.println("Set Alarm");
setAlarm = 1;
//Set Alarm
while (setAlarm) {
buttonValue = analogRead(2);
if (buttonValue > 50 && buttonValue <= 150) { //Increase hour
delay(200);
alarmMatrix[roomNumber][0]++;
if (alarmMatrix[roomNumber][0] > 23) {
alarmMatrix[roomNumber][0] = 0;
}
Serial.print(alarmMatrix[roomNumber][0]);
Serial.print(":");
Serial.println(alarmMatrix[roomNumber][1]);
}
if (buttonValue > 150 && buttonValue <= 350) { //Increase minute
delay(200);
alarmMatrix[roomNumber][1]++;;
if (alarmMatrix[roomNumber][1] > 59) {
alarmMatrix[roomNumber][1] = 0;
}
Serial.print(alarmMatrix[roomNumber][0]);
Serial.print(":");
Serial.println(alarmMatrix[roomNumber][1]);
}
if (buttonValue <= 50) {
delay(200);
Serial.println("Alarm has been set to: ");
Serial.print(alarmMatrix[roomNumber][0]);
Serial.print(" : ");
Serial.print(alarmMatrix[roomNumber][1]);
setAlarm = 0;
}
}
}
else if (buttonValue > 350 && buttonValue <= 450) {
delay(200);
//Turn off alarm
Serial.println("Turn off Alarm");
roomState[roomNumber] = false;
} else if (buttonValue > 1000) {
Serial.println("False Alarm");
}
state = LOW;
}
void changeRoom() {
delay(100);
//change the room
if (roomNumber == 4) {
roomNumber = 1;
} else {
roomNumber++;
}
Serial.print("Change to room number: ");
Serial.println(roomNumber);
changeRoomState = LOW;
}
//Interrupt Handlers
void changeRoomInterruptHandler() {
changeRoomState = !changeRoomState;
}
void buttonPressedInterruptHandler() {
state = !state;
}
ISR (TIMER1_COMPA_vect) {
completedCycles = !completedCycles;
secs++;
TCNT1 = 0;
}