Pages: [1]   Go Down
Author Topic: HC-SR04 Ultrasonic Distance Sensor  (Read 2749 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi smiley

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)
Code:
#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):

Code:
/*
  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);
}
« Last Edit: April 28, 2012, 07:46:51 pm by cschmeer » Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like you have already copied and pasted the two sketches. Now all you have to do is copy the relevant parts of one and paste into the other. But instead of having the spacebar trigger the shutter, you want the distance sensor to trigger it, right? Say you want to trigger the shutter at 100 centimeters, substitute if statements for the case statements. You'll probably also want to substitute comparison operators in the code below instead of cm == 100. http://arduino.cc/en/Reference/HomePage This will get you started, but you need to adress the issue of focusing the camera before the shutter is triggered, and you need to work out a way that the shutter only triggers once, for how long, etc. Think of how you want to use the camera. Is it stationary with a stationary target? That will affect how you want to trigger the shutter. How long does it take to focus? You could program this in a number of ways. Such as: maybe you only want to take the photo if the target is moving towards you, away from you. etc. etc. etc. Wouldn't it be cool to use a light sensor like a  photo resistor or photo transistor to trigger the shutter during a lighting storm? I've seen those tutorials before, and I'm sure you could use a good bit of their arduino code. Good Luck

Code:
/*
  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;
int FOCUS_PIN = 6;
int SHUTTER_PIN = 7;
int duration = 0;
int cm = 0;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(FOCUS_PIN, OUTPUT);
  pinMode(SHUTTER_PIN, OUTPUT);
  digitalWrite(FOCUS_PIN, LOW);
  digitalWrite(SHUTTER_PIN, LOW);
}

void loop()
{
  distance ();                                        //This calls the distance function
  if (cm == 100) {                                  //likely you will use comparison operators here  
    digitalWrite(SHUTTER_PIN, HIGH);    //takes photo
  }
}
int distance() {

  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);
  return cm;
}
« Last Edit: April 28, 2012, 10:22:56 pm by TeslaIaint » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi thanks for your help smiley That worked and I got the camera to only take one picture at 100cm by setting it to "Single Shot" mode rather than continuous burst. The problem is that now it takes a photo once a distance of 100cm has been detected, but if you change the distance, then come back to 100cm, it doesn't take another photo. Any idea why this might be?

EDIT: I've added the code below, now it works smiley Focus isn't an issue, the lens will be manually pre-focused to the same distance the distance sensor is set to.
Code:
else {                                   
digitalWrite(SHUTTER_PIN, LOW);   
}
« Last Edit: April 29, 2012, 11:45:25 am by cschmeer » Logged

Ontario, Ohio
Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

aweseome, good work
Logged

Pages: [1]   Go Up
Jump to: