Probably the only thing more annoying than the microprocessor is the moron that is trying to program it.
I believe I've found the error of my ways. The revised program below works as it should. Now I can take that and try to troubleshoot the main program I'm working on.
Thanks again to all for the suggestions, it really helps to know I'm not completely alone.
// Program constants:
const byte pinD12 = 12; // Output on pin D12, PB6, Output Compare 1B
const byte pinD11 = 11; // Output on pin D11, PB5, Output Compare 1A
const byte pinD7 = 7; // Output on pin D7, PH4, Output Compare 4B
const byte pinD6 = 6; // Output on pin D6, PH3, Output Compare 4A
const byte pinD5 = 5; // Output on pin D5, PE3, Output Compare 3A
const byte pinD19 = 19; // ISR input on pin D19, INT2
const byte pinD45 = 45; // Output on pin D45, PL4
const byte pinD49 = 49; // Output on pin D49, PL0 (Debug output)
// Program variables:
volatile bool ckpFlag; // Flag to tell the main loop that we have just had falling edge on pin 19
volatile unsigned int iteration;
volatile unsigned long curTimeStampUS; // Current microsecond time stamp
volatile unsigned long prevTimeStampUS; // Previous microsecond time stamp
volatile unsigned long curToothPeriod; // Period between time stamps
volatile unsigned long prevToothPeriod; // Previous period between time stamps
volatile bool periodOKFlag; // Flag to indicate that the interrupt period is great enough not to be noise and short enough that
// calculations that us the period can be doen without overflow
volatile byte pinD49State; // Debug output state
void pinD19Interrupt()
{
iteration++;
ckpFlag = true;
}
void setup()
{
pinMode(pinD12, OUTPUT); // Declare pinD12 an output
digitalWrite(pinD12, LOW); // Initialize pinD12 LOW
pinMode(pinD11, OUTPUT); // Declare pinD11 an output
digitalWrite(pinD11, LOW); // Initialize pinD11 LOW
pinMode(pinD7, OUTPUT); // Declare pinD7 an output
digitalWrite(pinD7, LOW); // Initialize pinD7 LOW
pinMode(pinD6, OUTPUT); // Declare pinD6 an output
digitalWrite(pinD6, LOW); // Initialize pinD6 LOW
pinMode(pinD5, OUTPUT); // Declare pinD5 an output
digitalWrite(pinD5, LOW); // Initialize pinD5 LOW
pinMode(pinD45, OUTPUT); // Declare pinD45 an output
digitalWrite(pinD45, LOW); // Initialize pinD45 LOW
pinMode(pinD49, OUTPUT); // Declare pinD49 an output
digitalWrite(pinD49, LOW); // Initialize pinD49 LOW
attachInterrupt(digitalPinToInterrupt(pinD19), pinD19Interrupt, FALLING); // Attach pinD19 to pinD19 Interrupt() ISR function, interrupt on falling edge
Serial.begin(115200); // Initialize serial communication at 115200 bits per second(for program developement)
ckpFlag = false;
iteration = 0;
periodOKFlag = false;
pinD49State = LOW;
}
void loop()
{
if(ckpFlag == true)
{
// Serial.println(iteration); // (For program developement)
curTimeStampUS = micros(); // Get the current microsecond time stamp
curToothPeriod = curTimeStampUS - prevTimeStampUS; // Tooth period = current time stamp minus the previous one
prevToothPeriod = curToothPeriod; // Save the current tooth period as the previous tooth period
prevTimeStampUS = curTimeStampUS; // Save the current time stamp as the previous time stamp
if(periodOKFlag == false)
{
if((curToothPeriod > 35000) && (curToothPeriod< 40000))
{
periodOKFlag = true;
} // End of if((curToothPeriod > 35000) && (curToothPeriod< 40000))
ckpFlag = false;
} // End of if(periodOKFlag == false)
if(periodOKFlag == true)
{
Serial.println(iteration); // (For program developement)
digitalWrite(pinD12, HIGH);
pinD49State = !pinD49State; // Toggle pinD49State
digitalWrite(pinD49, pinD49State); // Copy pinD49State to pinD49
ckpFlag = false;
} // End of if(periodOKFlag == true)
} // End of if(ckpFlag == true)
} // End of void loop()