Help using the arduino to time a camera delay

Firstly - I'm a newbie to arduino and my programming skills are 20 years rusty (was trained in dBIV :roll_eyes: )

After a few weeks of reading & playing with my UNO I need some help. My first proper project is a camera shutter but because I'm doing it a little different I decided to try & time the delay - ideally I want to capture lightning.

I know this isnt the normal way to do it but here goes - I'm using a solar cell as the light detector, a pot to set the sensitivity and have an optical isolator between the arduino and the camera trigger.

The camera is prefocused and the focus line is grounded so I am only switching the shutter button.

It's working, but the delay is a little longer than I want - Testing with flicking a lighter or even firing a slave flash 20 metres away works, but the corresponding photo shows no sign of the trigger light itself. So I naturally decided to try & time it.

I have a 1638 that is counting after the trigger level is met. The photos of the counter are showing (it think) 26ms with a shutter speed of 1/125 but what it isnt showing is the delay in the detector... which is what I want to know about too.

And a longer shutter (1/50) is showing the 26/27 delay.

What I want to do, is have the arduino fire the trigger led on the detector, start the timer and then send the camera trigger but every attempt I have made so far the timer stops when the fire signal is sent.

Thanks in advance for any help, and please understand 2 things, this is about the limit of what I have learnt after a couple of weeks bashing about and also - most of my electronics are scavenged form old printers etc as I'm a reasonable distance from any shop I could get a 220 ohm resistor from :smiley: and I enjoy tearing down hardware with my children.

Code below, I have brutally removed some comments I've left myself and normally I wouldnt be outputting to the 1638..

#include <TM1638.h>

int led = 13;
int trigger = 2;
int potPin = A0;
int lightValue = 0;
int sensorValue = 0;
TM1638 module(8, 9, 7);
unsigned long a=1;

void setup() {
   pinMode(led, OUTPUT); 
   pinMode(trigger, OUTPUT);
}
void loop() {
int sensorValue = analogRead(potPin);  
int lightValue = analogRead(A1);  
      if (lightValue > sensorValue){
        digitalWrite(led, HIGH);
        digitalWrite(trigger, HIGH);
        for (a=1; a<1000; a++)
 {
 module.setDisplayToDecNumber(a,4,false);
 delay(1);
 } 
        digitalWrite(led, LOW);
        digitalWrite(trigger, LOW);
       }
}

int sensorValue = analogRead(potPin);  
int lightValue = analogRead(A1);

First, you're aware that these functions take time, right? Unlike reading a digital pin, reading an analog pin takes a while. Even reading a digital pin using digitalRead is not all that fast, since there is a lot of work to map the pin number to a port.

I'm using a solar cell as the light detector

Which is a relatively slow-to-react device. Not what I'd use to detect something that comes and goes as fast as lightning.

I have a 1638 that is counting after the trigger level is met.

You might me intimately familiar with a 1638, and the library you are using with it. Most of us (OK, maybe just me) are not. Links are useful.

What I want to do, is have the arduino fire the trigger led on the detector

Fire the trigger? What? As in give it a pink slip?

The Arduino can set a pin HIGH or LOW. If you use that same terminology, we can understand what you want to do.

      if (lightValue > sensorValue){
        digitalWrite(led, HIGH);
        digitalWrite(trigger, HIGH);
        for (a=1; a<1000; a++)
 {
 module.setDisplayToDecNumber(a,4,false);
 delay(1);
 }

So, by some happy miracle, lightning is detected. The LED and trigger are turned on. Then something is told to display a value 1000 times, with a 1 millisecond pause between that thing being told. Hopefully, the next lightening strike is more than a second later.

The photos of the counter are showing (it think) 26ms with a shutter speed of 1/125 but what it isnt showing is the delay in the detector.

How can the delay in the detector be shown? That delay has already occurred before you start the device displaying data.

Thanks for your reply...

Yes I am aware analogue read takes time and when I know the difference the solar cell is causing I will know if this is worthwhile. My final plan if the timing adds up is an inital setup loop that will allow setting of the sensitivity and do away with one analogue read.

The 1638 is just a display led module - more than happy to send you all the links I have found about it if you want..

Fire the trigger - well it is a light detector for a camera so writing HIGH to the pin the optical isolator is attached to allows the camera to be triggered. Will keep that in mind.

The miracle is, lightning will be detected - the module if you had taken a moment is simply there as I thought I mentioned was to try and figure out how much of a delay there is. All the extra code is slowing it down so I hope to optimise it more.

Your last question about measuring delay leads me to believe you have only looked at the code I have, not what I am trying to achieve...

Not trying to be negative... and as said, any help appreciated.

I managed to capture a balloon bursting:

Your last question about measuring delay leads me to believe you have only looked at the code I have, not what I am trying to achieve...

I read what you are trying to achieve. Then, I looked at the code. This IS the programming forum, after all.

Perhaps you really should have posted in the Project Guidance section, instead.

You said that you are trying to measure the delay. I don't see that the Arduino can do that. You have some device (a solar cell) whose characteristics are such that it will not react quickly to changes in light level. You use a method to read the light level from that device that is known to be slow. Only after that reading is complete, and another one like it to read a potentiometer, do you trigger the camera and start the display displaying different values.

All that that accomplishes is to tell you how long it takes between setting the pin HIGH and the camera taking the picture.

If you were able to (you aren't) start the display displaying ever increasing values, then measure the light level and the potentiometer setting, then trigger the camera, your picture would then show the value that I think you are trying to determine.

But, you can't have the Arduino doing two things - incrementing the counter and changing the value being displayed every millisecond and measure the light level or potentiometer value - at the same time.

Fortunately, you don't have to. Instead of a counter that you increment every millisecond, use the one (millis()) that the Arduino already updates every millisecond (or micros() that is updated every 4 microseconds).

You can record when you start taking a light level reading. Then, when the light level exceeds the potentiometer reading, set the camera pin HIGH, and, in a for loop, display the difference between now and then on the display.

Thankyou PaulS - yes you clearly have understood what I am trying to accomplish and thanks for your prompting - I was working with this in ways I understand so far so thanks for your patience.

Your solution is fantastic - then I add that to the "evidence" I get from that camera photo and I have the total delay including any unnecessary code delay.

Lightning is a rare occurrence here but I still think it's worth a try and I have since ordered another uno so I can dedicate this one to a single project instead of tearing it down when my kids want to play with the larson scanner :slight_smile:

Okay, please forgive messy code - but I have done (I think) as suggested.

I have added 3 new variables, and the final result (time start, time stop and time stop - start) is displayed on the display.

For this test, I set the pot on A0 so it only just goes off in ambient light - so it triggers high as soon as the code evaluates the analogue pin.

The final result is .01 on the display after multiple runs with different light levels so can I assume the detection delay is .01 ms?? To the below code I added a delay(10) just before the timefin variable gets set and I get an output of .11/.12/.13 waving my hand across the sensor I hope thats right in showing the loops is accurately adding the time delay.

I'm guessing that's a bit long for a short strike of lightning - but I might get lucky with higher strikes in that travel a bit in the cloud first...

#include <TM1638.h>
int led = 13;
int trigger = 2;
int potPin = A0;
int lightValue = 0;
int sensorValue = 0;
int timestart = 0;
int timefin = 0;
int timetotal = 0;
TM1638 module(8, 9, 7);
unsigned long a=1;

void setup() {
   Serial.begin(9600);
   pinMode(led, OUTPUT); 
   pinMode(trigger, OUTPUT);
}

void loop() {
int timestart = (millis());   
int sensorValue = analogRead(potPin);  
int lightValue = analogRead(A1);  
      if (lightValue > sensorValue){
        digitalWrite(led, HIGH);
//        delay(10);
        int timefin = (millis());
        digitalWrite(trigger, HIGH);
 {
int timetotal = (timefin - timestart);
   module.setDisplayToDecNumber(timetotal,4,false);
 delay(1);
 } 

        digitalWrite(led, LOW);
        digitalWrite(trigger, LOW);
       }
}
int timestart = (millis());

The parentheses around the function call are not needed. The millis() function does not return an int.

Why do you have a local variable, timestart, with the same name as a global variable?

The time taken to determine the light level, to determine the potentiometer value, to write the pin high, and to capture the time is displayed on the device. When the camera gets around to going off, it takes a picture of the non changing value displayed. It hardly seems necessary to take that picture...

I'm no longer sure what time you are trying to measure, so I can't really offer any more suggestions. I think that you have the basics down (mostly), so you should be able to start and stop (or not stop) the timer/display as needed, to measure whatever it is you are trying to measure.

Hi _6ix,

You don't live in "the village" do you?
(Tangent - the Prisoner. Patrick McGoohan. Don't worry.)

Oh just saw where you are.

Ooops.

Kia ora bro'!

Suggestion. And that's all it is.
Yes, I appreciate you want to keep things neat with the Arduino and all that, but the threshold, etc....

I would get a comparitor (spelling?) and have a multi-turn pot to adjust the threshold?

Then the output goes to a digital input pin and that's that?

Just this way it is all done "real time". Although I guess the Arduino is "fast" when taking pictures, ANY delay can be problematic.

I am working on something similar myself - soon.

Nice piccies. :slight_smile:

How did you get the double/multiple exposure on one of the pictures?

This not a programming tip, but about photography. Lightning is a quick thing. It does not last long. What I have read about photographing it, you wait for a thunderstorm, open the shutter for a long time and hope that you catch a lightning. Sometimes you do, sometimes you dont. What I think I know about lightning, is that the fastest normal camera is not fast enough to catch a lightning when it has started.

_6ix,

(pre-edit this is going to ramble on a bit, but I am thinking/typing aloud)

Ok, reading your posts again this is what I read:

You get an even happening - in this case a light level - and THEN after a set time: the camera takes a picture.

Right?

I would suggest also that you do the digitalWrite(), time delay, digitalWrite() THEN do the display stuff.

Actually that doesn't make sense either.

To take a picture, you need to digitalWrite(pin,HIGH) delay maybe a wee bit of time then digitalWrite(pin,LOW)

So, what you should try is:

wait for threshold to be exceeded
wait for user defined delay
digitalWrite(pin,HIGH)
delay(200)
digitalWrite(pin,LOW)
then display any stuff on the display.

Any clearer?

LMI:
This not a programming tip, but about photography. Lightning is a quick thing. It does not last long. What I have read about photograping it, you wait for a thunderstorm, open the shutter for a long time and hope that you catch a lightning. Sometimes you do, sometimes you dont. What I think I know about lightning, is that the fastest normal camera is not fast enough to catch a lightning when it has started.

The "Trick" I have heard about catching lightning is to get a piece of cardboard and cover the lens.

Set the camera to BULB mode and open the shutter with the cardboard in place.

When you see the lightning, remove the cardboard then release the shutter button or replace the cardboard.

You replace the cardboard if you want multiple lightning strikes on one picture.

By the way, If you have a simple sensor to catch a flash of light and a simple output to trigger the camera, you dont need a CPU in between. Then there is even less delay. But the camera is clearly the slowest part here.

Using a flash is good trick but it does not help with lightning.

LMI,

I can't say for sure, but I think _6ix wants an optional time delay as well.

Dunno why. But that's what I read.

What I think I know about lightning, is that the fastest normal camera is not fast enough to catch a lightning when it has started.

FWIW, CHDK claim their motion sensing is fast enough to capture lightning using some Canon cameras.

lost_and_confused:
Nice piccies. :slight_smile:

How did you get the double/multiple exposure on one of the pictures?

The sensitivity was a bit high, so the flash went off twice.

Still: Nice effect.

Shame (maybe) you couldn't strobe the flash and get a real multiple image picture.

Guys, I really apologise... I lost notifications on this thread and assumed it went down in a heap.

I just (really just) had the opportunity to test the arduino and my skills. Long story short I got home from work knowing there were storms in the area... so I hit my parts jar for some reliable contacts for my arduino - used a motherboard fan header and usb header and wired them to my pot, and light sensor... in a massive hurry and the sky cleared.

Bugger.

Mate gave me a call and CHCH city was being hit so I made record speed on the 10 km drive to the beach and ....

Job done.

AWOL:

What I think I know about lightning, is that the fastest normal camera is not fast enough to catch a lightning when it has started.

FWIW, CHDK claim their motion sensing is fast enough to capture lightning using some Canon cameras.

I have been following CHDK - in fact the camera that took the photo above is running it, but it doesnt have a realtime "live view" so I have resorted to arduino...