New to Arduino, Mega 2560. I'm having a lot of trouble just trying to get a basic ISR code written to calculate the period between trigger inputs. At each iteration I am using a micro second time stamp, saving it as previous, then calculating the difference to get the period. I'm printing out the results as they happen just to see if I am doing things right, but something is very wrong. I am monitoring the inputs with a digital signal analyzer and the signal looks just fine, but the results seem to get screwed up and I have no idea why. Any help would be greatly appreciated.
Here's my code:
// ************************ Program constants **************************:
// - Pin numbers -:
const byte triggerPin = 19; // Trigger input on pin 19
// ************************ Global Variables ***************************:
volatile unsigned long curTimeStampUS; // Current microsecond time stamp
volatile unsigned long prevTimeStampUS; // Previous microsecond time stamp
volatile unsigned long periodUS; // Period between time stamps
volatile unsigned long iteration; // Iteration counter
// ************************ Program Functions **************************:
// -------------------------------------------------:
// ISR Function to calculate period between trigger inputs.
void triggerInput()
{ // Start of triggerInput ISR
// Subtract the current microsecond time stamp from the previous microsecond time stamp to get the microsecond period between trigger inputs
iteration++; // Increment iteration (For program developement)
Serial.println("iteration"); // For program developement
Serial.println(iteration); // For program developement
Serial.println("prevTimeStampUS"); // For program developement
Serial.println(prevTimeStampUS); // For program developement
curTimeStampUS = micros(); // Get the current microsecond time stamp
Serial.println("curTimeStampUS"); // For program developement
Serial.println(curTimeStampUS); // For program developement
periodUS = curTimeStampUS - prevTimeStampUS; // Tooth period = current time stamp minus the previous one
Serial.println("periodUS"); // For program developement
Serial.println(periodUS); // For program developement
prevTimeStampUS = curTimeStampUS; // Update the previous time stamp with the current one for next iteration
}// End of triggerInput ISR
// -------------------------------------------------:
// The setup function will only run once, after each powerup or reset of Arduino.
void setup()
{ // Start of setup function
curTimeStampUS = 0; // Current microsecond time stamp initialized with 0
prevTimeStampUS = 0; // Previous microsecond time stamp initialized with 0
periodUS = 0; // Period between time stamps initialized with 0
iteration = 0; // Iteration counter initialized with 0
attachInterrupt (digitalPinToInterrupt (triggerPin), triggerInput, RISING); // pulseIn() function is called when pin 19 goes from Low to High
Serial.begin(9600); // Initialize serial communication at 9600 bits per second(for program developement)
} // End of setup function
// - The loop() function contains the main program loop.
void loop()
{ // Start of loop function
// No code here yet
} // End of loop function
Here's a portion of the serial monitor print out:
iteration
1
prevTimeStampUS
0
curTimeStampUS
10307556
periodUS
10307556
iteration
2
prevTimeStampUS
10307556
curTimeStampUS
10308112
periodUS
556
iteration
3
prevTimeStampUS
10308112
curTimeStampUS
10452616
periodUS
144504
iteration
4
prevTimeStampUS
10452616
curTimeStampUS
10452940
periodUS
324
iteration
5
prevTimeStampUS
10452940
curTimeStampUS
10452204
periodUS
4294966560
iteration
6
prevTimeStampUS
10452204
curTimeStampUS
10452604
periodUS
400
iteration
7
prevTimeStampUS
10452604
curTimeStampUS
10452892
periodUS
288
iteration
8
prevTimeStampUS
10452892
curTimeStampUS
10452156
periodUS
4294966560
iteration
9
prevTimeStampUS
10452156
curTimeStampUS
10452556
periodUS
400
iteration
10
prevTimeStampUS
10452556
curTimeStampUS
10452860
periodUS
304
iteration
11
prevTimeStampUS
10452860
curTimeStampUS
10452140
periodUS
4294966576
iteration
12
prevTimeStampUS
10452140
curTimeStampUS
10452556
periodUS
416
I have a screen shot of the digital analyzer screen but I don't know how to attach it.
Thanks,
Robert