آقا یکم بیشتر سر کد ها فکر کردم و تونستم که دستورات if رو به نحوی تو برنامه جا بدم
کد:
#include "bounce2.h"
#include "dht.h"
#include "liquidcrystal.h"
#define setbutton 13
#define upbutton 12
#define downbutton 11
bounce setbuttondebouncer;
bounce upbuttondebouncer;
bounce downbuttondebouncer;
volatile int up = 30;
#define dhtpin 2
#define dhttype dht21
dht dht(dhtpin, dhttype);
liquidcrystal lcd(8, 7, 6, 5, 4, 3);
const int temprelay = 10;
const int humidityrelay = 9;
void setup() {
lcd.begin(16, 2);
lcd.clear();
dht.begin();
setbuttondebouncer.attach (setbutton);
setbuttondebouncer.interval(50);
upbuttondebouncer.attach (upbutton);
upbuttondebouncer.interval(50);
downbuttondebouncer.attach (downbutton);
downbuttondebouncer.interval(50);
pinmode (setbutton, input_pullup);
pinmode (upbutton, input_pullup);
pinmode (downbutton, input_pullup);
}
void loop() {
setbuttondebouncer.update();
upbuttondebouncer.update();
downbuttondebouncer.update();
if (setbuttondebouncer.fell ())
{
lcd.clear();
lcd.print("set humidity:");
delay(10000);
lcd.clear();
}
if(upbuttondebouncer.fell())
{
up++;
lcd.setcursor(7,2);
lcd.print(up);
}
delay(1000);
float h = dht.readhumidity();
float t = dht.readtemperature();
if (isnan(h) || isnan(t)) {
lcd.clear();
lcd.setcursor(1,0);
lcd.print("failed to read ");
lcd.setcursor(0,1);
lcd.print("from dht sensor!");
delay(2000);
lcd.clear();
return;
}
if ((t) >= 28){
digitalwrite (temprelay, high);
}
else if ((t) <= 24)
{
digitalwrite (temprelay, low);
}
if ((h) <= 78){
digitalwrite (humidityrelay, high);
}
else if ((h) >=92)
{
digitalwrite (humidityrelay, low);
}
lcd.print("humidity:");
lcd.setcursor(10,0);
lcd.print(h);
lcd.setcursor(15,0);
lcd.print("%");
lcd.setcursor(1,1);
lcd.print("temp:");
lcd.setcursor(7,1);
lcd.print(t);
lcd.setcursor(12,1);
lcd.print(" *c ");
}