I am using two vibration sensors to trigger interrupts. Both sensors will react when a shock is received on a board but one will trigger before the other. The objective is to calculate the time difference between the sensors triggering and determine which sensor triggered first.
I am using an Arduino UNO and interrupts 0 and 1.
Because the vibrators may (will) result in more than one pulse per shock, after receiving the first pulse I disconnect the interrupt. Each interrupt captures micros() and after both interrupts have been processed I calculate the difference with a plus or minus result depending on which interrupt was processed first. Before re-attaching the interrupts I use code I borrowed from Nick Gammons interrupts tutorial.
My problem is that I still get extra interrupts triggering after the interrupt processors are re-attached.
Any advice will be gratefully received.
int vibPin1 = 2;
int vibPin2 = 3;
int ledPin = 13;
int CR = 13;
volatile long timeone = 0;
volatile long timetwo = 0;
volatile long timedelay = 0;
volatile int(sequence);
void setup() {
Serial.begin(9699);
pinMode(vibPin1,INPUT);
pinMode(vibPin2,INPUT);
pinMode(ledPin,OUTPUT);
attachInterrupt(0, pin1_ISR, CHANGE);
attachInterrupt(1, pin2_ISR, CHANGE);
}
void loop() {
if (timeone * timetwo != 0) {
digitalWrite(ledPin, HIGH);
timedelay = timeone - timetwo;
Serial.print(sequence); Serial.print("\n");
Serial.print(timeone);Serial.print(" "); Serial.print(timetwo);Serial.print(" ");Serial.print(timedelay);Serial.print(" delay\n");
timetwo = 0;
timeone = 0;
delay(5000);
digitalWrite(ledPin, LOW);
sequence = 0;
EIFR = bit (INTF0);
EIFR = bit (INTF1);
attachInterrupt(0, pin1_ISR, CHANGE);
attachInterrupt(1, pin2_ISR, CHANGE);
}
}
void pin1_ISR(){
detachInterrupt(0);
if (timeone == 0)timeone = micros();
sequence = 1;
}
void pin2_ISR(){
detachInterrupt(1);
if (timetwo == 0)timetwo = micros();
sequence = 2;
}