/
PhotoMagnet.ino
132 lines (104 loc) · 2.96 KB
/
PhotoMagnet.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
int pin_out_camera=5; // experimental, not active yet
int pin_out_magnet=6;
int pin_out_flash=7;
int log_level=50; // for future use, could make the program less chatty
// default values
int delay_before_magnet=1000,delay_after_magnet=0;
int flash_duration=10;
int magnet_duration=50;
int camera_duration=0;
#define INLENGTH 20
char inString[INLENGTH+1];
int inCount;
#define INTERMINATOR 13
void setup()
{
Serial.begin(38400);
Serial.println("PhotoMagnet V1.01");
Serial.println();
pinMode(pin_out_magnet,OUTPUT);
pinMode(pin_out_flash,OUTPUT);
pinMode(pin_out_camera,OUTPUT);
}
void waitForEnter() {
Serial.println("---Press Enter to Continue>");
// read string until EOF
inCount=0;
do {
while (Serial.available()==0);
inString[inCount]=Serial.read();
if (inString[inCount]==INTERMINATOR)
break;
} while (++inCount<INLENGTH);
}
void readInt(char *what, int old_value, int *p_new_value) {
Serial.print("Enter ");
Serial.print(what);
Serial.print("(old value is ");
Serial.print(old_value);
Serial.print(") >");
// read string until EOF
inCount=0;
do {
while (Serial.available()==0);
inString[inCount]=Serial.read();
if (inString[inCount]==INTERMINATOR)
break;
} while (++inCount<INLENGTH);
// if empty string, return old value
if (inCount==0) {
*p_new_value=old_value;
} else {
// convert string to number
*p_new_value=atoi(inString);
}
Serial.println();
} // readInt
void myDelay(int ms) {
Serial.print("waiting for ");
Serial.print(ms);
Serial.println();
delay(ms);
} // myDelay
void delayAfterFiring(int firing_duration, int delay_duration) {
int d=delay_duration-firing_duration;
if (d>0) {
myDelay(d);
} else {
Serial.println("*** couldn't delay after firing, duration larger than delay");
}
}
void fireRelay(int pin, char *what, int duration) {
Serial.print("firing ");
Serial.print(what);
Serial.println("...");
digitalWrite(pin,HIGH);
myDelay(duration);
digitalWrite(pin,LOW);
}
void readValues() {
// readInt("camera_duration ",camera_duration,&camera_duration);
readInt("delay_before_magnet",delay_before_magnet,&delay_before_magnet);
readInt("delay_after_magnet ",delay_after_magnet,&delay_after_magnet);
readInt("flash_duration ",flash_duration,&flash_duration);
readInt("magnet_duration ",magnet_duration,&magnet_duration);
}
void loop()
{
// Action!
Serial.println("here we go again...");
// read values
readValues();
// wait for Start
waitForEnter();
// fire camera - not activated
// fireRelay(pin_out_camera,"camera",camera_duration);
// wait until magnet
delayAfterFiring(camera_duration,delay_before_magnet);
// fire magnet
fireRelay(pin_out_magnet,"magnet",magnet_duration);
// wait until flash
delayAfterFiring(magnet_duration,delay_after_magnet);
// fire flash
fireRelay(pin_out_flash,"flash",flash_duration);
}