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
| #include <LiquidCrystal_I2C.h>
#include <cactus_io_DHT22.h>
#include <Wire.h>
#define DHT22IN_PIN 5
#define DHT22OUT_PIN 6
#define DS3231_I2C_ADDRESS 0x68
#define SEL 7
#define CLK 2// Connected to CLK of encoder
#define DT 4 // Connected to DT of encoder
DHT22 dhtIN(DHT22IN_PIN); // Initialize DHT sensor IN
DHT22 dhtOUT(DHT22OUT_PIN); // Initialize DHT sensor OUT
LiquidCrystal_I2C lcd(0x20,16,2); // set the LCD address to 0x20 for a 16 chars and 2 line display
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
//encoder
volatile int encoderPosCount = 0;
volatile unsigned char CLKLast;
volatile unsigned char aVal;
volatile unsigned char LimitEncoder;
bool ClearTheLCD;
bool Auto; //Choice 1
bool Stat; //Choice 2
bool SetTime; //Choice 3
bool DisplaySensor; //Choice 4
bool Disptime; //choice 5
bool Mainmenu;
void setup()
{
Serial.begin(9600);
dhtIN.begin();
dhtOUT.begin();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(00,02,0,1,24,12,18);
pinMode (CLK,INPUT);
pinMode (DT,INPUT);
pinMode (SEL, INPUT);
//CLKLast = digitalRead(CLK);
bool Auto=false; //Choice 1
bool Stat=false; //Choice 2
bool SetTime=false; //Choice 3
bool DisplaySensor=false; //Choice 4
bool Disptime=false; //choice 5
bool Mainmenu=true;
attachInterrupt(0, encoder, CHANGE);
}
void loop(){
delay(500);
bool SensInOK=true;
bool SensOutOK=true;
bool Sel;
unsigned char Nav;
Sel = digitalRead(SEL);
//Serial.println(Mainmenu);
LimitEncoder = 8;
//encoder();
Nav = encoderPosCount;
if (ClearTheLCD==true){
lcd.clear();
ClearTheLCD=false;
}
if(!Auto&&!Stat&&!SetTime&&!DisplaySensor&&!Disptime){
Mainmenu=true;
}
if (Mainmenu==true){
LimitEncoder = 8;
Nav = encoderPosCount;
switch(Nav){
case 0:
lcd.setCursor(0,0);
lcd.print("Automatic");
break;
case 2:
lcd.setCursor(0,0);
lcd.print("Statistics");
break;
case 4:
lcd.setCursor(0,0);
lcd.print("Set Time");
break;
case 6:
lcd.setCursor(0,0);
lcd.print("Display Sensors");
if (Sel == true){
DisplaySensor = true;
Mainmenu = false;
lcd.clear();
}
break;
case 8:
lcd.setCursor(0,0);
lcd.print("Display time");
if (Sel == true){
Disptime = true;
Mainmenu = false;
lcd.clear();
}
break;
}
}
// Check if any reads failed IN and exit early (to try again).
if (isnan(dhtIN.humidity) || isnan(dhtIN.temperature_C)) {
SensInOK = false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Sensor Hum&temp IN");
lcd.setCursor(0,1);
lcd.print("IN read failure!");
Serial.print(SensInOK);
delay(2000);
}
else SensInOK=true;
// Check if any reads failed OUT and exit early (to try again).
if (isnan(dhtOUT.humidity) || isnan(dhtOUT.temperature_C)) {
SensOutOK = false;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Sensor Hum&temp OUT");
lcd.setCursor(0,1);
lcd.print("OUT read failure!");
delay(2000);
}
else SensOutOK=true;
if (SetTime==true){
}
if (Auto==true&&SensInOK==true&&SensOutOK){
}
if (Stat==true){
}
if (DisplaySensor == true){
dhtIN.readHumidity();
dhtIN.readTemperature();
dhtOUT.readHumidity();
dhtOUT.readTemperature();
if (SensInOK == true&&SensOutOK==true ){
lcd.setCursor(0,0);
lcd.print("IN ");
lcd.setCursor(3,0);
lcd.print(dhtIN.humidity);
lcd.setCursor(8,0);
lcd.print("% t");
lcd.setCursor(11,0);
lcd.print(dhtIN.temperature_C);
lcd.setCursor(0,1);
lcd.print("OUT");
lcd.setCursor(3,1);
lcd.print(dhtOUT.humidity);
lcd.setCursor(8,1);
lcd.print("% t");
lcd.setCursor(11,1);
lcd.print(dhtOUT.temperature_C);
}
delay(2000);
if (Nav!=6){
lcd.clear();
DisplaySensor = false;
encoderPosCount = 0;
}
}
if (Disptime==true){
displayTime(); // display the real-time clock data on the LCD
if (Nav!=8){
lcd.clear();
Disptime = false;
encoderPosCount = 0;
}
}
}//end of loop
void encoder (){ //encoder ISR
aVal = digitalRead(CLK);
if (aVal != CLKLast){ // if the knob is rotating, we need to determine direction
// We do that by reading pin B.
//lcd.clear();
if (digitalRead(DT) != aVal && encoderPosCount <= (LimitEncoder-1)) {
// Means pin A Changed first-We're Rotating Clockwise
encoderPosCount ++;
}
else if (encoderPosCount >=1){// Otherwise B changed first and we're moving CCW
encoderPosCount--;
}
Serial.print("Encoder Position: ");
Serial.println(encoderPosCount);
}
CLKLast = aVal;
ClearTheLCD =true;
} //Fin encoder
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
lcd.setCursor(0,0);
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.setCursor(2,0);
lcd.print("h");
if (minute<10)
{
lcd.setCursor(3,0);
lcd.print("0");
}
lcd.setCursor(3,0);
lcd.print(minute, DEC);
lcd.setCursor(5,0);
lcd.print(":");
if (second<10)
{
lcd.setCursor(7,0);
lcd.print(" ");
}
lcd.setCursor(6,0);
lcd.print(second, DEC);
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(dayOfMonth, DEC);
lcd.setCursor(2,1);
lcd.print("/");
lcd.setCursor(3,1);
lcd.print(month, DEC);
lcd.setCursor(5,1);
lcd.print("/");
lcd.setCursor(6,1);
lcd.print(year, DEC);
switch(dayOfWeek){
case 1:
lcd.setCursor(9,1);
lcd.print("Sunday ");
break;
case 2:
lcd.setCursor(9,1);
lcd.print("Monday ");
break;
case 3:
lcd.setCursor(9,1);
lcd.print("Tuesday ");
break;
case 4:
lcd.setCursor(9,1);
lcd.print("Wednesday");
break;
case 5:
lcd.setCursor(9,1);
lcd.print("Thursday");
break;
case 6:
lcd.setCursor(9,1);
lcd.print("Friday");
break;
case 7:
lcd.setCursor(9,1);
lcd.print("Saturday");
break;
}
} |
Partager