Could some with Interrupt coding knowledge look over my project help me??

Hi, I have a sensor that I want monitored and if no change in the sensor for 250ms a relay will trip. I have other loop code with other sensors not as critical sensors as this one that I stripped out and will just focus on the interrupt sensor and relay. My approach to this is:

Set up a timer for 250ms. Have the sensor interrupt and reset the timer at each sensor change: rise and fall.

If the timer expires the gear is stopped

I believe this will need 2 interrupts: One for the sensor to reset the timer (I called cogtimerreset) and the other for when the timer expires and sets a 'stopped' flag...but I'm not sure how to code this as an interrupt since it's not from an external pin...and I'm not that familaier with interrupts...

The main routine will just watch for the ' stopped' flag and trip relay I haven't ever coded for interrupts...could someone help me out? Thanks!!

int sensePin =0; //gear sensor 5v digital on pin 2 on Uno but interrupt calls it pin 0

int relayPin =3; // relay to cut power to main system

unsigned long cogtimer;  //250ms cog count down timer for interrupt

void setup(){
  
  pinMode(relayPin, OUTPUT);
  
  pinMode(sensePin, INPUT);
  
  
  cogtimer = millis(); //beginning timer for 250ms timer Not sure if this belongs here
   
  attachInterrupt(sensePin, cogtimerreset, CHANGE);  //cog timer interrupt from sensor change

  Serial.begin(9600);
}


void cogtimerreset()    //interrupt from cog sensor to reset the 250ms cogtimer
{  cogtimer = millis(); //resets timer as long as sensor is changing

}

void loop() 
{
  if (cogtimer - millis() > 250)
  {digitalWrite(relayPin, HIGH);  //If sensor stops reseting the timer the relay trips when time > 250ms
  
 }}

I don't see the word Volatile in your code ;)

does it belong here?

Volatile unsigned long cogtimer; //250ms cog count down timer for interrupt

Yes, and see if that makes a difference?

Thank you...my code as written I haven't tested yet...but I was thinking that ...

"I believe this will need 2 interrupts: One for the sensor to reset the timer (I called cogtimerreset) [DONE!!] and an another for when the timer expires and sets a 'stopped' flag...but I'm not sure how to code this as an interrupt.."

It seems the second part is equally important to quickly trip the relay if time >250ms....

any suggestions?

Thanks!

If you are only concerned about time intervals of 250 millisecs there is no need to use interrupts. Polling should be fine and a lot easier to implement and debug.

...R