Hi

I am looking for some help using an HC-SR04 Ultrasonic sensor to trigger a camera at a given distance. Using several sites as reference (especially
http://www.glacialwanderer.com/hobbyrobotics/?p=13) I have already wired up the trigger (see below) and it works using the code below (which triggers the camera if you send a spacebar towards the Arduino). Now, I am stuck with "mixing" this code with a code that triggers the camera at a certain distance value coming from the ultrasonic sensor. Any ideas on how to make this work?

CODE 1 (Camera trigger)
#define FOCUS_PIN 6
#define SHUTTER_PIN 7
void setup()
{
pinMode(FOCUS_PIN, OUTPUT);
pinMode(SHUTTER_PIN, OUTPUT);
digitalWrite(FOCUS_PIN, LOW);
digitalWrite(SHUTTER_PIN, LOW);
Serial.begin(9600); // open serial
Serial.println("Press 'f' to focus and 'spacebar' to trigger shutter");
}
void loop()
{
int cmd;
while (Serial.available() > 0)
{
cmd = Serial.read();
switch (cmd)
{
case 'f':
{
digitalWrite(FOCUS_PIN, HIGH);
delay(800); // May want to adjust this depending on focus time
digitalWrite(FOCUS_PIN, LOW);
break;
}
case ' ':
{
digitalWrite(SHUTTER_PIN, HIGH);
delay(2000); // May want to adjust this depending on shot type
digitalWrite(SHUTTER_PIN, LOW);
break;
}
default:
{
Serial.println("Press 'f' to focus and 'spacebar' to trigger shutter");
}
}
}
}
CODE2 (Ultrasonic sensor):
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
*/
int echoPin = 13;
int trigPin = 12;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = duration / 29 / 2;
/* Sound velocity=29cm/microsec,
then divide by 2 because of the return time */
Serial.print(cm);
Serial.println(" cm");
delay(500);
}