Timingproblem

Hi,

the sketch is part of a present for my neighbour. He wants to control the watering of his garden via his mobile phone.

I wrote a sketch, so he can turn the 2 valves manually on and off, or he can select a time after that the first valve turns off, the other turns on, and after the definied time the second valve turns off.
As add-on he can select an number of days, after that this should repeat (for beeing on holidays).

I tested it with short times (5 oder 10 minutes), and it worked fine.

Yesterday he told me, he tried it with 90 minutes for each valve, but the first valve did not close after 90 minutes, and the second valve did not open.
At first I thought, the problem is “int” and not “long” oder “unsigned long”. But that was not the reason of the fault.

It written for an ESP8266-01.
The basic of this sketch is 8266-Server from Rui Santos.

If you tab on the first line, you’ll get the actually time oder status of the valves.

Thanks for your help !

Petrus1.2.3-i.ino (13.4 KB)

Sorry, that code is just too massive to take on. Have you tried using serial debug statements to try and gain some insight?

Have you done any testing, to attempt to reproduce the problem, on your bench instead of in the neighbor's installation?

I used the serial monitor to know, in witch part the program is, and if millis work or not, and how many time is left.

I could try to test it with so times (so I did it with 5, 10 oder 15 minutes), but I'd like to understand the reason for this issue.

What happens if you test it with 90 minutes?

aragorn455:
I could try to test it with so times (so I did it with 5, 10 oder 15 minutes), but I'd like to understand the reason for this issue.

That is like a mechanic who needs to know what is wrong with the car, before trying to start it.

Nothing happens after 90 minutes. Only the timecounter goes on....

It must have something to do with this part:

if (Semi==1 && Phase==1) { // Start automatic = Phase 1
digitalWrite(output5, HIGH); // Valve 1 =on
output5State = "on";
preMillis5=millis();
digitalWrite(output4, LOW); // Valve 2 0 off
Phase=2;
previousMillis=millis();}
if (Semi==1 && Phase ==2) { // Phase 2 = Valve 1 = on
Serial.print ("Phase=2 ");
Serial.println(currentMillis-previousMillis);
currentMillis=millis();}

if (Semi==1 && Phase==2 && currentMillis - previousMillis >=ZBU) { // ZBU = time valve1 =on
Phase=3; // Phase 3 reset millis
previousMillis=millis();
Phase=4;}
if (Semi==1 && Phase==4){ // Phase 4 = valve 1 = off, valve 2 = on
Serial.print ("Phase=4 ");
digitalWrite(output5, LOW);
output5State="off";
digitalWrite(output4, HIGH);
output4State = "on";
Phase=5;
preMillis4=millis();}
if (Semi==1 && Phase==5){ // Phase 5 = same as Phase 2
currentMillis=millis();
Serial.print ("Phase=5 ");
Serial.println (currentMillis-previousMillis);

everything with millis is defined as unsigned long,

every variable is defined as unsigned long, execept the variables used to define oder show the minutes on the webserver.

I suspect you're getting overflow somewhere since it apparently works for small numbers of minutes. Given that you're using a 32 bit CPU, that does seem a bit unlikely. I'd suggest though that you put a UL suffix on all those large constants you're using in time calculations.

I can't read your code. All those variables with two character names are just incomprehensible. Of course, part of the issue is just that I don't speak German, but there's no reason to be so cryptic.

Maybe my problem is the ESP-01 ?

You’ll find attached a translated sketch. :confused:

Petrus1.2.3-i.ino (13.4 KB)

My apologies, I didn't mean the comments, I was referring to the two character variable names that are hard to distinguish. The fact that they're abbreviations in German doesn't help me, but if they were a bit longer (whatever the underlying language) it would be more comprehensible.

I'd be inclined to look at each calc and see what it would take to cause overflow. While you're doing that, perhaps try a 45 minute test. Perhaps you can binary chop your way to the critical number where it works for n minutes but fails for n+1.

Break this into sections. Main loop() and response in the .ino, the wifi in a separate file, the website in a separate file. This will help a lot in sorting out what's going on. You will be able to "see" your logic better. (And so will we.)

-jim lee

jimLee:
Break this into sections. Main loop() and response in the .ino, the wifi in a separate file, the website in a separate file. This will help a lot in sorting out what's going on. You will be able to "see" your logic better. (And so will we.)

-jim lee

Hi jim lee,

I'm a newbie. How to get it into separate files ?

I wrote an other sketch with a main loop only pointing to other loops

Example:

void loop {
wifi ();
ntp ();
Read inputs ();
Webserver ();
}

I think, you don't mean this, do you ?

No, I think he really means put it in separate files. Personally I'd prefer it in one for something of that size, but splitting it into separate functions is definitely good.

I did mean separate files, but separate sections would be fine as well. Just so you can say, This stuff is in this box, that stuff is in that box over there. They hook together like this..

-jim lee

aarg:
Have you done any testing, to attempt to reproduce the problem, on your bench instead of in the neighbor's installation?

I've tested it today with an usb-HW305:

Everything is fine, after 90 minutes the next step in the programm runs.

?????!