Hello Folks I got so good help from people here,
I made up following code>
//Leads for soil moistures 2 per soil moisture.
//divided in per module:
const byte sensor0 = A0;
#define LED_0_ON HIGH
#define LED_0_OFF LOW
#define pump_0_ON LOW
#define pump_0_OFF HIGH
const byte heartbeat_0_LED = 7;
const byte RELAY_0 = 3;
int SOIL_MOISTURE_0;
#define LEDon HIGH
#define LEDoff LOW
#define pumpON LOW
#define pumpOFF HIGH
//timing stuff
unsigned long heartbeatTime;
unsigned long machineTime;
unsigned long commonTime;
unsigned long commonInterval;
//const unsigned long pumpOnTime = 3000ul; //3 seconds
//const unsigned long pumpRestartTime = 24ul * 60 * 60 * 1000; //24 hours
//for testing
const unsigned long pumpOnTime = 8000ul; //8 seconds
const unsigned long pumpRestartTime = 10000; //10 seconds
enum MachineStates { Startup,State0,State1,State2 };
MachineStates mState = Startup;
void setup() {
Serial.begin(9600); //starts up the serial monitor.
digitalWrite(heartbeat_0_LED, LEDoff);
pinMode(heartbeat_0_LED, OUTPUT);
digitalWrite(RELAY_0, HIGH);
pinMode(RELAY_0, OUTPUT);
}
void loop() {
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatTime > 500) {
//restart this TIMER
heartbeatTime = millis();
//toggle LED
digitalWrite(heartbeat_0_LED, digitalRead(heartbeat_0_LED) == HIGH ? LOW : HIGH);
}
//********************************************** m a c h i n e T I M E R
//is it time to service the State Machine ?
if (millis() - machineTime > 1000ul) {
//restart this TIMER
machineTime = millis();
Plant_0();
}
} //END of loop()
void Plant_0() {
switch (mState) {
//******************
case Startup:
{
//next state
mState = State0;
}
break;
//******************
case State0:
{
SOIL_MOISTURE_0 = analogRead(sensor0);
Serial.print("Soil Moisture = ");
Serial.println(SOIL_MOISTURE_0);
//is the soil dry ?
if (SOIL_MOISTURE_0 > 500) {
digitalWrite(RELAY_0, pumpON);
Serial.println("\nTurning ON the pump");
Serial.println("Waiting for 3 sec");
//set the wait time
commonInterval = pumpOnTime;
//restart the common TIMER
commonTime = millis();
//next state
mState = State1;
}
}
break;
//******************
case State1:
{
//has the common TIMER expired ?
if (millis() - commonTime >= commonInterval) {
digitalWrite(RELAY_0, pumpOFF);
Serial.println("\nTurning OFF the pump");
Serial.println("Waiting for 24 hours\n");
//set the wait time
commonInterval = pumpRestartTime;
//restart the common TIMER
commonTime = millis();
//next state
mState = State2;
}
}
break;
//******************
case State2:
{
//has the common TIMER expired ?
if (millis() - commonTime >= commonInterval) {
//we will now restart the cycle
//next state
mState = State0;
}
break;
}
} //END of switch()
} //END of checkMachine()
This is my wiring diagram
The code itself is working I believe.
but I think somewhere in the wiring part its connected wrong somewhere.
Because the relay is not turning on the water pump.
Many thanks in advance