-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_projet_s1.ino
383 lines (326 loc) · 9.96 KB
/
script_projet_s1.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <Arduino.h>
#include <RTClib.h>
#include <MD_Parola.h>
#include <ss_oled.h>
#include <EEPROM.h>
#define VALIDE_MENU A1
#define PM_AM_SWITCH 13
#define LED_SECONDE 12
#define REVEIL_BUZZER 11
#define TRIGPIN 10
#define ECHOPIN 9
#define PINA_ENCODEUR 8
#define PINB_ENCODEUR 7
#define DATA_PIN 6
#define CS_PIN 5
#define CLK_PIN 4
#define BOUTON_MENU 3
#define REVEIL_BUTTON 2
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
unsigned long previousMinuteEdit = 0;
volatile byte reveil = 1;
byte alarmePasse = 0;
unsigned long previousReveilEdit = 0;
unsigned long previousReveilStart = 0;
unsigned long alarmPreviousMillis = 0;
byte alarmState = 0;
int etatPrecedentLigneCLK;
volatile byte flag = 0;
SSOLED ssoled;
MD_Parola myDisplay = MD_Parola(MD_MAX72XX::FC16_HW, DATA_PIN, CLK_PIN, CS_PIN, 4);
RTC_DS1307 RTC;
DateTime reveilTime;
void clsOled() {
oledFill(&ssoled, 0, 1);
}
void flagMenu() {
flag = 1;
}
int detectionMain() {
//digitalWrite(TRIGPIN, HIGH);
PORTD = PORTD | (1 << TRIGPIN);
delayMicroseconds(10);
//digitalWrite(TRIGPIN, LOW);
PORTD = PORTD & !(1 << TRIGPIN);
// Mesure la durée de l'impulsion reçue sur la broche Echo
int Duree = pulseIn(ECHOPIN, HIGH);
//int Duree = 0;
// Calcule la distance en cm
int a = Duree * 0.034 / 2;
if (a >= 10 || a < 1) {
return 0;
} else {
return 1;
}
}
void updateLedMatrix(DateTime time) {
char heureAffichable[6];
short heures = time.hour();
short heureFormat;
char heuresStr[3];
short minutes = time.minute();
char minutesStr[3];
if (digitalRead(PM_AM_SWITCH) == HIGH && heures > 12) {
heureFormat = heures - 12;
} else {
heureFormat = heures;
}
itoa(heureFormat, heuresStr, 10);
if (heureFormat < 10) {
heureAffichable[0] = '0';
heureAffichable[1] = heuresStr[0];
} else {
heureAffichable[0] = heuresStr[0];
heureAffichable[1] = heuresStr[1];
}
heureAffichable[2] = ':';
itoa(minutes, minutesStr, 10);
if (minutes < 10) {
heureAffichable[3] = '0';
heureAffichable[4] = minutesStr[0];
} else {
heureAffichable[3] = minutesStr[0];
heureAffichable[4] = minutesStr[1];
}
heureAffichable[5] = '\0';
myDisplay.print(heureAffichable);
// Serial.println(heureAffichable);
}
void updateOled(DateTime time) {
clsOled();
if (digitalRead(PM_AM_SWITCH) == HIGH) {
if (time.hour() >= 12) {
oledWriteString(&ssoled, 0, 0, 0, "PM", FONT_SMALL, 0, 1);
} else {
oledWriteString(&ssoled, 0, 0, 0, "AM", FONT_SMALL, 0, 1);
}
}
if (reveil) {
oledWriteString(&ssoled, 0, (int) OLED_WIDTH/2, 0, "ALARME ON", FONT_SMALL, 0, 1);
} else {
oledWriteString(&ssoled, 0, (int) OLED_WIDTH/2, 0, "ALARME OFF", FONT_SMALL, 0, 1);
}
char heureStr[3];
itoa(RTC.now().hour(), heureStr, 10);
char minuteStr[3];
itoa(RTC.now().minute(), minuteStr, 10);
oledWriteString(&ssoled, 0, 0, 2, heureStr, FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, ":", FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, minuteStr, FONT_SMALL, 0, 1);
}
void alarm(DateTime now) {
if (reveil && !alarmePasse && !alarmState && now.hour() == reveilTime.hour() && now.minute() == reveilTime.minute()) {
previousReveilStart = millis();
//digitalWrite(REVEIL_BUZZER, HIGH);
PORTD = PORTD | (1 << REVEIL_BUZZER);
alarmState = 1;
}
if (alarmState && millis() - alarmPreviousMillis >= 100) {
digitalWrite(REVEIL_BUZZER, !digitalRead(REVEIL_BUZZER));
alarmPreviousMillis = millis();
}
// Éteindre l'alarme après 20 secondes
byte detect = detectionMain();
if ((alarmState && millis() - previousReveilStart >= 20000) || (detect && alarmState)) {
if (detect) {
EEPROM.put(0, millis() - previousReveilStart);
}
// digitalWrite(REVEIL_BUZZER, LOW);
PORTD = PORTD & !(1 << REVEIL_BUZZER);
alarmState = 0;
alarmePasse = 1;
}
if (millis() - previousReveilStart >= 120000) {
alarmePasse = 0;
}
}
void toggleReveil() {
reveil = !reveil;
// Serial.println((reveil ? F("Réveil allumé") : F("Réveil éteint")));
}
void menu() {
if (flag == 1) {
// Serial.println(F("Menu !"));
byte fini = 0;
short etape = 0;
short temps = 24;
short heures = 0;
short compteur = 0;
byte boutonAppuie = 0;
clsOled();
if (etape < 2) {
oledWriteString(&ssoled, 0, 0, 0, "Reveil", FONT_SMALL, 0, 1);
} else {
oledWriteString(&ssoled, 0, 0, 0, "Heure", FONT_SMALL, 0, 1);
}
if (etape == 0 || etape == 2) {
char minuteStr[3];
itoa(compteur, minuteStr, 10);
oledWriteString(&ssoled, 0, 0, 2, minuteStr, FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, ":00", FONT_SMALL, 0, 1);
} else {
char heureStr[3];
itoa(heures, heureStr, 10);
char minuteStr[3];
itoa(compteur, minuteStr, 10);
oledWriteString(&ssoled, 0, 0, 2, heureStr, FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, ":", FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, minuteStr, FONT_SMALL, 0, 1);
}
while(!fini) {
short etatActuelDeLaLigneCLK = digitalRead(PINB_ENCODEUR);
if (!digitalRead(VALIDE_MENU) && !boutonAppuie) {
boutonAppuie = 1;
// Serial.print(temps);
// Serial.print(F(" : "));
// Serial.println(compteur);
if (etape == 0 || etape == 2) {
heures = compteur;
}
if (etape == 1 || etape == 3) {
char heureFinale[9];
char heureStr[2];
itoa(heures, heureStr, 10);
if (heures < 10) {
heureFinale[0] = '0';
heureFinale[1] = heureStr[0];
} else {
heureFinale[0] = heureStr[0];
heureFinale[1] = heureStr[1];
}
heureFinale[2] = ':';
char minuteStr[2];
itoa(compteur, minuteStr, 10);
if (compteur < 10) {
heureFinale[3] = '0';
heureFinale[4] = minuteStr[0];
} else {
heureFinale[3] = minuteStr[0];
heureFinale[4] = minuteStr[1];
}
heureFinale[5] = ':';
heureFinale[6] = '0';
heureFinale[7] = '0';
heureFinale[8] = '\0';
if (etape == 1) {
reveilTime = DateTime(__DATE__, heureFinale);
} else {
RTC.adjust(DateTime(__DATE__, heureFinale));
}
}
if (etape + 1 == 4) {
fini = 1;
// Serial.println(F("Bye :)"));
flag = 0;
break;
} else {
etape++;
if (temps == 24) {
temps = 60;
} else {
temps = 24;
}
compteur = 0;
}
}
if (!digitalRead(VALIDE_MENU) == LOW) {
boutonAppuie = 0;
}
if(etatActuelDeLaLigneCLK != etatPrecedentLigneCLK) {
etatPrecedentLigneCLK = etatActuelDeLaLigneCLK;
if(etatActuelDeLaLigneCLK == LOW) {
if(etatActuelDeLaLigneCLK != digitalRead(PINA_ENCODEUR)) {
if (compteur + 1 == temps) {
compteur = 0;
} else {
compteur++;
}
} else {
if (compteur == 0) {
compteur = temps - 1;
} else {
compteur--;
}
}
clsOled();
if (etape < 2) {
oledWriteString(&ssoled, 0, 0, 0, "Reveil", FONT_SMALL, 0, 1);
} else {
oledWriteString(&ssoled, 0, 0, 0, "Heure", FONT_SMALL, 0, 1);
}
if (etape == 0 || etape == 2) {
char minuteStr[3];
itoa(compteur, minuteStr, 10);
oledWriteString(&ssoled, 0, 0, 2, minuteStr, FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, ":00", FONT_SMALL, 0, 1);
} else {
char heureStr[3];
itoa(heures, heureStr, 10);
char minuteStr[3];
itoa(compteur, minuteStr, 10);
oledWriteString(&ssoled, 0, 0, 2, heureStr, FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, ":", FONT_SMALL, 0, 1);
oledWriteString(&ssoled, 0, -1, -1, minuteStr, FONT_SMALL, 0, 1);
}
}
}
delay(1);
}
}
}
void setup () {
// Serial.begin(9600);
Wire.begin();
RTC.begin();
// oledInit(SSOLED*, type, oled_addr, rotate180, invert, bWire, SDA_PIN, SCL_PIN, RESET_PIN, speed)
short rc = oledInit(&ssoled, OLED_128x64, 0x3C, 0, 0, 1, A4, A5, -1, 400000L); // use standard I2C bus at 400Khz
if (rc == -1) {
}
if (!RTC.isrunning()) {
// Serial.println(F("RTC is NOT running!"));
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
previousMinuteEdit = millis();
pinMode(LED_SECONDE, OUTPUT);
pinMode(PM_AM_SWITCH, INPUT);
pinMode(REVEIL_BUTTON, INPUT);
pinMode(REVEIL_BUZZER, OUTPUT);
pinMode(PINA_ENCODEUR, INPUT);
pinMode(PINB_ENCODEUR, INPUT);
pinMode(BOUTON_MENU, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(VALIDE_MENU, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(REVEIL_BUTTON), toggleReveil, FALLING);
attachInterrupt(digitalPinToInterrupt(BOUTON_MENU), flagMenu, RISING);
etatPrecedentLigneCLK = digitalRead(PINB_ENCODEUR);
cli();
TCCR1A = 0;
TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10); // horloge interne divisée par 1024
TIMSK1 = _BV(OCIE1A); // it sur comparateur
TCNT1 = 0;
OCR1A = (F_CPU / 1000 / 1024) * 500; // 500ms est une demi-période des secondes
sei();
myDisplay.begin();
myDisplay.displayClear();
myDisplay.setTextAlignment(PA_CENTER);
updateLedMatrix(RTC.now());
updateOled(RTC.now());
delay(200);
}
ISR(TIMER1_COMPA_vect) {
// fait clignoter la led à 1/2 periode de 1s
digitalWrite(LED_SECONDE, !digitalRead(LED_SECONDE));
}
void loop () {
DateTime now = RTC.now();
updateOled(now);
if (millis() - previousMinuteEdit >= 3000) {
updateLedMatrix(now);
previousMinuteEdit = millis();
}
menu();
alarm(now);
}