Trying to design transmitters/receivers for a long-range timing gate (across a wide road).
Test setup works reliably over a distance of more than 60 meters in broad daylight,
with ~120mA peak LED current (60mA current draw from a single LiPo cell).
Continuous modulated IR (no time gaps), and no optics (for a narrow ⌀ 5mm beam).
Writing some laptime test code for it (running on an Uno), and want to know if I made (interrupt) thinking errors.
Ignore the fact that a long time 1/1000 sec display is not possible with a ceramic resonator clock.
Just testing the principle here.
Reaction time should be ~1/2000 sec with a TSSP4038 receiver.
It still registers if I flick a ⌀10mm stick as fast as I can through the beam.
Leo..
// Beambreak Sensor
const byte sensorPin = 2; // 3-pin TSSP40xx IR sensor connected to pin 2, interrupt 0
const byte buzzerPin = 11; // active buzzer, to monitor IR beam
volatile unsigned long currentMillis, lastTrigger, timeout = 3000; // lockout time after a trigger
unsigned long allSeconds, previousMillis, lapTime;
unsigned int pointer, runHours, secsRemaining, runMinutes, runSeconds, runThousands;
volatile boolean printed; // print flag
char buf[33]; // holds print string
void setup() {
Serial.begin(115200); // set serial monitor to this baudrate
Serial.print(F("BeamBreak Sensor\ntimeout set to "));
Serial.print(timeout / 1000); // in seconds
Serial.println(F(" seconds\n"));
pinMode(buzzerPin, OUTPUT);
pinMode(sensorPin, INPUT_PULLUP); // aditional pull up
attachInterrupt(digitalPinToInterrupt(sensorPin), beamBreak, RISING);
}
void beamBreak() { // interrupt
currentMillis = millis(); // trigger time
if (currentMillis - lastTrigger > timeout) { // if valid
lastTrigger = currentMillis; // update
printed = false; // enable printing
} else lastTrigger = currentMillis; // false trigger(s), new timeout
}
void loop() {
digitalWrite(buzzerPin, digitalRead(sensorPin)); // buzzer follows sensor
if (!printed) { // if not printed
lapTime = currentMillis - previousMillis; // calculate lap time
previousMillis = currentMillis; // set new start time
allSeconds = lapTime / 1000; // calculate,
runHours = allSeconds / 3600; // and print lap time
secsRemaining = allSeconds % 3600;
runMinutes = secsRemaining / 60;
runSeconds = secsRemaining % 60;
runThousands = lapTime % 1000; // last three digits
sprintf(buf, "Event#%04d\tRuntime: %02d:%02d:%02d.%03d", pointer, runHours, runMinutes, runSeconds, runThousands);
Serial.println(buf); // print the above string
printed = true; // print once
pointer++; // event pointer +1
if (pointer > 9999) pointer = 0; // limit to 10000 events
}
}