-
Notifications
You must be signed in to change notification settings - Fork 10
/
Follow_Focus_Sender_22_1_13.ino
362 lines (297 loc) · 7.26 KB
/
Follow_Focus_Sender_22_1_13.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Movement of Optical encoder is translated to moving stepper
// THIS IS A WORKING VERSION 22.1.13
// Adi Soffer 2013 //
// for more info visit //
// http://adisoffer.tumblr.com //
#include <OneButton.h>
//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int lastEncoded = 0;
int encoderValue = 0;
int lastencoderValue = 0;
int lastMSB = LOW;
int lastLSB = LOW;
//LEDs
#define realTimeLED 12 //Real Time LED
#define playLED 9 //PLay LED
#define inLED 11 //In LED
#define outLED 10 //Out LED
// Setup OneButton
OneButton realTimebutton(A3, true);
OneButton playButton (A1, true);
OneButton inButton (A5, true);
OneButton outButton (A4, true);
//Values for focus points
volatile int inPoint = 0;
volatile int outPoint = 3000;
//Blink without delay
boolean ledState = LOW;
long previousMillis = 0;
int ledInterval = 75;
//Defining booleans
boolean rClickedOnce = false;
boolean rLongPress = false;
boolean pClickedOnce = false;
boolean highEndMark = false;
boolean lowEndMark = false;
boolean startUp = false;
//Value for sendFunction - to send over Xbee
byte value;
//value for variable speed - to send over Xbee
int valToRemap;
int encoderValToRemap;
//Value to recieve from 2nd Xbee: when "play" or "rewind" has finished"
int dataReceive;
//Modes
int mode;
#define Stop 1
#define LENSCALIB 2
void setup(){
Serial.begin(115200);
//Pinmodes
pinMode (realTimeLED, OUTPUT);
pinMode (playLED, OUTPUT);
pinMode (inLED, OUTPUT);
pinMode (outLED, OUTPUT);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
//Attach Click to Buttons
realTimebutton.attachClick(Click);
playButton.attachClick(ClickPlay);
inButton.attachClick(ClickIn);
outButton.attachClick(ClickOut);
//Attach Press to Real Time for calibrating lens and motor
realTimebutton.attachPress(rPress);
//StartUp light
}
void loop(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
//only if changed value
if(lastMSB != MSB || lastLSB != LSB){
updateEncoder(MSB, LSB);
}
if (startUp == false)
{
for (int x=0;x<3;x++)
{
for (int l=9;l<13;l++)
{
digitalWrite(l,HIGH);
delay (100);
digitalWrite(l,LOW);
delay (100);
}
}
startUp = true;
}
// keep watching the push buttons:
realTimebutton.tick();
playButton.tick();
inButton.tick();
outButton.tick();
// filter signal from stepper motor
//arduino when(play/rewind) is done.
if (Serial.available())
{
dataReceive = Serial.read();
if (dataReceive == 1)
{
pClickedOnce = false;
rClickedOnce = false;
digitalWrite(playLED, LOW);
}
else if (dataReceive == 2)
blinkMark (inLED);
else if (dataReceive == 3)
blinkMark (outLED);
}
switch (mode)
{
case LENSCALIB:
{
blinkFunction (realTimeLED);
if (lowEndMark == true && highEndMark == true ||rClickedOnce == true || pClickedOnce == true)
{
rLongPress = false;
digitalWrite (realTimeLED, LOW);
if (rClickedOnce == true)
digitalWrite(realTimeLED, HIGH);
if (pClickedOnce == true)
digitalWrite (playLED, HIGH);
mode = Stop;
break;
}
}
}
}
void updateEncoder(int MSB, int LSB){
lastMSB = MSB;
lastLSB = LSB;
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
{
encoderValue ++;
// This is a function making use of the data
// in 2 different ways: 1/as encoder value
// 2/as speed value for play/rewind
// depending on state of switches.
sendFunction (1);
}
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
{
encoderValue --;
// This is a function makinf use of the data
// in 2 different ways: 1/as encoder value
// 2/as speed value for play/rewind
// depending on state of switches.
sendFunction(2);
}
lastEncoded = encoded; //store this value for next time
}
//4 Buttons Click Functions
//Real Time Switch
void Click() {
digitalWrite(playLED, LOW);
rLongPress = false;
if (rClickedOnce == false )
{
rClickedOnce = true;
digitalWrite(realTimeLED, HIGH);
Serial.write (6);
}
else
{
digitalWrite(realTimeLED, LOW);
rClickedOnce = false;
}
}
// Press Function - calibrating lens and stepper
void rPress() {
if (rLongPress == false) {
rLongPress = true;
rClickedOnce = false;
highEndMark = false;
lowEndMark = false;
Serial.write (9);
mode = LENSCALIB;
}
else
{
rLongPress = false;
digitalWrite(realTimeLED, LOW);
mode = Stop;
}
}
//Play Switch
void ClickPlay () {
//terminate realtime in case it wasn't stopped
if (pClickedOnce == false)
{
pClickedOnce = true;
rClickedOnce = false;
rLongPress = false;
digitalWrite(realTimeLED, LOW);
digitalWrite(playLED, HIGH);
Serial.write(5);
//Serial.print("pressed");
}
else
{
pClickedOnce = false;
digitalWrite(playLED, LOW);
}
}
//In Switch
void ClickIn () {
// Saving In
if (rClickedOnce == true)
{
inPoint = encoderValue;
Serial.write (3);
}
else if (rLongPress == true)
{
lowEndMark = true;
Serial.write (7);
}
}
//Out Switch
void ClickOut() {
//Saving Out
if (rClickedOnce == true)
{
Serial.write (4);
outPoint = encoderValue;
}
else if (rLongPress == true)
{
highEndMark = true;
Serial.write (8);
}
}
int blinkFunction (int y) // Blink without delay
{
unsigned long currentMillis = millis ();
if (currentMillis - previousMillis>ledInterval)
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite (y, ledState);
}
}
int blinkMark (int y)
{
boolean b = HIGH;
for (int i=0;i<6;i++)
{
digitalWrite(y, b);
delay(75);
b=!b;
}
}
int sendFunction(byte value)
{
// 1/Send encoder value to move stepper
if (rClickedOnce == true||rLongPress == true)
Serial.write (value);
else if (pClickedOnce == false)
{
// 2/Send remapped value to change speed of stepper
encoderValToRemap = encoderValue;
if (inPoint<outPoint)
{
if (encoderValToRemap< inPoint)
encoderValToRemap = inPoint;
if (encoderValToRemap>outPoint)
encoderValToRemap = outPoint;
}
else
{
if (encoderValToRemap>inPoint)
encoderValToRemap = inPoint;
if (encoderValToRemap<outPoint)
encoderValToRemap = outPoint;
}
//remap value to ba able to send bytes
valToRemap = focusSpeed(encoderValToRemap);
}
}
int focusSpeed (int valToRemap)
{
//remapped value sent to "receiving" arduino to change stepper speed
//during play/rewind.
valToRemap = map(valToRemap, inPoint, outPoint, 10, 30);
if (valToRemap>30)
valToRemap = 30;
if (valToRemap<10)
valToRemap = 10;
Serial.write (valToRemap);
}