Hi all,
Thanks for reading these. Here's the scoop:
Goal: to record the exact time an IR barrier has been crossed and reformed.
Equipment: Arduino Mega, IR LED (toned at 37kHz), IR Receiver (TSOP 38238)
Code:
volatile bool pinState [1][3] = {0, 0, 0};
volatile unsigned long interruptTime [1][3] = {0, 0, 0};
int debounceDelay = 100;
volatile bool pinFlag [1][3] = {0, 0, 0};
int IRPin [1][3] = {2, 3, 19};
void ISRIRCheck0 () {
if (millis () && (millis () - interruptTime [0][0]) > debounceDelay) {
pinFlag [0][0] = 1;
pinState [0][0] = digitalRead (IRPin [0][0]);
//I will put the SD code stuff here.
}
}
void ISRIRCheck1 () {
if (millis () && (millis () - interruptTime [0][1]) > debounceDelay) {
pinFlag [0][1] = 1;
pinState [0][1] = digitalRead (IRPin [0][1]);
//I will put the SD code stuff here.
}
}
void ISRIRCheck2 () {
if (millis () && (millis () - interruptTime [0][2]) > debounceDelay) {
pinFlag [0][2] = 1;
pinState [0][2] = digitalRead (IRPin [0][2]);
//I will put the SD code stuff here.
}
}
void setup () {
pinMode (12, OUTPUT);
tone (12, 37000);
for (int i = 0; i <= 2; i++) {
pinMode (IRPin [0][i], INPUT_PULLUP);
}
Serial.begin (9600);
attachInterrupt (digitalPinToInterrupt (IRPin [0][0]), ISRIRCheck0, CHANGE);
attachInterrupt (digitalPinToInterrupt (IRPin [0][1]), ISRIRCheck1, CHANGE);
attachInterrupt (digitalPinToInterrupt (IRPin [0][2]), ISRIRCheck2, CHANGE);
}
void loop () {
for (int i = 0; i <= 2; i++) {
if (pinFlag [0][i] == 1) {
pinFlag [0][i] = 0;
Serial.print (i);
Serial.print ("_");
Serial.println (pinState [0][i]);
}
}
}
Expectation: When I cross the IR barrier with my finger, I expect to get a single reading in the serial monitor. Similarly, when I remove my finger, I also expect to get a single reading in the serial monitor.
Reality: I crossed and reformed the IR barrier across the three IR sensors only once, and have copied and pasted the serial monitor reading below. As you can see, it's not stable.
18:24:28.446 -> 0_1
18:24:28.479 -> 0_0
18:24:28.479 -> 0_1
18:24:28.479 -> 0_1
18:24:28.479 -> 0_0
18:24:28.479 -> 0_1
18:24:28.479 -> 0_0
18:24:28.479 -> 0_1
18:24:28.524 -> 0_1
18:24:28.524 -> 0_1
18:24:29.491 -> 0_0
18:24:29.491 -> 0_1
18:24:29.491 -> 0_0
18:24:29.491 -> 0_1
18:24:29.525 -> 0_0
18:24:29.525 -> 0_1
18:24:29.525 -> 0_1
18:24:29.525 -> 0_0
18:24:29.525 -> 0_1
18:24:29.525 -> 0_0
18:24:29.525 -> 0_1
18:24:29.559 -> 0_1
18:24:29.559 -> 0_0
18:24:29.559 -> 0_1
18:24:29.559 -> 0_0
18:24:31.263 -> 1_1
18:24:31.301 -> 1_0
18:24:31.301 -> 1_1
18:24:31.301 -> 1_1
18:24:31.301 -> 1_0
18:24:31.301 -> 1_1
18:24:31.301 -> 1_0
18:24:31.301 -> 1_1
18:24:31.335 -> 1_1
18:24:31.335 -> 1_0
18:24:31.335 -> 1_1
18:24:31.335 -> 1_0
18:24:31.335 -> 1_1
18:24:31.335 -> 1_1
18:24:31.335 -> 1_1
18:24:32.058 -> 1_1
18:24:32.058 -> 1_1
18:24:32.058 -> 1_0
18:24:32.058 -> 1_1
18:24:32.058 -> 1_1
18:24:32.058 -> 1_0
18:24:32.058 -> 1_1
18:24:32.092 -> 1_1
18:24:32.092 -> 1_0
18:24:32.092 -> 1_1
18:24:32.092 -> 1_0
18:24:32.092 -> 1_0
18:24:32.092 -> 1_1
18:24:32.128 -> 1_1
18:24:32.128 -> 1_0
18:24:32.128 -> 1_1
18:24:32.128 -> 1_0
18:24:32.128 -> 1_0
18:24:33.802 -> 2_1
18:24:33.802 -> 2_0
18:24:33.802 -> 2_1
18:24:34.535 -> 2_0
18:24:34.535 -> 2_1
18:24:34.535 -> 2_1
18:24:34.535 -> 2_1
18:24:34.572 -> 2_1
18:24:34.572 -> 2_0
18:24:34.572 -> 2_1
18:24:34.572 -> 2_0
Now, the question might be why am I trying to debounce the interrupt within the ISR. My final code is going to be lengthy, and so if the goal is to record the exact timing of the IR changes, I can't rely on a flag and an in-loop() debounce.
Please let me know if I need to add anything else. Thank you again.