Hello everyone. I'm new with arduino and with program language C, but I know electronics and automatics. I'm fast learner .
For project of mine I'm trying tom make relay HIGH for 5 seconds, than LOW for 10 minutes from 09 to 17 every day while displaying current time, temperature and humidity.
I'm using DS3231 module for keeping track of the time and DHT11 module for temperature and humidity.
First I tried delay() function and soon realized that its "blocking" my other code from running. So I googled a bit and mixed few codes and this is what I got (all code in attachment):
#include <dht.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3231.h>
DS3231 rtc(SDA, SCL); // Init the DS3231 using the hardware interface
Time t; // Init a Time-data structure
#define dataPin 13 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object
//TWO INDEPENDANT TIMED EVENTS
const unsigned long relay_ON = 10000; //intervals in ms
const unsigned long relay_OFF = 600000; //intervals in ms
unsigned long eventTime_ON = 0;
unsigned long eventTime_OFF = 0;
// RELAYS
int Sprinkle1Relay = 3; //prskalica1
int Sprinkle2Relay = 4; //prskalica2
int LightsRelay = 5; //svijetlo
int HeaterRelay =6; //grijač
/*//BUTTONS
const int FeedingButton = 7;
const int HeaterButton = 18;*/
//SENSORS
const int IRSensor = 7;
const int TempSensor = 18;
//LEDS
const int LightsLED = 8;
const int Sprinkle1LED = 9; //sigPrskalica1
const int Sprinkle2LED = 10; //sigPrskalica2
//const int FeedingLED = 11; //
const int HeaterLED = 12; //sigGrijač
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); // initialize serial communications:
//CLOCK SETTING
rtc.begin(); // Initialize the rtc object
// The following lines can be uncommented to set the date and time
//rtc.setDOW(SATURDAY); // Set Day-of-Week to SUNDAY
//rtc.setTime(17, 59, 0); // Set the time to 12:00:00 (24hr format)
//rtc.setDate(28, 12, 2019); // Set the date to DD/MM/YYYY
t = rtc.getTime(); // Get data from the DS3231
//INPUTS, OUTPUTS
//pinMode(IRSensor, INPUT);
//pinMode(TempSensor, INPUT);
pinMode(Sprinkle1Relay, OUTPUT);
pinMode(Sprinkle1LED, OUTPUT);
pinMode(Sprinkle2Relay, OUTPUT);
pinMode(Sprinkle2LED, OUTPUT);
pinMode(LightsRelay, OUTPUT);
pinMode(LightsLED, OUTPUT);
pinMode(HeaterRelay, OUTPUT);
pinMode(HeaterLED, OUTPUT);
//pinMode(FeedingLED, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
PrintData();
//Updateds frequently
unsigned long currentTime = millis();
if (t.hour > 9 && t.hour < 18) {
digitalWrite(Sprinkle1Relay, HIGH);
}else{
if(millis() > eventTime_ON + relay_OFF){
eventTime_ON = millis();
SprinkleON();
PrintData();
if(millis() > eventTime_OFF + relay_ON){
eventTime_OFF = millis();
SprinkleOFF();
PrintData();
}
}
}
}
void SprinkleON() {
digitalWrite(Sprinkle1Relay, LOW); // Set Sprinkle Relay On
digitalWrite(Sprinkle1LED, HIGH); // Set Sprinkle LED On
Serial.println("Navodnjavanje");
Serial.println("--------------------------------");
delay(5000);
}
void SprinkleOFF() {
digitalWrite(Sprinkle1Relay, HIGH); // Set Sprinkle Relay On
digitalWrite(Sprinkle1LED, LOW); // Set Sprinkle LED On
Serial.println("Navodnjavanje završeno");
Serial.println("--------------------------------");
delay(10000);
}
void LightsOn() {
digitalWrite(LightsRelay, LOW); // Set Lights On
digitalWrite(LightsLED, HIGH); // Set LED On
Serial.println("Svijetlo Uključeno");
}
void LightsOff() {
digitalWrite(LightsRelay, HIGH); // Set Lights Off
digitalWrite(LightsLED, LOW); // Set LED Off
Serial.println("Svijetlo Isključeno");
}
void PrintData() {
int readData = DHT.read11(dataPin); // Reads the data from the sensor
float tmp = DHT.temperature; // Gets the values of the temperature
float hmd = DHT.humidity; // Gets the values of the humidity
t = rtc.getTime(); // Get data from the DS3231
// Printing the results on the serial monitor
Serial.print("Temperatura = ");
Serial.print(tmp);
Serial.print(" *C ");
Serial.println();
Serial.print("Vlaznost vazduha = ");
Serial.print(hmd);
Serial.println(" % ");
// Send date over serial connection
Serial.print("Datum: ");
Serial.print(t.date, DEC);
Serial.print("/");
Serial.print(t.mon, DEC);
Serial.print("/");
Serial.print(t.year, DEC);
Serial.println();
// Send Day-of-Week and time
Serial.print("Dan u nedjelji: ");
Serial.print(t.dow, DEC);
Serial.println();
Serial.print("Vrijeme: ");
Serial.print(t.hour, DEC);
Serial.print(":");
Serial.print(t.min, DEC);
Serial.print(":");
Serial.print(t.sec, DEC);
Serial.println();
Serial.println("--------------------------------");
delay(2000); // Delays 2 secods, as the DHT11 sampling rate is 0.5Hz
}
Can I make this simper or different? I wanna add some code for heater and for LCD instead of Serial.print everything.
Thanks in advice!
Nikola1219.ino (4.27 KB)
Nikola1219.ino (4.27 KB)