Edit: Regarding your updated code, I suggest a few more changes:
As noticed by 6v6gt, do not rely on
calcPulse()settingnewPulsetofalse, as this makes your program too dependent on timings. Instead, set it tofalseinloop(), right after toggling the variablecameraEnable(which should be named “cameraEnabled”). I may be repeating myself here.Computing the pulse width should not be conditioned on
newPulse == false. There is no reason to do so: every pulse should be measured.Do not use
startPeriodin a boolean context: the value 0 is a perfectly valid return value formicros().
Here is a version of calcPulse() with all unnecessary stuff removed:
void calcPulse() {
unsigned long now = micros();
if (digitalRead(pulsePin) == HIGH) { // pulse starts
pulseStartTime = now;
} else { // pulse ends
unsigned long pulseLength = now - pulseStartTime;
if (pulseLength > 1500) {
newPulse = true;
}
}
}
I just noticed that the newPulse variable is not actually needed: the
interrupt handler could just take care of toggling cameraEnabled. This
should also implify loop():
unsigned long pulseStartTime;
volatile bool cameraEnabled = false; // is the camera firing?
// Interrupt handler for CHANGE in pulsePin.
void calcPulse() {
unsigned long now = micros();
if (digitalRead(pulsePin) == HIGH) { // pulse starts
pulseStartTime = now;
} else { // pulse ends
unsigned long pulseLength = now - pulseStartTime;
if (pulseLength > 1500) {
cameraEnabled = !cameraEnabled;
}
}
}
void loop() {
if (cameraEnabled) {
// Fire camera.
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
}