I guess this have been done a thousand times
Here is what I came up with (Writing the code by myself and learning component by component how to wire them with the Arduino. The code joins them all together)
Took a bit of code to define the writeln() function from that discussion: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1209050315
Feel free to comment/give suggestions on the code!
Video of the setup in action (Unfortunately there is no lightning storm right now so I had to setup a fake lightning (100ms duration) on my monitor ;-))
A photo of the setup (Sorry for the messy desk ;-))
The code
/*
Benoit Lefebvre (mox@mox.ca)
2011-07-06
Lightning detector camera trigger
*/
#include <stdio.h>
char _str[32]; // 32 chars max! increase if required to avoid overflow
#define writeln(...) sprintf(_str, __VA_ARGS__); Serial.println(_str)
const byte dPinTrigger = 12; // Trigger LED Indicator
const byte dPinReady = 11; // Ready LED Indicator
const byte dPinCamera = 8;
const byte aPinPot = 0; // Potentiometer
const byte aPinLDR = 5; // LDR
const int potMax = 1000; // Maximum Potentiometer Value
const int potMin = 0; // Minimum Potentiometer Value
const int diffMax = 10; // Maximum LDR Difference to trigger
const int diffMin = 1; // Minimum LDR Difference to trigger
const int delayAfterTrigger = 1000; // Delay after trigger (wait time)
const int delayHoldShutter = 100; // Delay how long we hold the shutter
int potVal = 0;
int newLDRVal = 0;
int oldLDRVal = 0;
void setup() {
Serial.begin(9600);
Serial.println("Initializing...");
pinMode(dPinTrigger, OUTPUT);
pinMode(dPinReady, OUTPUT);
pinMode(dPinCamera, OUTPUT);
digitalWrite(dPinTrigger, LOW);
digitalWrite(dPinReady, HIGH);
digitalWrite(dPinCamera, LOW);
oldLDRVal = analogRead(aPinLDR);
}
void loop() {
// Read Potentiometer and LDR
potVal = analogRead(aPinPot);
newLDRVal = analogRead(aPinLDR);
// Calculate lightning difference tolerance to trigger
int diff = (int)(diffMin + ((potVal * (diffMax - diffMin)) / (potMax - potMin)));
// Trigger the camera
if ((newLDRVal - oldLDRVal) > diff) {
triggerCamera();
}
// Debug to serial port if necessary
//writeln("POT: %d LDR: %d DIFF: %d", potVal, newLDRVal, diff);
oldLDRVal = newLDRVal;
}
void triggerCamera() {
// Trigger camera at first!
digitalWrite(dPinCamera, HIGH);
writeln("CAMERA TRIGGERED!");
digitalWrite(dPinReady, LOW);
digitalWrite(dPinTrigger, HIGH);
delay(delayHoldShutter);
// Reset everything
digitalWrite(dPinCamera, LOW);
delay(delayAfterTrigger);
digitalWrite(dPinReady, HIGH);
digitalWrite(dPinTrigger, LOW);
// Read LDR before returning to loop
oldLDRVal = analogRead(aPinLDR);
}