forked from cameradactyl/Shutter-Timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutter-Timer.ino
57 lines (47 loc) · 4.1 KB
/
Shutter-Timer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Shutter timer forked from Shutter_Timer_v1.2.ino by cameradactyl on GitHub - https://github.com/cameradactyl/Shutter-Timer/blob/master/Shutter_Timer_v1.2.ino
// Modified to use boolean flags and more descriptive variable names
long Start; // this is the time in microseconds that the shutter opens (the arduino runs a microsecond clock in the background always - it is reasonably accurate for this purpose)
long Stop; // this is the time in microseconds that the shutter closes
bool Fired = false; // this is a flag indicating when the shutter has been fired completely. when fired =1, the shutter has been fired, and the computer needs to display the information related to the exposure time.
volatile bool Risingflag = false; // this is a flag that i set in my interrupt routine, Rising flag is set to = 1 when the voltage INCREASES in the interrupt
volatile bool Fallingflag = false; // this is a flag that i set in the interrupt routine, Fallingflag is set to =1 when the voltage DECREASES in the interrupt
void setup() { //This part of the program is run exactly once on boot
Serial.begin(9600); //opens a serial connection.
attachInterrupt(digitalPinToInterrupt(2), shutterEvent, CHANGE); //run the function CLOCK, every time the voltage on pin 2 changes.
}
void loop() { // this part of the program is run, in order, over and over again, start to finish, unless INTERRUPTED by our interrupt
if(Risingflag == true){
Start = micros(); //set the variable Start to current microseconds
Risingflag = false; //reset the rising flag to 0, so that this function isnt called again until the shutter actually fires
}
if(Fallingflag == true){
Stop = micros(); // set the variable Stop to current microseconds
Fallingflag = false; //reset the falling flag to 0, so that this function isnt called again untill the shutter fires again.
Fired = true; // set the fired flag to 1, triggering the calculation of a shutter speed, and its display over the serial monitor.
}
if(Fired == true){ //if the flag Fired = 1, print this information to the serial monitor"
Serial.print("Start: ");
Serial.println(Start);
Serial.print("Stop: ");
Serial.println(Stop);
long Speed = (Stop - Start); // make a variable called speed, which is the total number of microseconds that the shutter is open for
Serial.print("Microseconds: ");
Serial.println(Speed); //display total microseconds in shutter interval
float SS = (float)Speed/1000000; // make a variable SS, which is how many seconds that the shutter open for
float SS2 = 1/SS; // make a variable SS2, which is the inverse of the SS, or 1/ the shutter speed
Serial.print("shutter speed: 1/");
Serial.println(SS2); //display the shutter speed
Serial.println();
Start = false; // reset Start to 0
Stop = false; //reset Stop to 0 . *** these are not necessarily needed, but makes errors more evident should they occur
Fired = false; //reset Fired flag to 0, so that the shutter speed will not be calclulated and displayed, until the next full interrupt cycle, where a start and stop time are generated.
}
}
void shutterEvent() { //this is the interrupt function, which is called everytime the voltage on pin 2 changes, no matter where in the main program loop that the computer is currently in
if(digitalRead(2) == HIGH){
Risingflag = true; // if the voltage on pin 2 is high, set the Risingflag to 1 : this will trigger the function called Rising from the main loop, which will set a start time
}
if(digitalRead(2) == LOW){ // . if the voltage on pin 2 is low, set the Fallingflag to 1 : this will trigger the function called Falling from the main loop, which will set the stop time, and also set the Fired flag to 1.
Fallingflag = true;
}
}