1
32
33#include "Wire.h"
34#include <LCD.h>
35#include <LiquidCrystal_I2C.h>
36
37#define I2C_ADDR 0x3F
38#define BACKLIGHT_PIN 3
39#define En_pin 2
40#define Rw_pin 1
41#define Rs_pin 0
42#define D4_pin 4
43#define D5_pin 5
44#define D6_pin 6
45#define D7_pin 7
46LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
47
48int ore=0;
49int minuti=0;
50int numeroBagnature=0;
51int secondi;
52int giorni;
53
54#define DS3231_I2C_ADDRESS 0x68
55
56byte decToBcd(byte val)
57{
58 return( (val/10*16) + (val%10) );
59}
60
61byte bcdToDec(byte val)
62{
63 return( (val/16*10) + (val%16) );
64}
65
66void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
67dayOfMonth, byte month, byte year)
68{
69
70 Wire.beginTransmission(DS3231_I2C_ADDRESS);
71 Wire.write(0);
72 Wire.write(decToBcd(second));
73 Wire.write(decToBcd(minute));
74 Wire.write(decToBcd(hour));
75 Wire.write(decToBcd(dayOfWeek));
76 Wire.write(decToBcd(dayOfMonth));
77 Wire.write(decToBcd(month));
78 Wire.write(decToBcd(year));
79 Wire.endTransmission();
80}
81
82void setup()
83{
84 Wire.begin();
85 lcd.begin (16,2);
86
87 pinMode(2, INPUT);
88 pinMode(3, INPUT);
89 pinMode(4, INPUT);
90 pinMode(5, INPUT);
91 pinMode(6, INPUT);
92 pinMode(7, INPUT);
93 pinMode(8, INPUT);
94 pinMode(9, INPUT);
95
96 secondi = 10*(4*digitalRead(2) + 2*digitalRead(3) + digitalRead(4) +1);
97 giorni = 4*digitalRead(5) + 2*digitalRead(6) + digitalRead(7) +1;
98
99 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
100 lcd.setBacklight(HIGH);
101 lcd.clear();
102
103 lcd.setCursor (0, 0 );
104 lcd.print("Waters every ");
105 lcd.print(giorni);
106 lcd.print("dd");
107 lcd.setCursor (0, 1 );
108 lcd.print(60*ore + minuti);
109 lcd.print("' ");
110 lcd.print(" for ");
111 lcd.print(secondi);
112 lcd.print("'' ");
113 delay(5000);
114 lcd.clear();
115
116 pinMode(11, OUTPUT);
117 pinMode(12, OUTPUT);
118 digitalWrite(11, LOW);
119 digitalWrite(12, LOW);
120
121
122 setDS3231time(0,0,0,0,0,0,0);
123}
124
125void readDS3231time(byte *second,
126byte *minute,
127byte *hour,
128byte *dayOfWeek,
129byte *dayOfMonth,
130byte *month,
131byte *year)
132{
133 Wire.beginTransmission(DS3231_I2C_ADDRESS);
134 Wire.write(0);
135 Wire.endTransmission();
136 Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
137
138 *second = bcdToDec(Wire.read() & 0x7f);
139 *minute = bcdToDec(Wire.read());
140 *hour = bcdToDec(Wire.read() & 0x3f);
141 *dayOfWeek = bcdToDec(Wire.read());
142 *dayOfMonth = bcdToDec(Wire.read());
143 *month = bcdToDec(Wire.read());
144 *year = bcdToDec(Wire.read());
145}
146
147void bagna()
148{
149 digitalWrite(11, HIGH);
150 lcd.clear();
151 lcd.print(" WATERING ");
152 delay(secondi*1000);
153 digitalWrite(11, LOW);
154
155 setDS3231time(0,0,0,0,0,0,0);
156 lcd.clear();
157
158 numeroBagnature= ++numeroBagnature;
159}
160
161void displayTime()
162{
163 int minutiMancanti;
164 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
165
166 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
167 &year);
168
169 minutiMancanti=giorni*24*60 + ore*60 + minuti - dayOfWeek*24*60 - 60*hour - minute;
170 if (minutiMancanti > 0)
171 {
172 lcd.setCursor (2, 0 );
173 lcd.print(dayOfWeek, DEC);
174 lcd.print("gg ");
175 if (hour<10) lcd.print("0");
176 lcd.print(hour, DEC);
177
178 lcd.print(":");
179 if (minute<10) lcd.print("0");
180 lcd.print(minute, DEC);
181 lcd.print(":");
182 if (second<10) lcd.print("0");
183 lcd.print(second, DEC);
184 lcd.setCursor (0, 1);
185 lcd.print("Bagnato ");
186 lcd.print(numeroBagnature);
187 lcd.print(" volte");
188 }
189 else bagna();
190}
191
192void loop()
193{
194 displayTime();
195 delay(500);
196 lcd.setCursor(0,1);
197 lcd.print(" ");
198}
199