if condition with calculation: syntax question

unsigned long valveDelayClose; // valve closure delay 5 minutes
unsigned long startTime1; // timer for active electrovalve alarm: starting time
unsigned long currentTime1; // timer for active electrovalve alarm
void setup() {
  valveDelayClose = 300000; // 5 minuten
}
void loop() {
if ((currentTime1 - (startTime1 + valveDelayClose)) > 300000) {
              digitalWrite(alarmPin, LOW);
            }
            else {
              digitalWrite(alarmPin, HIGH); // alarm no longer valid
            }
}

In serial print output I read:
startTime1 valveOpenTime= 179771
currentTime1 = 334768
valveDelayClose = 300000

...yet at that time the alarmPin goes low.

What is wrong with the if statement?

Where do you read millis()?

I don't see Serial.print or anywhere in your posted code where currentTime1 or startTime1 are assigned any value so straight away there is a problem.

brice3010:

unsigned long valveDelayClose; // valve closure delay 5 minutes

unsigned long startTime1; // timer for active electrovalve alarm: starting time
unsigned long currentTime1; // timer for active electrovalve alarm
void setup() {
 valveDelayClose = 300000; // 5 minuten
}
void loop() {
if ((currentTime1 - (startTime1 + valveDelayClose)) > 300000) {
             digitalWrite(alarmPin, LOW);
           }
           else {
             digitalWrite(alarmPin, HIGH); // alarm no longer valid
           }
}



In serial print output I read:
startTime1 valveOpenTime= 179771
currentTime1 = 334768
valveDelayClose = 300000

...yet at that time the alarmPin goes low.

What is wrong with the if statement?

Plus, alarmPin isn't even declared so the code won't compile.

Pete

Side note - I would recommend to use 300000ul as a good practice when you deal with large integrals (or just 300000l if signed maths)

The math comes out negative but you are using unsigned values so the result is 4,294,822,293 which is larger than 300,000. Always subtract the PAST time from the NEWER time.

It appears that you are trying to see if the interval (5 minutes) would have passed if the start time was 5 minutes later than it was?!? I think the way to do that is to see if 10 minutes have passed:

if ((currentTime1 - startTime1) > (300000 + valveDelayClose))) {
              digitalWrite(alarmPin, LOW);

J-M-L:
Where do you read millis()?

I had posted just the bare minimum, or what I thought would suffice; here is more detail:

#define DS3231_I2C_ADDRESS 0x68
unsigned long valveDelayClose; // valve closure delay 5 minutes
unsigned long startTime1; // timer for active electrovalve alarm: starting time
unsigned long currentTime1; // timer for active electrovalve alarm
#define alarmPin 2
void setup() {
  valveDelayClose = 300000; // 5 minuten
  pinMode(alarmPin, OUTPUT); // GPIO2
 }
void loop() {
    currentTime1 = millis(); // read current time
   
    if ((currentTime1 - (startTime1 + valveDelayClose)) > 300000) {
              digitalWrite(alarmPin, LOW);
            }
            else {
              digitalWrite(alarmPin, HIGH); // alarm no longer valid
            }
}

If needed I can post the entire code but it is 600 lines.
Serial print is used to verify the values of various variables, and at exact the time the alarmPin is set low, the values for its parameters are:
startTime1 valveOpenTime= 179771
currentTime1 = 334768
valveDelayClose = 300000

johnwasser:
The math comes out negative but you are using unsigned values so the result is 4,294,822,293 which is larger than 300,000. Always subtract the PAST time from the NEWER time.

It appears that you are trying to see if the interval (5 minutes) would have passed if the start time was 5 minutes later than it was?!? I think the way to do that is to see if 10 minutes have passed:

if ((currentTime1 - startTime1) > (300000 + valveDelayClose))) {

digitalWrite(alarmPin, LOW);

GREAT!! That is what was missing in my reasoning, thanks a lot! +1