Problem, loop failing

Good day,

I am relatively inexperienced with Arduino, this is my most complicated project yet and I have been learning lots but I have either coding or hardware causing an error. What I am doing is controlling my irrigation, an old topic but I am using my well and a pump therefor a limited water source.

When an area has the lowest moisture and this is below 70% the watering can commence till the well level goes below 10% or the soil moisture rises above 90%.

Watering can also only be done between 10-30 degrees Celsius when the humidity is below 85%.

Lastly watering can only be done daily between 05:00-09:00 and 18:00-22:00.

If the temperature, humidity, time or raining condition is not correct the system will wait checking that conditions every 15min but when watering the soil and level is checked every minute. When no watering is required logging takes place every 15min to a SD card.

My problem is that the system started correctly and watered till the well was below 10% on and then logged the moisture twice and then stopped. Nothing works since then. I have discovered that my DHT22 gives NaN readings readings regularly and have tried decreasing the amount of times I read this as I have done with the hygrometer the DHT22 however gets power constantly unlike my hygrometers which only gets power when readings are taken, ie every minute for about 2 seconds or every 15 minutes for about 2 seconds.
Attached is my current code, it is a mesh of the standard files examples and my logic.

Complete_solution_v0.6.6.ino (32.9 KB)

I have either coding or hardware causing an error.

You have orders of magnitude too much code when you do not know whether you have a hardware problem, or not.

You have WAY to many global variables.

unsigned int hour,minute, second,year,month,day;

What universe do you live in that has more than 32768 seconds in a minute, more than 32768 minutes in an hour, more than 32768 hours in a day, more than 32768 days in a month, etc.?

Is unsigned int REALLY the correct type?

String hygro1dataString;
void hygro1DataString() {

This is just plain dumb. Use your imagination. I'm sure that you can come up with names for completely different things that differ more more than case.

Using Strings on the Arduino is inappropriate. There is no need for them.

Having 4 functions that do exactly the same thing, but for different pins, is NOT appropriate. The Arduino team didn't provide 54 different functions to read the 54 digital pins on the Arduino. YOU should not provide 4 functions to read the 4 hygrometers.

   bool notraining;
   notraining= digitalRead(RAIN);

Why do you need two lines for this? What's wrong with:

   bool notraining = digitalRead(RAIN);

Does the rain sensor pull the pin high or low ALL the time? If not, YOU need to. It's simplest to do that using the internal pullup resistor.

  stringhour=String(hour, DEC);

More stupidity. You wasted resources converting hour to a String. Why do you need to convert that String back to an int? If you do NOT get the EXACT same value as is in hour, you have serious problems.

  if (soil1 < 60 && soil1 <= soil2 && soil1 <= soil3 && soil1 <= soil4 && level >= 50)  //Initial set point below minimum check and confirm area has the lowest soil moisture ***Set lower set point here

Get your enter key fixed. There is NO requirement to put the (almost useless) comment on the same line as the code. Put it on a line by itself, before the code.

Testing whether zone 1 needs watering BEFORE testing that there is water is pointless. Either there is enough water to water something, or there isn't.

If the time is right for watering, then have another test to determine if there is enough water. Only if there is should you need to determine which zone needs watering.

That method of determining the zone is flawed, in my mind. You should determine the zone that needs water the most, and water that zone. IMO.

That was as
far as I got
reading your code,
before I got seasick.
Fix that piss-poor indenting
before posting
code again.

Tools + Auto Format makes that so simple that a child could do it.

@PaulS can sometimes express himself rather bluntly but if you put that to one side his advice is good.

I don't understand how you got to have such a large program before you discovered there is a problem? Perhaps you have just added a small extra piece and it was working OK before that?

My wild guess is that your use of the String class is giving rise to memory corruption. It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.


@Robin2 Thank you, and yes PaulS advice was a rude to be honest. I come from a PLC programming environment and should have mentioned this is very messy as I used online code neaten functions which kept causing compile errors so I gave up unknowing that the Arduino IDE has the function with one click :-[

With regards to the problems I have found it. The coding was originally done in sections as you suggested, with one key thing never tested, removing the PC USB cable while programming, that I only did last week as my PC went to sleep, I only realized this last night and believe this is the error as my Serial will wait due to the setup. The serial is there as I was unaware of how easy the SD card shield logs data and now my supervisors still want to see a Raspberry PI connected which I originally proposed so thus the need for serial.

With regards to the other statements from Paul, all I tried most of his advice even before he suggested but mostly got errors when I tried it, which is most probably due to a mistake on my side but I just do not have the knowledge or experience to do all of it correctly yet.

My wild guess is that your use of the String class is giving rise to memory corruption. It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

I will look into this thank you, I do not know this function and will try it. I simply used examples from libraries so I use whatever they use and then modify the values to do my comparisons so I honestly do not know what is best practice for the Arduino for use of cstrings or String or Unsigned int or int so appreciate all suggestions.

Thank you, I appreciate all positive feedback and will update when I removed the serial code temporarily for a test, I believe all will function then. Thank you for all the valuable advice and suggestions and for PaulS teaching me about the Auto format