1: // include the library code:
2: #include <SoftwareSerial.h>
3: #include <LiquidCrystal.h>
4: #include <dht11.h>
5:
6: //lcd(12, 11, 5, 4, 3, 2);
7:
8: #define motorFan 6
9:
10: #define butMode 8
11: #define butSx 9
12: #define butDx 7
13:
14: #define livelSensor A0 // sensore temperatura al pin analogico A0
15: #define DHT11PIN 10
16: #define ledPin 13
17:
18: void ChangeMenu();
19: void ChangeClock();
20: void DisplayMenu();
21: void MenuDefault();
22: void MenuMedia();
23: void ValoriAggiornati();
24: void MenuValori();
25: void MenuSetClock();
26: void MenuSendValues();
27: void PrintClock(int col);
28: void SaveValue();
29: void SaveDay();
30: void SendValues();
31: void Clock();
32: void SalvaValori();
33:
34: //Variabili globali
35: int menu = 0;
36: boolean mainMenu;
37:
38: int sensorValue = 0;
39: int reading = 0;
40: int sensorPin = A0;
41: byte sensorValues[144];
42: byte sensorValues_t[144];
43:
44: int sensorLastValue;
45: int sensorLastValue_t;
46:
47: int levelSensorValue = 0;
48: boolean ledblink = false;
49:
50: boolean buttonStateSx = 0; // current state of the button
51: boolean lastButtonStateSx = 0; // previous state of the button
52: boolean buttonStateDx = 0; // current state of the button
53: boolean lastButtonStateDx = 0; // previous state of the button
54: boolean buttonStateMode = 0; // current state of the button
55: boolean lastButtonStateMode = 0; // previous state of the button
56:
57: byte countDay;
58: byte day1[144];
59: byte day2[144];
60: byte day1_t[144];
61: byte day2_t[144];
62:
63: byte settingClock;
64: byte settingSerial;
65: unsigned long previousTime = 0;
66: byte seconds;
67: byte minutes;
68: byte hours;
69: byte days;
70:
71:
72: int ledState = LOW; // ledState used to set the LED
73: unsigned long previousMillis = 0; // will store last time LED was updated
74: const long interval = 1000; // interval at which to blink (milliseconds)
75:
76:
77: // initialize the library with the numbers of the interface pins
78: LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
79:
80: dht11 DHT11;
81: SoftwareSerial NSS(0, 5);
82:
83: void setup() {
84:
85: pinMode(butSx, INPUT);
86: pinMode(butDx, INPUT);
87: pinMode(butMode, INPUT);
88: pinMode(livelSensor, INPUT);
89:
90: digitalWrite (motorFan, OUTPUT);
91: digitalWrite (ledPin, OUTPUT);
92:
93: mainMenu = true;
94: settingClock = 0;
95:
96: seconds = 0;
97: minutes = 0;
98: hours = 0;
99: days = 0;
100:
101: // set up the LCD's number of columns and rows:
102: lcd.begin(16, 2);
103:
104: lcd.setCursor(0, 0);
105: lcd.print("loading ...");
106:
107: Serial.begin(9600);
108:
109: NSS.begin(9600);
110:
111: delay(1000); // Pausa di stabilizzazione sensore
112:
113: sensorLastValue = 0;
114: SaveValue();
115:
116: delay(1000); // Pausa di stabilizzazione sensore
117: lcd.clear();
118: }
119:
120: void loop() {
121:
122: // Menu normale
123: if (mainMenu == true) {
124: ChangeMenu();
125: } else {
126: switch (menu) {
127: case 1:
128: ValoriAggiornati();
129: break;
130: case 2:
131: SalvaValori();
132: break;
133: case 3:
134: ChangeClock();
135: break;
136: case 4:
137: SendValues();
138: break;
139: }
140: }
141:
142: DisplayMenu(); // Mostra il display
143:
144: // Aggiorna l'orologio
145: if (menu != 3) {
146: Clock();
147: }
148:
149: // Ventolina
150: if (sensorLastValue > 65) {
151: digitalWrite(motorFan, 1);
152: } else {
153: digitalWrite(motorFan, 0);
154: }
155:
156: //Led
157: if (levelSensorValue > 250) {
158: if (levelSensorValue > 400) {
159: digitalWrite(ledPin, HIGH);
160: } else {
161: if (ledblink == true) {
162: digitalWrite(ledPin, HIGH);
163: } else {
164: digitalWrite(ledPin, LOW);
165: }
166: }
167: } else {
168: digitalWrite(ledPin, LOW);
169: }
170:
171: }
172:
173: //Funzioni
174: void ChangeMenu() {
175: boolean bntPremuto = false;
176: boolean bntMode = false;
177: buttonStateSx = digitalRead(butSx);
178: buttonStateDx = digitalRead(butDx);
179: buttonStateMode = digitalRead(butMode);
180:
181: if (buttonStateSx != lastButtonStateSx) {
182: if (digitalRead(butSx) == 1) {
183: bntPremuto = true;
184: menu--;
185: }
186: // Delay a little bit to avoid bouncing
187: delay(50);
188: }
189:
190: if (buttonStateDx != lastButtonStateDx) {
191: if (digitalRead(butDx) == 1) {
192: bntPremuto = true;
193: menu++;
194: }
195: // Delay a little bit to avoid bouncing
196: delay(50);
197: }
198:
199: if (buttonStateMode != lastButtonStateMode) {
200: if (digitalRead(butMode) == 1) {
201: bntPremuto = true;
202: bntMode = true;
203: }
204: // Delay a little bit to avoid bouncing
205: delay(50);
206: }
207:
208: lastButtonStateSx = buttonStateSx;
209: lastButtonStateDx = buttonStateDx;
210: lastButtonStateMode = buttonStateMode;
211:
212: // Se sono nel menu 3,4 (clock settings) flaggo il bit che mi fa cambiare menu'
213: if (bntPremuto == true) {
214: lcd.clear();
215: if (menu < 0) {
216: menu = 4;
217: } else if (menu > 4) {
218: menu = 0;
219: }
220: if ((bntMode == true) && (menu == 1)) {
221: mainMenu = false;
222: }
223: if ((bntMode == true) && (menu == 2)) {
224: mainMenu = false;
225: }
226: if ((bntMode == true) && (menu == 3)) {
227: settingClock = 1;
228: mainMenu = false;
229: }
230: if ((bntMode == true) && (menu == 4)) {
231: settingSerial = 1;
232: mainMenu = false;
233: }
234: }
235: }
236:
237: void ChangeClock() {
238: int _valore = 0;
239: boolean bntPremuto = false;
240: boolean bntMode = false;
241: buttonStateSx = digitalRead(butSx);
242: buttonStateDx = digitalRead(butDx);
243: buttonStateMode = digitalRead(butMode);
244:
245: if (buttonStateSx != lastButtonStateSx) {
246: if (digitalRead(butSx) == 1) {
247: bntPremuto = true;
248: _valore = -1;
249: }
250: // Delay a little bit to avoid bouncing
251: delay(50);
252: }
253:
254: if (buttonStateDx != lastButtonStateDx) {
255: if (digitalRead(butDx) == 1) {
256: bntPremuto = true;
257: _valore = +1;
258: }
259: // Delay a little bit to avoid bouncing
260: delay(50);
261: }
262:
263: if (buttonStateMode != lastButtonStateMode) {
264: if (digitalRead(butMode) == 1) {
265: bntPremuto = true;
266: //_valore = 0;
267: }
268: // Delay a little bit to avoid bouncing
269: delay(50);
270: }
271:
272: lastButtonStateSx = buttonStateSx;
273: lastButtonStateDx = buttonStateDx;
274: lastButtonStateMode = buttonStateMode;
275:
276: //In base al valore di settingClock
277: // aumento/diminuisco le ore o i minuti...
278: if (bntPremuto == true) {
279: lcd.clear();
280: if (_valore == 0) {
281: settingClock++;
282: if (settingClock == 3) {
283: settingClock = 0;
284: mainMenu = true;
285: menu = 0;
286: }
287: } else {
288: if (settingClock == 1)
289: {
290: hours = hours + _valore;
291: if ((hours == 0) && (_valore == -1)) {
292: hours = 23;
293: }
294: if (hours >= 24) {
295: hours = 0;
296: }
297: } else {
298: minutes = minutes + _valore;
299: if ((minutes == 0) && (_valore == -1)) {
300: minutes = 59;
301: }
302: if (minutes >= 60) {
303: minutes = 0;
304: }
305: }
306: }
307: }
308:
309: }
310:
311: void DisplayMenu() {
312: switch (menu) {
313: case 0:
314: MenuDefault();
315: break;
316: case 1:
317: MenuMedia();
318: break;
319: case 2:
320: MenuValori();
321: break;
322: case 3:
323: MenuSetClock();
324: break;
325: case 4:
326: MenuSendValues();
327: break;
328: }
329: }
330:
331: void MenuDefault() {
332:
333: PrintClock(0);
334:
335: lcd.setCursor(0, 1);
336: lcd.print("U:");
337: lcd.setCursor(2, 1);
338: lcd.print(sensorLastValue);
339:
340: lcd.setCursor(5, 1);
341: lcd.print("T:");
342: lcd.setCursor(7, 1);
343: lcd.print(sensorLastValue_t);
344:
345: lcd.setCursor(10, 1);
346: lcd.print("L:");
347: lcd.setCursor(12, 1);
348: lcd.print(levelSensorValue);
349:
350: }
351:
352: void MenuMedia() {
353: lcd.setCursor(0, 0);
354: lcd.print("Agg. sensore:");
355: lcd.setCursor(0, 1);
356: lcd.print("> premi mode");
357: }
358:
359: void ValoriAggiornati() {
360: SaveValue();
361: lcd.clear();
362: lcd.setCursor(0, 0);
363: lcd.print("Dati aggiornati");
364: menu = 0;
365: mainMenu = true;
366: delay(1000);
367: lcd.clear();
368: }
369:
370: void MenuValori() {
371: lcd.setCursor(0, 0);
372: lcd.print("Save day data:");
373: lcd.setCursor(0, 1);
374: lcd.print("> premi mode");
375: }
376:
377: void SalvaValori() {
378: lcd.clear();
379: lcd.setCursor(0, 0);
380: lcd.print("Loading ...");
381: SaveDay();
382: lcd.clear();
383: lcd.setCursor(0, 0);
384: lcd.print("Valori salvati");
385: menu = 0;
386: mainMenu = true;
387: delay(1000);
388: lcd.clear();
389: }
390:
391: void MenuSetClock() {
392: if (settingClock == 0) {
393: lcd.setCursor(0, 0);
394: lcd.print("Imp. orologio:");
395: lcd.setCursor(0, 1);
396: lcd.print("> premi mode");
397: } else {
398: switch (settingClock) {
399: case 1:
400: lcd.setCursor(0, 0);
401: lcd.print("Cambia l'ora");
402: break;
403: case 2:
404: lcd.setCursor(0, 0);
405: lcd.print("Cambia minuti");
406: break;
407: }
408: PrintClock(1);
409: }
410:
411: }
412:
413: void MenuSendValues() {
414: if (settingSerial == 0) {
415: lcd.setCursor(0, 0);
416: lcd.print("Invia valori:");
417: lcd.setCursor(0, 1);
418: lcd.print("> premi mode");
419: } else {
420: }
421: }
422:
423: void PrintClock(int col) {
424:
425: lcd.setCursor(0, col);
426: lcd.print("Ore ");
427:
428: if (hours < 10) {
429: lcd.setCursor(4, col);
430: lcd.print("0");
431: lcd.setCursor(5, col);
432: lcd.print(hours);
433: } else {
434: lcd.setCursor(4, col);
435: lcd.print(hours);
436: }
437:
438: lcd.setCursor(6, col);
439: lcd.print(":");
440:
441: if (minutes < 10) {
442: lcd.setCursor(7, col);
443: lcd.print("0");
444: lcd.setCursor(8, col);
445: lcd.print(minutes);
446: } else {
447: lcd.setCursor(7, col);
448: lcd.print(minutes);
449: }
450:
451: lcd.setCursor(9, col);
452: lcd.print(":");
453:
454: if (seconds < 10) {
455: lcd.setCursor(10, col);
456: lcd.print("0");
457: lcd.setCursor(11, col);
458: lcd.print(seconds);
459: } else {
460: lcd.setCursor(10, col);
461: lcd.print(seconds);
462: }
463:
464: }
465:
466: void Clock() {
467: if (millis() >= (previousTime))
468: {
469: previousTime = previousTime + 1000;
470: seconds = seconds + 1;
471:
472: if (seconds == 60)
473: {
474: seconds = 0;
475: minutes = minutes + 1;
476: checkSensor();
477: if (minutes % 10 == 0) {
478: SaveValue();
479: }
480: }
481: if (minutes == 60)
482: {
483: minutes = 0;
484: hours = hours + 1;
485: }
486: if (hours == 23)
487: {
488: hours = 0;
489: SaveDay();
490: countDay++;
491: if (countDay == 2) {
492: countDay = 0;
493: }
494: }
495:
496: if (ledblink == true) {
497: ledblink = false;
498: } else {
499: ledblink = true;
500: }
501:
502: }
503: }
504:
505: void checkSensor() {
506: uint8_t chk = DHT11.read(DHT11PIN);
507: sensorLastValue = DHT11.humidity;
508: sensorLastValue_t = DHT11.temperature;
509: levelSensorValue = analogRead(livelSensor);
510: }
511:
512: void SaveValue() {
513: byte i = 0;
514: checkSensor();
515: i = (hours * 6) + (minutes / 10);
516: sensorValues[i] = sensorLastValue;
517: sensorValues_t[i] = sensorLastValue_t;
518: }
519:
520: void SaveDay() {
521: switch (countDay) {
522: case 0:
523: for (byte i = 0; i < 144; i++) {
524: day1[i] = sensorValues[i];
525: day1_t[i] = sensorValues_t[i];
526: }
527: break;
528: case 1:
529: for (byte i = 0; i < 144; i++) {
530: day2[i] = sensorValues[i];
531: day2_t[i] = sensorValues_t[i];
532: }
533: break;
534: }
535: }
536:
537: void SendValues() {
538:
539: if (Serial.available() > 0) {
540: lcd.setCursor(0, 0);
541: lcd.print("Trasmissione ...");
542: lcd.setCursor(0, 1);
543: lcd.print("in corso");
544: byte x = 0;
545: for (x = 0; x < 2; x++) {
546: switch (x) {
547: case 0:
548: for (byte i = 0; i < 144; i++) {
549: Serial.println(i);
550: delay(50); // Delay a little bit to avoid bouncing
551: Serial.println("day1-umi");
552: delay(50); // Delay a little bit to avoid bouncing
553: Serial.println(day1[i]);
554: delay(50); // Delay a little bit to avoid bouncing
555: Serial.println("day1-t");
556: delay(50); // Delay a little bit to avoid bouncing
557: Serial.println(day1_t[i]);
558: delay(50); // Delay a little bit to avoid bouncing
559: }
560: break;
561: case 1:
562: for (byte i = 0; i < 144; i++) {
563: Serial.println(i);
564: delay(50); // Delay a little bit to avoid bouncing
565: Serial.println("day2-umi");
566: delay(50); // Delay a little bit to avoid bouncing
567: Serial.println(day2[i]);
568: delay(50); // Delay a little bit to avoid bouncing
569: Serial.println("day2-t");
570: delay(50); // Delay a little bit to avoid bouncing
571: Serial.println(day2_t[i]);
572: delay(50); // Delay a little bit to avoid bouncing
573: }
574: break;
575: }
576: }
577: lcd.clear();
578: lcd.setCursor(0, 1);
579: lcd.print("valori inviati");
580: settingSerial = 0;
581: } else {
582: lcd.setCursor(0, 0);
583: lcd.print("Trasmissione ...");
584: lcd.setCursor(0, 1);
585: lcd.print("no connessione");
586: }
587:
588: delay(1000);
589: lcd.clear();
590: settingSerial = 0;
591: mainMenu = true;
592: }
593: