کد:
#include <EEPROM.h>
#include <Bounce2.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include <SoftwareSerial.h>;
#include "JDateLib.h"
#include <SHT1x.h>
#define setBUTTON 13
#define upBUTTON 11
#define downBUTTON 12
#define dataPin 8
#define clockPin 9
Bounce set;
Bounce up;
Bounce down;
LiquidCrystal_I2C lcd(0x3F,16,2);
SoftwareSerial Bolo(2,3);
RTC_DS1307 RTC;
SHT1x sht1x(dataPin, clockPin);
unsigned long prevMillis = 0;
unsigned long currentmillis = millis();
static byte prevsetButton;
static byte prevSetMode;
byte setMode;
int tempmax = 20;
int tempmin = 20;
int Humiditymin = 20;
int Humiditymax = 20;
int state;
String jdate;
int JY,JM,JD;
const int rala1 = 4;
const int rala2 = 5;
const int rala3 = 6;
const int rala4 = 7;
byte termometru[8] ={B00100,B01010,B01010,B01110,B01110,B11111,B11111,B01110};
byte picatura[8] = {B00100,B00100,B01010,B01010,B10001,B10001,B10001,B01110,};
byte m[8] = {B00000,B00000,B10001,B01010,B00100,B10001,B01010,B00100,};
byte x[8] = {B00000,B00000,B00100,B01010,B10001,B00100,B01010,B10001,};
byte l[8] = {B00001,B00011,B00111,B01111,B01111,B00111,B00011,B00001,};
byte k[8] = {B10000,B11000,B11100,B11110,B11110,B11100,B11000,B10000,};
void setup(){
lcd.begin(16,2);
lcd.clear();
lcd.init();
lcd.backlight();
lcd.createChar(1,termometru);
lcd.createChar(2,picatura);
lcd.createChar(3,x);
lcd.createChar(4,m);
lcd.createChar(5,l);
lcd.createChar(6,k);
Serial.begin(9600);
Bolo.begin(9600);
Wire.begin();
pinMode(rala1, OUTPUT);
pinMode(rala2, OUTPUT);
pinMode(rala3, OUTPUT);
pinMode(rala4, OUTPUT);
pinMode(setBUTTON,INPUT_PULLUP);
pinMode(upBUTTON,INPUT_PULLUP);
pinMode(downBUTTON,INPUT_PULLUP);
set.attach (setBUTTON);
set.interval(30);
up.attach (upBUTTON);
up.interval(30);
down.attach (downBUTTON);
down.interval(30);;
digitalWrite(rala1, HIGH);
digitalWrite(rala2, HIGH);
digitalWrite(rala3, HIGH);
digitalWrite(rala4, HIGH);
tempmax = EEPROM.read(0);
tempmin = EEPROM.read(1);
Humiditymin = EEPROM.read(2);
Humiditymax = EEPROM.read(3);
}
void loop(){
set.update();
up.update();
down.update();
if ( setMode != 0) {
if (currentmillis - prevMillis >= 10000UL) {
setMode = 0;
prevMillis = currentmillis;
}}
prevsetButton = setBUTTON;
if (setMode >= 5) {
setMode = 0;
}
if (setMode != prevSetMode) {
lcd.clear();
prevSetMode = setMode;
}
if (set.fell()) { setMode++; }
DateTime now=RTC.now();
show_time_and_date(now); }
void show_time_and_date(DateTime datetime){
float h = sht1x.readHumidity();
float t = sht1x.readTemperatureC();
switch (setMode) {
case 0://normal
lcd.setCursor(0,0);
if(datetime.hour()<10)lcd.print("0");
lcd.print(datetime.hour(),DEC);
lcd.print(":");
if(datetime.minute()<10)lcd.print("0");
lcd.print(datetime.minute(),DEC);
lcd.setCursor(8, 0);
Date now(datetime.year(),datetime. month(), datetime.day());
JY = now.JYear();
JM = now.JMonth();
JD = now.JDay();
lcd.print((JY)-1300);
lcd.print("/");
if(JD<10)lcd.print("0");
lcd.print(JD,DEC);
lcd.print("/");
if(JM<10)lcd.print("0");
lcd.print(JM,DEC);
lcd.setCursor(1, 1);
lcd.write(1);
lcd.setCursor(3, 1);
lcd.print(t,1);
lcd.setCursor(5, 1);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(9, 1);
lcd.write(2);
lcd.setCursor(11, 1);
lcd.print(h, 1);
lcd.println("%");
prevMillis = currentmillis;
break; }
switch (setMode) {
case 1:
if (up.fell()) { tempmin++; }
if (down.fell()) { tempmin--; }
lcd.print(" Set temp:");
lcd.setCursor(1, 1);
lcd.write(4);
lcd.setCursor(2, 1);
lcd.print(tempmin);
lcd.setCursor(4, 1);
lcd.write(5);
lcd.setCursor(12, 1);
lcd.write(3);
lcd.setCursor(13, 1);
lcd.print(tempmax);
EEPROM.write(1, tempmin);
break; }
switch (setMode) {
case 2:
if (up.fell()) { tempmax++; }
if (down.fell()) { tempmax--; }
lcd.print(" Set temp:");
lcd.setCursor(1, 1);
lcd.write(4);
lcd.setCursor(2, 1);
lcd.print(tempmin);
lcd.setCursor(11, 1);
lcd.write(6);
lcd.setCursor(12, 1);
lcd.write(3);
lcd.setCursor(13, 1);
lcd.print(tempmax);
EEPROM.write(0, tempmax);
break; }
switch (setMode) {
case 3:
if (up.fell()) { Humiditymin++; }
if (down.fell()) { Humiditymin--; }
lcd.print(" Set Humidity:");
lcd.setCursor(1, 1);
lcd.write(4);
lcd.setCursor(2, 1);
lcd.print(Humiditymin);
lcd.setCursor(4, 1);
lcd.write(5);
lcd.setCursor(12, 1);
lcd.write(3);
lcd.setCursor(13, 1);
lcd.print(Humiditymax);
EEPROM.write(2, Humiditymin);
break; }
switch (setMode) {
case 4:
if (up.fell()) { Humiditymin++; }
if (down.fell()) { Humiditymin--; }
if (digitalRead(upBUTTON) == LOW) { Humiditymax++; }
if (digitalRead(downBUTTON) == LOW) { Humiditymax--; }
lcd.print(" Set Humidity:");
lcd.setCursor(1, 1);
lcd.write(4);
lcd.setCursor(2, 1);
lcd.print(Humiditymin);
lcd.setCursor(11, 1);
lcd.write(6);
lcd.setCursor(12, 1);
lcd.write(3);
lcd.setCursor(13, 1);
lcd.print(Humiditymax);
EEPROM.write(3, Humiditymax);
break; }
if(Bolo.available()) {state = Bolo.read();}
if (state == '1') {
digitalWrite(rala1, LOW);
digitalWrite(rala2, LOW); }
else if (state == '0') {
digitalWrite(rala1, HIGH);
digitalWrite(rala2, HIGH); }
else if (state == 'A') {digitalWrite(rala1,LOW); }
else if (state == 'a') {digitalWrite(rala1,HIGH);}
else if (state == 'B') {digitalWrite(rala2,LOW); }
else if (state == 'b') {digitalWrite(rala2,HIGH);}
if( t <= tempmin ) {digitalWrite(rala4,LOW); Bolo.print(" T on"); }
else if( t >= tempmax ) {digitalWrite(rala4,HIGH); Bolo.print(" T off"); }
if( h <= Humiditymin ) {digitalWrite(rala3,LOW); Bolo.print(" H on"); }
else if( h >= Humiditymax ) {digitalWrite(rala3,HIGH); Bolo.print(" H off"); }
Bolo.print(t,1);
Bolo.print("C");
Bolo.print(" ");
Bolo.print(h,1 );
Bolo.println("%");
Bolo.print(" tmin : ");
Bolo.print(tempmin );
Bolo.print(" tmax : ");
Bolo.print(tempmax );
Bolo.print(" Hmin : ");
Bolo.print(Humiditymin );
Bolo.print(" Hmax : ");
Bolo.print(Humiditymax );
}
[/QUOTE]