Dear Arduino users,
since this is my first post, even though I have read and enjoyed in many posts here at the forum, where I have found plenty useful information, since I started to use arduino, I am not sure if I posted in right place.
However, I am preparing usage of arduino in existing greenhouse, which is currently controlled by PLC, but with simple controls. Motors (24V) are already in place with DPDT relays for start and direction change, and only control is done using mentioned PLC with temperature sensors for opening and closing the sidewall curtains.
My idea is to have following sensors for input: temperature, humidity, soil moisture, light, etc, and to be able to control motors to open the sidewall curtains, fans, sprinklers, light, watering, etc. And of course, to be able to control it via web interface and mobile devices. Some of mentioned idea are for the upgraded versions, but right now I need to have it running/controlling properly.
Project is based on already purchased: Arduino Mega, sensor shield, relay board, Ethernet shield, sensors, solenoid valves.
Code that I have prepared is working properly when using individual for different sensors and outputs.
However, I am currently experiencing some problems when using multiple conditions if some output is previously HIGH. I am sure that this might be trivial issue, and I have missed some obvious "rule" when writing the code, but I have spent hours reading materials and testing, but simply I cannot found solution.
Concrete problem is when output 2 (that controls the fan) is HIGH previously triggered by temp sensor, and when than when humidity is above set condition, and tries to write HIGH again to the same output, output pin on relay board is flickering.
Again, I am sure that this is trivial, and I need to revise my code again, I would really appreciate some guide on how to solve this problem.
Please found below my code:
#include <OneWire.h>
#include "DHT.h"
#include <DallasTemperature.h>
OneWire oneWire(18);
#define DHTPIN 21
#define DHTTYPE DHT22
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1 14 // Arduino Digital Output pins for relay 1 - DPDT relay for motor control
#define Relay_2 15 // Arduino Digital Output pins for relay 2 - Fan
#define Relay_3 16 // Arduino Digital Output pins for relay 3 - Sprinkler
#define Relay_4 17 // Arduino Digital Output pins for relay 4 - Pump
int setpointD = 18; // Reference Temperature for sensor Dallas DS18B20
int thresholdD = 7; // Temperature difference for sensor Dallas DS18B20
int setpointDHT = 18; // Reference Temperature for sensor DHT22
int thresholdDHT = 7; // Temperature difference for sensor Dallas DHT22
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
sensors.begin();
Serial.begin(9600);
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(4000); //Check that all relays are inactive at Reset
dht.begin();
}
void loop()
{
sensors.requestTemperatures();
float currentTempDallas1;
currentTempDallas1 = sensors.getTempCByIndex(0);
float h = dht.readHumidity();
float t = dht.readTemperature();
//Condition for triggering motor for opening sidewall curtains using relay 1, which should be initiated if either of temp sensors is higher than set value
if (currentTempDallas1 > setpointD+thresholdD || t > setpointDHT+thresholdDHT)
{
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
Serial.print("\t");
Serial.print("Sidewall curtains up");
Serial.print("\t");
}
if (currentTempDallas1 < setpointD || t < setpointDHT)
{
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
Serial.print("\t");
Serial.print("Sidewall curtains down");
Serial.print("\t");
}
//Condition for triggering the fan using relay 2, which should be initiated if either of temp sensors or humidity is higher than set value
if (currentTempDallas1 > setpointD+thresholdD || h > 75)
{
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
Serial.print("\t");
Serial.print("Fan ON");
Serial.print("\t");
}
if (currentTempDallas1 < setpointDHT || h < 75)
{
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
Serial.print("\t");
Serial.print("Fan OFF");
Serial.print("\t");
}
//Condition for triggering the sprinkler using relay 3, which should be initiated if humidity is higher than set value
if (h > 90)
{
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
Serial.print("\t");
Serial.print("Sprinkler ON");
Serial.print("\t");
}
else
{
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
Serial.print("\t");
Serial.print("Sprinkler OFF");
Serial.print("\t");
}
Serial.print("\t");
Serial.print("Temperature Dallas1: ");
Serial.print("\t");
Serial.println(currentTempDallas1,2);
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
}
delay(1000);
}
I have tried some solution I have found using case/switch but couldn't get it working and also some solution with simple if statements for different scenarios writing either HIGH or LOW, but each time arduino goes through the loop, it "clicks" trying to set pin even if it had same state as requested, which is not goo solution at all.
Also, I think that this arduino community if one of the greatest I have seen
Many thanks again.
A