Record time between 2 sensor events using millis

Hi,

I would like to record the time between 2 sensor events using millis. My code below.

I am having trouble getting the time difference between timerOn and timerOff. Could someone please take a look at my code to see where I have gone wrong.

const int sensor1 = 6;
const int sensor2 = 9;
const int sensor3 = 5;
const int relay1 = 4;
const int relay2 = 7;
int sensorState;

unsigned long previousMillis_1 = 0;
unsigned long previousMillis_2 = 0;        // will store last time LED was updated

// constants won't change:
unsigned long sprayinterval = 5000;

boolean sensor1WasHigh = false;
boolean sensor2WasHigh = false;
boolean sensor3WasHigh = false;
boolean relay1On = false;
boolean relay2On = false;

boolean timingFlag = false;
unsigned long timerOff;
unsigned long timerOn;
unsigned long  cowSpeed;
unsigned long cowTimer;


void setup() {
  pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);
  pinMode(sensor3, INPUT_PULLUP);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
    unsigned long currentMillis = millis();   // capture the latest value of millis()
    

    // read the state of the Sensor 1,2 and set a flag if it is HIGH:
    if (digitalRead(sensor1) == HIGH && digitalRead(sensor2) == HIGH)  {
        timerOn = millis();
        sensor1WasHigh = true;
        sensor2WasHigh = true;
        timingFlag = true;
        
        //Serial.print("timerOn: ");
        //Serial.println( timerOn );
    
    
    
    if(digitalRead(sensor3) == HIGH && timingFlag == true)
      {
        timerOff = millis();
        timingFlag = false;
        cowTimer = timerOff - timerOn;
        //cowSpeed = 300UL / timerOff;
        Serial.print("Time: ");
        Serial.println( cowTimer );
        
      }
    }
     if(cowTimer > 4000)
      {
        if (digitalRead(sensor1) == LOW && sensor1WasHigh || digitalRead(sensor2) == LOW && sensor2WasHigh )  {
        // reset the button low flag
        sensor1WasHigh = false;
        sensor2WasHigh = false;
          
        // Sensor event here
           digitalWrite(relay2, HIGH);
           relay2On = true;
           Serial.println( "Fast Spray" );
        }
      }
      else{
    //Normal Speed - Fire Relay 1
    // This if statement will only fire on the rising edge of the button input
    if (digitalRead(sensor1) == LOW && sensor1WasHigh || digitalRead(sensor2) == LOW && sensor2WasHigh )  {
        // reset the button low flag
        sensor1WasHigh = false;
        sensor2WasHigh = false;
    
        // Sensor event here
           digitalWrite(relay1, HIGH);
           relay1On = true;
           Serial.println( "Normal Spray" );
    }
    } 
    
     if (currentMillis - previousMillis_2 >= sprayinterval) {
        //save the last time you blinked the LED
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        relay1On = false;
        relay2On = false;
        previousMillis_2 = currentMillis;
        }
    

}

Describe the "trouble". What did you expect to happen, and what happened instead?

The check to set timerOff is inside the if that sets timerOn. If timerOff is ever set (unlikely I suspect) the difference is likely to be zero anyway.