Let me start by describing what I am trying to do then perhaps someone could advise me as to the best method.

The platform:I am using an Arduino Mega

The setup:The Mega is being used to control a 90v DC motor. I control the speed via PWM using timer 2 and pin 10. I use the wire library to get the time of day from a DS1307, I use Serial1 to communicate with a touch screen and Serial (guess that would be 0) for debugging to the IDE and the command "attachInterrupt(0, doEncoder, CHANGE)" to keep track of the position via an optical encoder.

The mission!I want to set up a what I used to call a watchdog timer. I want it to make sure that a pulse from the encoder happens ever so many milli seconds. The purpose is to detect a stall condition. Something like.....

watcher code:if( I don't hear from Mr. encoder){ raise a fit and stop the motor;}

encoder code;{ I am here and counting so sent note to watcher}

Summary:So! Any ideas folks, seems I need some combination of timer, interrupt or something.

void loop( void ){ switch ( CurrentMotorState ) { case MotorInit: // This stuff could also be done in setup // Ensure the pin for the motor is off digitalWrite( MotorPin, 0 ); // Ensure the motor shaft is not rotating (save the state of the encoder, delay, check the state of the encoder) // Say hello to the human or display a fault message // If everything looks good, transition to MotorOff CurrentMotorState = MotorOff; break;

Thank you for the replay CB but because of the complexity of code running I would prefer not to use a polling method. I have found some code that looks promising but would like to see what you and the others think. I have not figured out how to enable/disable the timer interrupt call yet. help?

OK, when all else fails read the book and do it yourself. I modified Timer1 created by Jesse Tane and created Timer3. You can find my notes and the files here: http://www.arduino.cc/playground/Code/Timer1