Monday, May 10, 2010

Distance-Activated Camera Trigger


This trigger was built to sense objects that are within a certain distance. The accuracy is rate fairly high between 2cm and 20 feet assuming the objects are solid and can reflect the signal. Using Processing software, I could read the data from the signal sent through the USB to illustrate the distance by the size of a blue circle. For more information, see Arduino Meets Processing.





The initial use of this sensor was to attach to a transistor-type switch to electronically release the shutter on my camera. See how the switch work here. I programmed the microcontroller to sense objects within four to twelve inches and send power to pin 13. The breadboard receives the power sending it to the base of the transistor. This in turn allows voltage to flow through and “flip the switch. Correctly placed leads from the camera jack shorts the circuit and releases the shutter. See prototype below. I used a LED to ensure the power was working correctly





Below is the schematic for the Arduino and the electronic switch.



The code to upload to the Arduino to interface with the Ping))) sensor is below.
It detect objects between four and twelve inches and sends power to pin 13.





/* Ultrasound Sensor
*------------------
*
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
*
* Initial code design was by:
*
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
* Modified by Josiah Leverich 3/27/2009
*/
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13

void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop() {
timecount = 0;
val = 0;
long duration, inches, cm;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse
* -------------------------------------------------------------------
*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

/* Writing out values to the serial port
* -------------------------------------------------------------------
*/

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

Serial.write('A'); // Example identifier for the sensor
Serial.print(ultrasoundValue);
Serial.write(10);
//serialWrite(13);

/* Lite up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/

/* Delay of program
* -------------------------------------------------------------------
*/

delay(100);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
//pinMode(ultraSoundSignal, INPUT);
//duration = pulseIn(ultraSoundSignal, HIGH);

// convert the time into a distance
inches = microsecondsToInches(ultrasoundValue);
Serial.print(inches);
Serial.print("in, ");





if (inches >= 4 inches <=12){

digitalWrite(ledPin, HIGH);

delay(500);
digitalWrite(ledPin, LOW);
}
else {
digitalWrite(ledPin, LOW);
}

}


long microsecondsToInches(long microseconds)
{

return microseconds / 29;
}









No comments:

Post a Comment