Problem about millis

Hi,

I have a problem in my code. I want to measure time between sensors. Both of sensors HIGH default, when first sensor is LOW, the time should be recorded and when second sensor is LOW meanwhile first sensor back to HIGH, the time should be recorded. Then subtraction between times and I get the result.

The problem is when first condition is provided, millis doesn't work or it is default zero. So when I reset the Arduino, I get the result plus waiting time for the first condition.

I hope you understand. Here is my code. What is the problem? Thank you.

if(digitalRead(13)== LOW && (digitalRead(12)) == HIGH){
  
start1Time = millis();


}

if(digitalRead(12) == LOW && (digitalRead(13)) == HIGH) { 

stop1Time = millis();
Serial.print("Fall Time1 = ");
Serial.println(stop1Time - start1Time);

added some ( ) Try this out

if((digitalRead(13)== LOW) && (digitalRead(12) == HIGH)){
  
start1Time = millis();


}

if((digitalRead(12) == LOW) && (digitalRead(13) == HIGH)) { 

stop1Time = millis();
Serial.print("Fall Time1 = ");
Serial.println(stop1Time - start1Time);

Unfortunately didn't work. Still count the waiting time.

Sounds like the start condition is not being sensed. Try this. If you don't get output it means that one or both of your sensors are stuck on HIGH.

unsigned long startTime = 0;
unsigned long stopTime = 0;

void setup() {
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
}

void loop() {
  while (digitalRead(13) == HIGH); // Active LOW
  startTime = millis();
  while (digitalRead(12) == HIGH); // Active LOW
  stopTime = millis();

  Serial.print("Fall Time1 = ");
  Serial.println(stopTime - startTime);
}

johnwasser:
Sounds like the start condition is not being sensed. Try this. If you don't get output it means that one or both of your sensors are stuck on HIGH.

Thank you. It works now. I have one more question. On the COM, I see 0, 0, 0... continuously. When it is measured, hard to see the result. How can I modify the code so that I can see the results only when it is measured?

umtakcn:
Thank you. It works now. I have one more question. On the COM, I see 0, 0, 0... continuously. When it is measured, hard to see the result. How can I modify the code so that I can see the results only when it is measured?

if((stopTime - startTime)  > 0 ){
  Serial.print("Fall Time1 = ");
  Serial.println(stopTime - startTime);
}

Thank you. I works without problem now.

umtakcn:
Thank you. It works now. I have one more question. On the COM, I see 0, 0, 0... continuously. When it is measured, hard to see the result. How can I modify the code so that I can see the results only when it is measured?

The only way for that to happen is for both of your inputs to be stuck LOW. How do your sensors work? Are they not supposed to go back to HIGH?

johnwasser:
The only way for that to happen is for both of your inputs to be stuck LOW. How do your sensors work? Are they not supposed to go back to HIGH?

I used the condition zhomeslice give and it is good now. Actually sensors go back to HIGH. Sensors are photodiodes and there are leds against them. The ball passes through and they go LOW for a moment. I measure the time between them. This is the setup.