Thursday, June 16, 2011

Sound Triggered Camera Flash

After reading an article on @diyphotography on a Sound and Optical Slave Flash Trigger, I decided make a sound trigger using an Arduino.

An old computer headset is connected to a simple NPN transistor amplifier circuit and fed into one of the Arduino's analog inputs.
When the user defined threshold of sound is met, a digital pin goes HIGH. The pin is connected to a 4n35 optoisolator (scavenged from a USB charger unit) to short the contacts of the flash unit and fire the flash.

There are three LEDs which indicate the current state of the trigger unit.
Red LED (Configuration state):
The Arduino continuously listens for the sound differential between the present level and from 1us before. The analog input of the Arduino represents the voltage level from 0-5v as 0-1023. When a difference of sound of 40 or greater is detected, it is set as the current threshold to flash the trigger and the program proceeds to the test state.

Yellow LED (Test State):
In this state, the flash trigger can be tested for proper functionality. Instead of triggering a flash, an LED is lit when the sound threshold is met. When the user presses the start button, the test state is exited and proceeds to the active state.

Green LED (Active State):
In this state, the Arduino is waiting for a sound within the desired threshold to trigger the flash unit.

If the reset button is pressed the flash trigger is brought back to the configuration state.

The following is the code for the flash trigger:


int sensorPin = 0;
int sensorValue = 0;
int state = 0;
int resetPin = 6;
int startPin = 7;
int threshold = 0;
int ledPin = 5;
int flashPin = 8;
int diff = 0;
int configLED = 2;
int testLED = 3;
int activeLED = 4;

void setup() {
Serial.begin(9600);
pinMode(resetPin, INPUT);
pinMode(startPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(configLED, OUTPUT);
pinMode(testLED, OUTPUT);
pinMode(activeLED, OUTPUT);
pinMode(flashPin, OUTPUT);
}

void loop() {
if(digitalRead(resetPin) == HIGH){
state = 0;
digitalWrite(ledPin, LOW);
digitalWrite(testLED, LOW);
digitalWrite(activeLED, LOW);
}
else{
switch(state){
case 0 : //define trigger threshold
Serial.println(state);
digitalWrite(configLED, HIGH);
sensorValue = analogRead(sensorPin);
delayMicroseconds(1);
//compare present value with value from 1us before
diff = abs(sensorValue - analogRead(sensorPin));
if(diff > 30){
threshold = diff;
digitalWrite(configLED, LOW);
state = 1;
}
else state = 0;

break;

case 1 : //test trigger with LED
Serial.println(state);
Serial.println(threshold);
if(digitalRead(startPin) == HIGH){ //exit case if start button is pressed
state = 2;
digitalWrite(testLED, LOW);
}
else{
digitalWrite(testLED, HIGH);
sensorValue = analogRead(sensorPin);
delayMicroseconds(1);
//compare present value with value from 1ms before
diff = abs(sensorValue - analogRead(sensorPin));
if(diff >= threshold){
digitalWrite(testLED, LOW);
delay(200);
digitalWrite(testLED, HIGH);
}
}
break;

case 2 : //active mode, wait for sound within threshold
Serial.println(state);
digitalWrite(activeLED, HIGH);
sensorValue = analogRead(sensorPin);
delayMicroseconds(1);
//compare present value with value from 1ms before
diff = abs(sensorValue - analogRead(sensorPin));
if(diff >= threshold){
digitalWrite(flashPin, HIGH);
Serial.println("FLASH!!");
digitalWrite(flashPin, LOW);
delay(4000);
}
else
digitalWrite(flashPin, LOW);
break;
}
}
}
--------------------------

No comments:

Post a Comment