Trying to understand why an automation task doesn't work

Hello, I have a project I'm working on that I got most of the code off another project page, however the code it came with didn't seem to function completely. I tried changing around the autoControlPlantation function but what I tried doesn't seem to fix it even though the logic of my fix seems to make sense to me.

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED PUMP(V0); 
#include "DHT.h"
#define DHTPIN 4     // pin  DATA with D4
#define SOIL_MOIST_1_PIN A0 // pin A0 with A0
#define PUMP_PIN 1   // PUMP RELAY
#define DHTTYPE DHT11   
#define DRY_SOIL      23
#define WET_SOIL      50
#define TIME_PUMP_ON  15
#define READ_SOIL_HUM_TM  10L 
#define READ_AIR_DATA_TM  2L  
#define SEND_UP_DATA_TM   10L 
#define AUTO_CTRL_TM      60L 
char auth[] = "";
char ssid[] = "i";
char pass[] = "";
float humDHT = 0;
float tempDHT = 0;
int soilMoist = 0;
boolean pumpStatus = 0;
int timePumpOn = 1; 
long sampleTimingSeconds = 20; 
long startTiming = 0;
long elapsedTime = 0;
SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  pinMode(PUMP_PIN, OUTPUT);
 aplyCmd();
  Serial.begin(115200);
  dht.begin();    
  Blynk.begin( auth, ssid , pass );
  PUMP.off();
 startTimers();
}
void loop() {
  timer.run(); 
  Blynk.run();
}
BLYNK_WRITE(3) 
{
  int i = param.asInt();
  if (i == 1)
  {
    pumpStatus = !pumpStatus;
    aplyCmd();
  }
}
void getSoilMoist(void)
{
  int i = 0;
  soilMoist = 0;
  for (i = 0; i < 10; i++)  //
  {
    soilMoist += analogRead(SOIL_MOIST_1_PIN); 
    delay(20);   
  }
  soilMoist = soilMoist / (i);
  soilMoist = map(soilMoist, 0, 1023, 0, 100); 

}
void getDhtData(void)
{
tempDHT = dht.readTemperature();
  humDHT = dht.readHumidity();
  if (isnan(humDHT) || isnan(tempDHT))   
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
}
void aplyCmd()
{
  if (pumpStatus == 1)
  {
    Blynk.notify("pump ON");
   
    PUMP.on();
  }

  else {
    digitalWrite(PUMP_PIN, HIGH);
    PUMP.off();
  }
}
void autoControlPlantation(void)
{
  if (soilMoist < DRY_SOIL) //check soil
  {
    digitalWrite(PUMP_PIN, HIGH); //turn on pump??
  }
  else (soilMoist > WET_SOIL); //check soil
{
     digitalWrite(PUMP_PIN, LOW); //turn off pump??
  }
}

void startTimers(void)
{
  timer.setInterval(READ_AIR_DATA_TM * 1000, getDhtData);
  timer.setInterval(READ_SOIL_HUM_TM * 1000, getSoilMoist);
  timer.setInterval(SEND_UP_DATA_TM * 1000, sendUptime);
  timer.setInterval(AUTO_CTRL_TM * 1000, autoControlPlantation);
}
void sendUptime()
{
  Blynk.virtualWrite(1, tempDHT); 
  Blynk.virtualWrite(2, humDHT); 
  Blynk.virtualWrite(3, soilMoist);
}

Here is the original code from the project to see what I changed

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED PUMP(V0); 
#include "DHT.h"
#include <SimpleTimer.h>
#define DHTPIN 4     // pin  DATA with D4
#define SOIL_MOIST_1_PIN A0 // pin A0 with A0
#define PUMP_PIN 13   // PUMP RELAY
#define DHTTYPE DHT11   
#define DRY_SOIL      68
#define WET_SOIL      90
#define TIME_PUMP_ON  15
#define READ_SOIL_HUM_TM  10L 
#define READ_AIR_DATA_TM  2L  
#define SEND_UP_DATA_TM   10L 
#define AUTO_CTRL_TM      60L 
char auth[] = "Blynk token";
char ssid[] = "wifi name";
char pass[] = "wifi password";
float humDHT = 0;
float tempDHT = 0;
int soilMoist = 0;
boolean pumpStatus = 0;
int timePumpOn = 1; 
long sampleTimingSeconds = 20; 
long startTiming = 0;
long elapsedTime = 0;
SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  pinMode(PUMP_PIN, OUTPUT);
 aplyCmd();
  Serial.begin(115200);
  dht.begin();    
  Blynk.begin( auth, ssid , pass );
  PUMP.off();
 startTimers();
}
void loop() {
  timer.run(); 
  Blynk.run();
}
BLYNK_WRITE(3) 
{
  int i = param.asInt();
  if (i == 1)
  {
    pumpStatus = !pumpStatus;
    aplyCmd();
  }
}
void getSoilMoist(void)
{
  int i = 0;
  soilMoist = 0;
  for (i = 0; i < 10; i++)  //
  {
    soilMoist += analogRead(SOIL_MOIST_1_PIN); 
    delay(20);   
  }
  soilMoist = soilMoist / (i);
  soilMoist = map(soilMoist, 1023, 0, 0, 100); 

}
void getDhtData(void)
{
tempDHT = dht.readTemperature();
  humDHT = dht.readHumidity();
  if (isnan(humDHT) || isnan(tempDHT))   
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
}
void aplyCmd()
{
  if (pumpStatus == 1)
  {
    Blynk.notify("pump ON");
    digitalWrite(PUMP_PIN, LOW);
    PUMP.on();
  }

  else {
    digitalWrite(PUMP_PIN, HIGH);
    PUMP.off();
  }
}
void autoControlPlantation(void)
{
  if (soilMoist < DRY_SOIL)
  {
    turnPumpOn();
  }
}
void turnPumpOn()
{
  pumpStatus = 1;
  aplyCmd();
  delay (TIME_PUMP_ON * 1000);
  pumpStatus = 0;
  aplyCmd();
}
void startTimers(void)
{
  timer.setInterval(READ_AIR_DATA_TM * 1000, getDhtData);
  timer.setInterval(READ_SOIL_HUM_TM * 1000, getSoilMoist);
  timer.setInterval(SEND_UP_DATA_TM * 1000, sendUptime);
  timer.setInterval(AUTO_CTRL_TM * 1000, autoControlPlantation);
}
void sendUptime()
{
  Blynk.virtualWrite(1, tempDHT); 
  Blynk.virtualWrite(2, humDHT); 
  Blynk.virtualWrite(3, soilMoist);
}

the aplyCmd doesn't make sense to me, and I opted to change the autoControlPlantation because the 1 second pump on time didn't make sense to begive with, it made more sense to me to make it turn off and on based on the moisture value.

Any suggestions? Thanks in advance.

There are TWO variables named "i". This will confuse YOU but will not confuse the compiler. When you divide by i (the parentheses are harmless but unnecessary), you are dividing by the "i" that contains zero. BAD ! You probably intended to divide by 10 .

There may be other issues as well.

1 second pump on time didn't make sense to begive with, it made more sense to me to make it turn off and on based on the moisture value.

It takes time for the pumped water to soak into the soil and for the moisture reading to stabilise. If you pump until the desired moisture value is seen, you will cause a flood. So the technique to use is to pump for a short time, wait for some time, read the moisture value and repeat if necessary.

The error described by vaj4088 is in the original version of the code, so clearly the code you copied was written by someone with little experience and not tested well.

PaulRB:
It takes time for the pumped water to soak into the soil and for the moisture reading to stabilise. If you pump until the desired moisture value is seen, you will cause a flood. So the technique to use is to pump for a short time, wait for some time, read the moisture value and repeat if necessary.

The error described by vaj4088 is in the original version of the code, so clearly the code you copied was written by someone with little experience and not tested well.

Thanks for the reply, that makes sense. I still don't understand why my autoControlPlantation function doesn't make any changes. I have been testing in an environment where I can just stick the sensor in a cup of water for testing and it doesn't do anything when I take the probe in and out of the water. The readings for the probe do change but no signal is sent to the pump relay.

vaj4088:
There are TWO variables named "i". This will confuse YOU but will not confuse the compiler. When you divide by i (the parentheses are harmless but unnecessary), you are dividing by the "i" that contains zero. BAD ! You probably intended to divide by 10 .

There may be other issues as well.

Yeah i don't even know what they are trying to do there. I would like to get away from the original code but my skill is very limited. the only part I did myself was the new autoControlPlantation function.

The getSoilMoist seems to work because I do get a readout from the soil probe when I dip it in and out of water. I do agree that it would be dividing by 0 the way it looks.

I'll keep playing with it.

else (soilMoist > WET_SOIL); //check soil

Two classic beginner C mistakes on that line. You missed the "if" after "else". The condition in brackets gets evaluated but because the "if" is missing, the program flow is not altered based on the evaluated condition. Second mistake: the ";" after the brackets. This ends the previous/original "if". The code after that in braces gets executed every time. So the pin controlling the pump gets set HIGH and then LOW a few microseconds later. Too short a pulse to activate a relay or motor.

This is what C is like, unfortunately. It does very little to protect you the programmer from your own mistakes. Other high-level languages do more but this slows them down and produces longer executable files. C is fast and efficient, but it's not very forgiving of mistakes.

PaulRB:

else (soilMoist > WET_SOIL); //check soil

Two classic beginner C mistakes on that line. You missed the "if" after "else". The condition in brackets gets evaluated but because the "if" is missing, the program flow is not altered based on the evaluated condition. Second mistake: the ";" after the brackets. This ends the previous/original "if". The code after that in braces gets executed every time. So the pin controlling the pump gets set HIGH and then LOW a few microseconds later. Too short a pulse to activate a relay or motor.

This is what C is like, unfortunately. It does very little to protect you the programmer from your own mistakes. Other high-level languages do more but this slows them down and produces longer executable files. C is fast and efficient, but it's not very forgiving of mistakes.

Just so i get this correctly, you are saying it should be "else if"?

Another thing I'm noticing about the code, the DHT sensor doesn't send feedback. Some functions have a void in their parentheses. would that have any bearing on that? I've gotten DHTs to work in other projects and were able to test that the hardware was working.

Yes, "else if". And loose that ";".

Try running the basic example sketch from File->Examples->DHT. If that works ok, there is another problem in your code. If it doesn't, you have a hardware problem.

Your "void" idea is a red herring, I suspect. It simply means that when the function is called, it must not be given any parameters, and the compiler will give an error if you try. A function defined with empty brackets can have any number of parameters (which can be accessed using some special functions, I've never used that feature) but if given no parameters, that's fine too and you won't get a compiler error.