3669 |
kakl |
1 |
#include <OneWire.h> |
2527 |
kaklik |
2 |
#include <Stepper.h> |
|
|
3 |
#include <Wire.h> |
|
|
4 |
|
|
|
5 |
/* |
|
|
6 |
|
|
|
7 |
protokol: |
|
|
8 |
|
|
|
9 |
M - motor na jednu stranu |
|
|
10 |
m - motor na druhou stranu |
|
|
11 |
-- |
|
|
12 |
a - vypni FW1 |
|
|
13 |
A - zapni FW1 |
|
|
14 |
-- |
|
|
15 |
b - vypni FW2 |
|
|
16 |
B - zapni FW2 |
|
|
17 |
-- |
|
|
18 |
c - vypni FW3 |
|
|
19 |
C - zapni FW3 |
|
|
20 |
-- |
|
|
21 |
i - inicializace: vypni vsechny FW, vytahni motor |
|
|
22 |
|
|
|
23 |
*/ |
|
|
24 |
|
|
|
25 |
#define light0 0x44 // A0 = L (I2C light0) |
|
|
26 |
#define light1 0x45 // A0 = H (I2C light1) |
|
|
27 |
|
|
|
28 |
int lights[] = {light0, light1}; |
|
|
29 |
|
|
|
30 |
//#define LAMP1 13 // Callibration Lamp 1 |
|
|
31 |
//#define LAMP2 6 // Callibration Lamp 2 |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
#define FW1 7 // [PD7 - red] Slit Wheel 1-st from light |
|
|
35 |
#define FW2 8 // [PB0 - yellow] Grism Wheel 2-nd from light |
|
|
36 |
#define FW3 3 // [PD3 - blue] Filter Wheel 3-rd from light |
|
|
37 |
|
|
|
38 |
int filters[] = {FW1, FW2, FW3}; |
|
|
39 |
|
|
|
40 |
const int STEPS = 3500; // change this to fit the number of steps |
|
|
41 |
const int SSPEED = 8000; // max. 15 // stepper motor speed |
|
|
42 |
|
|
|
43 |
// initialize the stepper library on pins |
|
|
44 |
#define M1 9 |
|
|
45 |
#define M2 10 |
|
|
46 |
#define M3 11 |
|
|
47 |
#define M4 12 |
|
|
48 |
Stepper myStepper(200, M1,M2,M3,M4); |
|
|
49 |
|
|
|
50 |
// DS18S20 Temperature chip |
|
|
51 |
OneWire ds(5); // 1-Wire pin |
|
|
52 |
byte addr[2][8]; // 2x 1-Wire Address |
|
|
53 |
|
|
|
54 |
char deleni16[16]={'0','1','1','2','3','3','4','4','5','6','6','7','7','8','9','9'}; |
|
|
55 |
|
|
|
56 |
char serInString[100]; |
|
|
57 |
int serInIndx = 0; |
|
|
58 |
int in1, in2; |
|
|
59 |
|
|
|
60 |
void motor (word arg) |
|
|
61 |
{ |
|
|
62 |
word n; |
|
|
63 |
word s=SSPEED; |
|
|
64 |
|
|
|
65 |
/* |
|
|
66 |
for(n=0;n<2500000/SSPEED;n++) |
|
|
67 |
{ |
|
|
68 |
digitalWrite(M1, LOW); |
|
|
69 |
digitalWrite(M2, HIGH); |
|
|
70 |
digitalWrite(M1, LOW); |
|
|
71 |
digitalWrite(M2, HIGH); |
|
|
72 |
delayMicroseconds(SSPEED); |
|
|
73 |
digitalWrite(M1, HIGH); |
|
|
74 |
digitalWrite(M2, LOW); |
|
|
75 |
digitalWrite(M1, HIGH); |
|
|
76 |
digitalWrite(M2, LOW); |
|
|
77 |
delayMicroseconds(SSPEED); |
|
|
78 |
} |
|
|
79 |
*/ |
|
|
80 |
|
|
|
81 |
if(arg==-1) |
|
|
82 |
{ |
|
|
83 |
for(n=0;n<STEPS/4;n++) |
|
|
84 |
{ |
|
|
85 |
digitalWrite(M1, LOW); |
|
|
86 |
digitalWrite(M2, HIGH); |
|
|
87 |
// digitalWrite(M3, LOW); |
|
|
88 |
// digitalWrite(M4, HIGH); |
|
|
89 |
delayMicroseconds(s); |
|
|
90 |
// digitalWrite(M1, LOW); |
|
|
91 |
// digitalWrite(M2, HIGH); |
|
|
92 |
digitalWrite(M3, HIGH); |
|
|
93 |
digitalWrite(M4, LOW); |
|
|
94 |
delayMicroseconds(s); |
|
|
95 |
digitalWrite(M1, HIGH); |
|
|
96 |
digitalWrite(M2, LOW); |
|
|
97 |
// digitalWrite(M3, HIGH); |
|
|
98 |
// digitalWrite(M4, LOW); |
|
|
99 |
delayMicroseconds(s); |
|
|
100 |
// digitalWrite(M1, HIGH); |
|
|
101 |
// digitalWrite(M2, LOW); |
|
|
102 |
digitalWrite(M3, LOW); |
|
|
103 |
digitalWrite(M4, HIGH); |
|
|
104 |
delayMicroseconds(s); |
|
|
105 |
if(s>1500)s-=50; |
|
|
106 |
} |
|
|
107 |
} |
|
|
108 |
else |
|
|
109 |
{ |
|
|
110 |
for(n=0;n<STEPS/4;n++) |
|
|
111 |
{ |
|
|
112 |
// digitalWrite(M1, HIGH); |
|
|
113 |
// digitalWrite(M2, LOW); |
|
|
114 |
digitalWrite(M3, LOW); |
|
|
115 |
digitalWrite(M4, HIGH); |
|
|
116 |
delayMicroseconds(s); |
|
|
117 |
digitalWrite(M1, HIGH); |
|
|
118 |
digitalWrite(M2, LOW); |
|
|
119 |
// digitalWrite(M3, HIGH); |
|
|
120 |
// digitalWrite(M4, LOW); |
|
|
121 |
delayMicroseconds(s); |
|
|
122 |
// digitalWrite(M1, LOW); |
|
|
123 |
// digitalWrite(M2, HIGH); |
|
|
124 |
digitalWrite(M3, HIGH); |
|
|
125 |
digitalWrite(M4, LOW); |
|
|
126 |
delayMicroseconds(s); |
|
|
127 |
digitalWrite(M1, LOW); |
|
|
128 |
digitalWrite(M2, HIGH); |
|
|
129 |
// digitalWrite(M3, LOW); |
|
|
130 |
// digitalWrite(M4, HIGH); |
|
|
131 |
delayMicroseconds(s); |
|
|
132 |
if(s>1500)s-=50; |
|
|
133 |
} |
|
|
134 |
} |
|
|
135 |
/* |
|
|
136 |
myStepper.setSpeed(1); |
|
|
137 |
myStepper.step(arg * 10); |
|
|
138 |
myStepper.setSpeed(sspeed); |
|
|
139 |
myStepper.step(arg * steps); |
|
|
140 |
*/ |
|
|
141 |
digitalWrite(M1, LOW); |
|
|
142 |
digitalWrite(M2, LOW); |
|
|
143 |
digitalWrite(M3, LOW); |
|
|
144 |
digitalWrite(M4, LOW); |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
int light (int arg) |
|
|
148 |
{ |
|
|
149 |
int LSB = 0, MSB = 0; // data from light |
|
|
150 |
|
|
|
151 |
// Setup device |
|
|
152 |
Wire.beginTransmission(lights[arg]); |
|
|
153 |
Wire.write(byte(0x00)); // command register |
|
|
154 |
Wire.write(byte(0b11000001)); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
155 |
Wire.endTransmission(); // stop transmitting |
|
|
156 |
|
|
|
157 |
// Delay for measurement, maybe 100ms is enough, maybe not |
|
|
158 |
delay(110); |
|
|
159 |
|
|
|
160 |
// LSB |
|
|
161 |
Wire.beginTransmission(lights[arg]); |
|
|
162 |
Wire.write(byte(0x01)); // sends light0 |
|
|
163 |
Wire.endTransmission(); // stop transmitting |
|
|
164 |
// Connect to device and request one byte |
|
|
165 |
Wire.beginTransmission(lights[arg]); |
|
|
166 |
Wire.requestFrom(lights[arg], 1); |
|
|
167 |
LSB = Wire.read(); |
|
|
168 |
Wire.endTransmission(); |
|
|
169 |
|
|
|
170 |
// MSB |
|
|
171 |
Wire.beginTransmission(lights[arg]); |
|
|
172 |
Wire.write(byte(0x02)); // sends light0 |
|
|
173 |
Wire.endTransmission(); // stop transmitting |
|
|
174 |
// Connect to device and request one byte |
|
|
175 |
Wire.beginTransmission(lights[arg]); |
|
|
176 |
Wire.requestFrom(lights[arg], 1); |
|
|
177 |
MSB = Wire.read(); |
|
|
178 |
Wire.endTransmission(); // stop transmitting |
|
|
179 |
|
|
|
180 |
return ((MSB << 8) + LSB); |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
int temperature (int arg) |
|
|
184 |
{ |
|
|
185 |
int i, temp; |
|
|
186 |
byte data[12]; |
|
|
187 |
|
|
|
188 |
if (OneWire::crc8 (addr[arg], 7) != addr[arg][7]) |
|
|
189 |
{ |
|
|
190 |
Serial.print("CRC is not valid!\n"); |
|
|
191 |
return 0; |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
ds.reset(); |
|
|
195 |
ds.select(addr[arg]); |
|
|
196 |
ds.write(0x44, 1); // start conversion, with parasite power on at the end |
|
|
197 |
|
|
|
198 |
delay(800); // maybe 750ms is enough, maybe not |
|
|
199 |
|
|
|
200 |
ds.reset(); |
|
|
201 |
ds.select(addr[arg]); |
|
|
202 |
ds.write(0xBE); // Read Scratchpad |
|
|
203 |
|
|
|
204 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
|
|
205 |
{ |
|
|
206 |
data[i] = ds.read(); |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature |
|
|
210 |
// temp = temp >> 4; //divide by 16 to get pure celcius readout |
|
|
211 |
|
|
|
212 |
return temp; |
|
|
213 |
} |
|
|
214 |
|
|
|
215 |
void inicializace () |
|
|
216 |
{ |
|
|
217 |
// vysune se motor |
|
|
218 |
motor (1); |
|
|
219 |
// vsechny vystupy off |
|
|
220 |
// digitalWrite(LAMP1, LOW); |
|
|
221 |
// digitalWrite(LAMP2, LOW); |
|
|
222 |
digitalWrite(FW1, LOW); |
|
|
223 |
digitalWrite(FW2, LOW); |
|
|
224 |
digitalWrite(FW3, LOW); |
|
|
225 |
} |
|
|
226 |
|
|
|
227 |
void setup() |
|
|
228 |
{ |
|
|
229 |
// pinMode(LAMP1, OUTPUT); |
|
|
230 |
// pinMode(LAMP2, OUTPUT); |
|
|
231 |
pinMode(FW1, OUTPUT); |
|
|
232 |
pinMode(FW2, OUTPUT); |
|
|
233 |
pinMode(FW3, OUTPUT); |
|
|
234 |
|
|
|
235 |
pinMode(6, OUTPUT); |
|
|
236 |
//analogWrite(6, 254); |
|
|
237 |
digitalWrite(6, HIGH); |
|
|
238 |
|
|
|
239 |
// initialize the serial port: |
|
|
240 |
Serial.begin(9600); |
|
|
241 |
|
|
|
242 |
Wire.begin(); // join i2c bus |
|
|
243 |
|
|
|
244 |
// OneWire |
|
|
245 |
ds.reset_search(); |
|
|
246 |
if (!ds.search(addr[0])) // search for next thermometer |
|
|
247 |
{ |
|
|
248 |
Serial.print ("1st thermometer error."); |
|
|
249 |
ds.reset_search(); |
|
|
250 |
delay(250); |
|
|
251 |
return; |
|
|
252 |
} |
|
|
253 |
if (!ds.search(addr[1])) // search for next thermometer |
|
|
254 |
{ |
|
|
255 |
Serial.print ("2nd thermometer error."); |
|
|
256 |
ds.reset_search(); |
|
|
257 |
delay(250); |
|
|
258 |
return; |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
inicializace (); |
|
|
262 |
} |
|
|
263 |
|
|
|
264 |
void telemetrie () |
|
|
265 |
{ |
|
|
266 |
int t; |
|
|
267 |
Serial.print ("FW1="); |
|
|
268 |
Serial.print (digitalRead (filters[0]) ? '1' : '0'); |
|
|
269 |
Serial.print (",FW2="); |
|
|
270 |
Serial.print (digitalRead (filters[1]) ? '1' : '0'); |
|
|
271 |
Serial.print (",FW3="); |
|
|
272 |
Serial.print (digitalRead (filters[2]) ? '1' : '0'); |
|
|
273 |
Serial.print (",T1="); |
|
|
274 |
t=temperature(0); |
|
|
275 |
Serial.print (t >> 4); |
|
|
276 |
Serial.print ("."); |
|
|
277 |
Serial.print (deleni16[t & 0xf]); |
|
|
278 |
Serial.print (",T2="); |
|
|
279 |
t=temperature(1); |
|
|
280 |
Serial.print (t >> 4); |
|
|
281 |
Serial.print ("."); |
|
|
282 |
Serial.print (deleni16[t & 0xf]); |
|
|
283 |
Serial.print (",L1="); |
|
|
284 |
Serial.print (light (0)); |
|
|
285 |
Serial.print (",L2="); |
|
|
286 |
Serial.print (light (1)); |
|
|
287 |
Serial.println (); |
|
|
288 |
} |
|
|
289 |
|
|
|
290 |
void readSerialString () |
|
|
291 |
{ |
|
|
292 |
serInIndx=0; |
|
|
293 |
do |
|
|
294 |
{ |
|
|
295 |
while(Serial.available()==0); |
|
|
296 |
serInString[serInIndx] = Serial.read(); |
|
|
297 |
Serial.print(serInString[serInIndx]); |
|
|
298 |
serInIndx++; |
|
|
299 |
} while ((serInString[serInIndx-1]!='\n')&&(serInString[serInIndx-1]!='.')); |
|
|
300 |
Serial.print("\r\n="); |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
void loop() |
|
|
304 |
{ |
|
|
305 |
// readSerialString(); |
|
|
306 |
|
|
|
307 |
if (Serial.available()) |
3669 |
kakl |
308 |
{ |
2527 |
kaklik |
309 |
switch (Serial.read()) |
|
|
310 |
{ |
|
|
311 |
case 'i': // inicializace |
|
|
312 |
inicializace (); |
|
|
313 |
break; |
|
|
314 |
case 'm': |
|
|
315 |
motor (-1); |
|
|
316 |
break; |
|
|
317 |
case 'M': |
|
|
318 |
motor (1); // vysunuto |
|
|
319 |
break; |
|
|
320 |
|
|
|
321 |
case 'x': |
|
|
322 |
myStepper.setSpeed(100); |
|
|
323 |
myStepper.step(3000); |
|
|
324 |
break; |
|
|
325 |
case 'y': |
|
|
326 |
myStepper.setSpeed(10.0); |
|
|
327 |
myStepper.step(-3000); |
|
|
328 |
break; |
|
|
329 |
|
|
|
330 |
case 'A': |
|
|
331 |
digitalWrite(FW1, HIGH); |
|
|
332 |
break; |
|
|
333 |
case 'a': |
|
|
334 |
digitalWrite(FW1, LOW); |
|
|
335 |
break; |
|
|
336 |
case 'B': |
|
|
337 |
digitalWrite(FW2, HIGH); |
|
|
338 |
break; |
|
|
339 |
case 'b': |
|
|
340 |
digitalWrite(FW2, LOW); |
|
|
341 |
break; |
|
|
342 |
case 'C': |
|
|
343 |
digitalWrite(FW3, HIGH); |
|
|
344 |
break; |
|
|
345 |
case 'c': |
|
|
346 |
digitalWrite(FW3, LOW); |
|
|
347 |
break; |
|
|
348 |
} |
|
|
349 |
telemetrie (); |
|
|
350 |
Serial.flush (); |
|
|
351 |
// /for (serInIndx = 100; serInIndx > 0; serInIndx--) |
|
|
352 |
// serInString[serInIndx] = ' '; |
|
|
353 |
} |
|
|
354 |
|
|
|
355 |
// delay(100); |
|
|
356 |
} |