0

I am currently working on the fan function, which would be able to switch every minute for 10 seconds with RTC DS3231, at the same time I would like to incorporate a temperature sensor DHT22 and button, in the case of program part DHT22 + button , it also works, but if I have a DHT22 + RTC DS3231 + Button, only the temperature sensor and the button work. Thank you for any advice

#include "DHT.h"   
#include <LiquidCrystal_I2C.h> 
#include <DS3231.h>

#define DHTPIN A2                       
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

DS3231  rtc(SDA, SCL);
Time t;



const int OnSec = 30; //TURN ON


const int OffSec = 50; //TURN OFF


float set_temp_cool = 27.0; //setting max temperature                                                                                          


int relay_1   = 3; //relay pin
int button_1  = 8; //button pin

LiquidCrystal_I2C lcd(0x27, 2, 1,0,4,5,6,7,3, POSITIVE);

void setup() { 
 lcd.begin(20,4); 
 dht.begin();
 rtc.begin();

 pinMode(relay_1,OUTPUT);           //fan
 pinMode(button_1, INPUT_PULLUP);   //fan - button  
}

void loop() {
  t = rtc.getTime();

  Zobrazeni();

  Vetrak();

  lcd.clear();
}

void Zobrazeni(){
  float temp = dht.readTemperature();
  lcd.setCursor(0,0);
  lcd.print(rtc.getTimeStr());
  lcd.setCursor(0,1);
  lcd.print("Tepl: ");
  lcd.print(temp);
  lcd.print(" C");
  }

  void Vetrak(){
  float temp = dht.readTemperature();                                                                                                      
  int button_1 = digitalRead(8);
  int timerPhase = ( (t.sec < OffSec) && ( t.sec > OnSec ) ) ? 0 : 1;
  t = rtc.getTime();

  if (button_1 == LOW)      //if button is pressed - trigger relay
    {
    digitalWrite(3,LOW);
    }
  else if (temp < 26)           //if temp is above 27° - trigger relay 
    {
    digitalWrite(3, HIGH);
    }
  else               //turn on every minute in 40 sec and turn off at 50 sec                                                                                      
    {
    if (timerPhase == 1)
    {
    digitalWrite(3,HIGH);
    }
    else 
    {
    digitalWrite(3,LOW);
    }
    }
  }
2
  • What results are you getting from the RTC? If you only connect the RTC, does that work? If you log the values you are reading from the RTC to the console, do they look reasonable? What do the docs say about error codes? – Duncan C Dec 1 '19 at 21:02
  • I'm not sure this is a dupe of the ever-popular how do I combine two sketches post. The question includes what appears to be a custom sketch that attempts to incorporate both the DHT22 and the RTC module. I'd be inclined to close it as a "not enough information" question, since we don't know what's going wrong with the code without more information – Duncan C Dec 1 '19 at 22:08