Timer Circuit

So I decided to start a project where I was looking to time the opening and closing of a contactor and a circuit breaker.

The basic idea is the contactor puts an earth fault on to the system and the circuit breaker then has to clear it. Both the contactor and the circuit breaker have auxiliary contacts which can be used to measure when they are open or closed.

My idea was:

When the contactor closed onto the earth fault I would take a note of the current time with "micros"

Then when the circuit breaker opened i would take a note of that time again with "micros", subtract the two and i would have the tripping time.

The problem is that when the relay is closed it keeps resetting the currentTime so when the circuit breaker does open the value I'm recording is incorrect.

Any ideas how I can resolve this? I was considering the "for" command but I have not used this before. or to get it stuck in a loop as soon as RelayState goes HIGH

  if(RelayState== HIGH) {
    currentTime = micros();  
}


if(CBState== HIGH){
  
  
    currentTime2 = micros();
    TripTime = currentTime2 - currentTime;

You are looking at the state of CBState (the level of CBState) and resetting the time each time through the loop because it is still high. You need to record when it changes low to high , not that it is high. Look in the examples in the IDE for the state change detection example (File, Examples, Digital). Only set the time when the state becomes high not when it is high.

On a side note, you may need to debounce the contacts to make sure there is only one transition for each closure.

Perfect, thanks very much il have a read through the state detection example and let you know how i get on!