hi, never used the forum before hopefully I post correctly. I have spent a while perfecting my code to control a greenhouse. it works well, however I think it could be neater. I have been looking at modular code and writing bits from new but im stuggling to take my code and change it to modular. any advice would be appreciated. Thankyou.
it reads sensors for temp, humidity and soil moisture… displays current readings… target values defined by potentiometers… displays target value while potentiometer is moving… turns on output to bring current values up or down to match target vales… 2 day flush cycle to prevent stagnant water.
#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
#include <dht.h>
#define RELAY1 (22)
#define RELAY2 (23)
#define RELAY3 (24)// soil moisture
int idealtemp = 19;
int idealhumidity = 60;
int idealsoilmoisture = 50;
int changetemp = 0;
int changesoilmoisture = analogRead(A4);
int changehumidity = analogRead(A3);
int d_length = 0;
int c = 0;
int temphum = A0;
int soilmoist = A1; // Analog input pin that the Sensor is attached
int sensorValue = 0; // value read from the Soil Moisture
int outputValue = 0;
dht sensor;
void setup()
{
//Serial.begin(57600);
// Initialise the Arduino data pins for OUTPUT
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
lcd.begin(16, 2); //16 by 2 screen
}
void dlay()
{
delay (172800000);// flushing cycle
digitalWrite(24, HIGH);
delay(300000);
digitalWrite(24, LOW);//finish flush (5 min)
c = 0;
do {
c = c + 1;
delay (1);
changetemp = analogRead(A2);
changetemp = map(changetemp, 0, 1023, 0, 40);
if (changetemp != idealtemp)
do {
idealtemp = changetemp;// save the changed value
//Serial.print(changetemp);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (“Heater on at:”);
lcd.setCursor(0, 1);
lcd.print(changetemp);
lcd.print(“C”);
changetemp = analogRead(A2);
changetemp = map(changetemp, 0, 1023, 0, 40);
}
while (changetemp != idealtemp);
//////////////////////////////////////////////////////////////////////
c = c + 1;
delay (1);
changehumidity = analogRead(A3);
changehumidity = map(changehumidity, 0, 1023, 0, 100);
if (changehumidity != idealhumidity)
do {
idealhumidity = changehumidity;// save the changed value
//Serial.print(changetemp);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (“Humidifier on at:”);
lcd.setCursor(0, 1);
lcd.print(changehumidity);
lcd.print("%");
changehumidity = analogRead(A3);
changehumidity = map(changehumidity, 0, 1023, 0, 100);
}
while (changehumidity != idealhumidity);
//
c = c + 1;
delay (1);
////////////////////////////////////////////////////////////////////////////////
c = c + 1;
delay (1);
changesoilmoisture = analogRead(A4);
changesoilmoisture = map(changesoilmoisture, 0, 1023, 0, 100);
if (changesoilmoisture != idealsoilmoisture)
do {
idealsoilmoisture = changesoilmoisture;// save the changed value
//Serial.print(changetemp);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print (“Water on at:”);
lcd.setCursor(0, 1);
lcd.print(changesoilmoisture);
lcd.print("%");
changesoilmoisture = analogRead(A4);
changesoilmoisture = map(changesoilmoisture, 0, 1023, 0, 100);
}
while (changesoilmoisture != idealsoilmoisture);
//
c = c + 1;
delay (1);
// copy and paste above code here
}
while (c != d_length);
delay (500);
}
void loop()
{
d_length = 1500;
dlay();
sensor.read11(temphum);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Humidity% =”);
lcd.print(sensor.humidity);
lcd.setCursor(0, 1);
lcd.print("Temp C = ");
lcd.print(sensor.temperature);
d_length = 1500;
dlay();
lcd.clear();
lcd.print(“Soil Moisture:”);// print the results to the LCD Display:
sensorValue = analogRead(soilmoist);// read the analog in value:
outputValue = map(sensorValue, 314, 596, 100, 0); // change for capacitive sensor changed from 101 to 100
lcd.setCursor(0, 1);
lcd.print(outputValue);
lcd.print("%");
changesoilmoisture = analogRead(A4);
changesoilmoisture = map(changesoilmoisture, 0, 1023, 0, 100);
if (changesoilmoisture != idealsoilmoisture)
idealsoilmoisture = changesoilmoisture;// save the changed value
changehumidity = analogRead(A3);
changehumidity = map(changehumidity, 0, 1023, 0, 100);
if (changehumidity != idealhumidity)
idealhumidity = changehumidity;// save the changed value
if ((sensor.temperature) > idealtemp) digitalWrite (RELAY1, HIGH);
if ((sensor.temperature) < idealtemp -2) digitalWrite (RELAY1, LOW);
if ((sensor.humidity) > idealhumidity) digitalWrite (RELAY2, LOW);
if ((sensor.humidity) < idealhumidity - 5) digitalWrite (RELAY2, HIGH);
if ((outputValue) > (idealsoilmoisture -5)) digitalWrite (RELAY3, HIGH);
if ((outputValue) < idealsoilmoisture) digitalWrite (RELAY3, LOW);
//delay (1000); digitalWrite (RELAY3, HIGH);
//delay (1000); digitalWrite (RELAY3, LOW);
//lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print(“output valaue =”);
//lcd.print(outputValue);
//lcd.setCursor(0, 1);
//lcd.print("soilmoist = ");
//lcd.print(idealsoilmoisture);
//delay(1000);
}