Two-stage camera remote switch using three relays and a timing issue.

I'm new to the Arduino programming.

I'm using a Nikon shutter release (Model MC-30A). The release button turns on the metering and focus first, then fires the shutter by pressing down to activate the second micro-switch. I am using a Arduino Uno with a Relay Shield v2.0. Pullup resistors on pins 2 & 3. So far so good.

What I need to do is to insert a time delay between the shutter and a relay to fire a flash-bulb. The flash-bulb fires 350ms before the shutter. Seems easy and I made the following code:

// Flashbulb Trigger.   Turns on LEDs on when the two-stage release button is pressed. Start of delay relay timing sketch.

const int LED1 = 4;   // The pin for the Green LED, or Relay Shield relay number 1.
const int LED2 = 6;   //The pin for the Red LED, or Relay Shield relay number 2.
const int LED3 = 5;   //The pin for the yellow LED which will be the shutter delay relay number 3.
const int BUTTON1 = 2; // The input pin where the focus switch is connected.
const int BUTTON2 = 3; // The input pin where the shutter switch is connected.

int val1 = 0; // Value will be used to store the state of the input pin.
int val2 = 0; // As above for button 2.            
int val3 = 0; // As above.

void setup() {
  pinMode(LED1, OUTPUT); // Green Focus LED and tell Arduino LED is an output.
  pinMode(LED2, OUTPUT); // Red Shutter LED and tell Arduino LED is an output.
  pinMode(LED3, OUTPUT); // Yellow Shutter Delay LED and tell Arduino LED is an output.  
  pinMode(BUTTON1, INPUT); // Button 1 is Focus switch and an input.
  pinMode(BUTTON2, INPUT); // Button 2 is Shutter switch and an input.
}

void loop(){
  val1 = digitalRead(BUTTON1); // Read input value and store it and check whether the input is HIGH (button pressed).
  if (val1 == HIGH) {
    digitalWrite(LED1, HIGH); // Turn the Green LED ON.
  }  
  else {
    digitalWrite(LED1, LOW); // Turn the Green LED OFF.
  }

  val2 = digitalRead(BUTTON2); // Read input value and store it and check whether the input is HIGH (button pressed).
  if (val2 == HIGH) {
    digitalWrite(LED2, HIGH); // Turn the Red LED ON.
    delay (350); // Time delay of Yellow LED in milliseconds.
  if (val2 == HIGH) 
    digitalWrite(LED3, HIGH); // Turn the Yellow LED ON.
  }   
  else {
    digitalWrite(LED2, LOW); //  Turn the Red LED OFF.
    digitalWrite(LED3, LOW); //  Turn the Yellow LED OFF.
  }
}

It works, with the exception of one bad problem.

Problem is that the shutter relay (Yellow LED, or Relay 3) may not fire if I release the button sooner than the flash-bulb relay has timed out in the Delay (350); It turns on and flash fires (Red LED/Relay 2), but if I release the button too soon the Yellow LED/Relay 3 will not activate and the shutter will not trip.).

I need something in the code to make sure the flash-bulb has fired, delay 350ms, AND that the shutter will fire regardless if I released the button too soon.

Thanks for any help.

Mack

When you read the button, set a flag. After the delay, test the flag, not the button. When you trip the shutter, reset the flag.

  if (val2 == HIGH) {
    digitalWrite(LED2, HIGH); // Turn the Red LED ON.
    delay (350); // Time delay of Yellow LED in milliseconds.
  if (val2 == HIGH) 
    digitalWrite(LED3, HIGH); // Turn the Yellow LED ON.
  }   
  else {
    digitalWrite(LED2, LOW); //  Turn the Red LED OFF.
    digitalWrite(LED3, LOW); //  Turn the Yellow LED OFF.
  }

If you put each { on a new line, I think it would be easier to spot the two missing curly braces.