Rev Author Line No. Line
2088 kakl 1 #include <OneWire.h>
2 #include <Stepper.h>
3 #include <Wire.h>
4  
2090 kakl 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  
2088 kakl 25 #define light0 0x44 // A0 = L (I2C light0)
26 #define light1 0x45 // A0 = H (I2C light1)
27  
28 int lights[] = {light0, light1};
29  
2092 kakl 30 //#define LAMP1 13 // Callibration Lamp 1
31 //#define LAMP2 6 // Callibration Lamp 2
2088 kakl 32  
33  
2089 kakl 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
2088 kakl 37  
38 int filters[] = {FW1, FW2, FW3};
39  
2092 kakl 40 const int STEPS = 3500; // change this to fit the number of steps
41 const int SSPEED = 8000; // max. 15 // stepper motor speed
2088 kakl 42  
43 // initialize the stepper library on pins
44 #define M1 9
45 #define M2 10
46 #define M3 11
47 #define M4 12
2092 kakl 48 Stepper myStepper(200, M1,M2,M3,M4);
2088 kakl 49  
50 // DS18S20 Temperature chip
51 OneWire ds(5); // 1-Wire pin
52 byte addr[2][8]; // 2x 1-Wire Address
53  
2092 kakl 54 char deleni16[16]={'0','1','1','2','3','3','4','4','5','6','6','7','7','8','9','9'};
55  
2088 kakl 56 char serInString[100];
57 int serInIndx = 0;
58 int in1, in2;
59  
2092 kakl 60 void motor (word arg)
2088 kakl 61 {
2092 kakl 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);
2093 kakl 105 if(s>1500)s-=50;
2092 kakl 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);
2093 kakl 132 if(s>1500)s-=50;
2092 kakl 133 }
134 }
135 /*
2089 kakl 136 myStepper.setSpeed(1);
137 myStepper.step(arg * 10);
2088 kakl 138 myStepper.setSpeed(sspeed);
2089 kakl 139 myStepper.step(arg * steps);
2092 kakl 140 */
2088 kakl 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.send(0x00); // command register
154 Wire.send(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.send(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.receive();
168 Wire.endTransmission();
169  
170 // MSB
171 Wire.beginTransmission(lights[arg]);
172 Wire.send(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.receive();
178 Wire.endTransmission(); // stop transmitting
179  
180 return ((MSB << 8) + LSB);
181 }
182  
183 int temperature (int arg)
184 {
2090 kakl 185 int i, temp;
2088 kakl 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  
2090 kakl 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 }
2088 kakl 214  
2090 kakl 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);
2088 kakl 225 }
226  
2090 kakl 227 void setup()
2088 kakl 228 {
2090 kakl 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);
2092 kakl 236 //analogWrite(6, 254);
237 digitalWrite(6, HIGH);
2090 kakl 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 ();
2088 kakl 262 }
263  
2090 kakl 264 void telemetrie ()
265 {
2092 kakl 266 int t;
2090 kakl 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=");
2092 kakl 274 t=temperature(0);
275 Serial.print (t >> 4);
2090 kakl 276 Serial.print (".");
2092 kakl 277 Serial.print (deleni16[t & 0xf]);
2090 kakl 278 Serial.print (",T2=");
2092 kakl 279 t=temperature(1);
280 Serial.print (t >> 4);
2090 kakl 281 Serial.print (".");
2092 kakl 282 Serial.print (deleni16[t & 0xf]);
2090 kakl 283 Serial.print (",L1=");
284 Serial.print (light (0));
285 Serial.print (",L2=");
286 Serial.print (light (1));
287 Serial.println ();
288 }
289  
2088 kakl 290 void readSerialString ()
291 {
2089 kakl 292 serInIndx=0;
293 do
2088 kakl 294 {
2089 kakl 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=");
2088 kakl 301 }
302  
303 void loop()
304 {
2090 kakl 305 // readSerialString();
2088 kakl 306  
2090 kakl 307 if (Serial.available())
308 {
309 switch (Serial.read())
2088 kakl 310 {
2090 kakl 311 case 'i': // inicializace
312 inicializace ();
2088 kakl 313 break;
2090 kakl 314 case 'm':
315 motor (-1);
2088 kakl 316 break;
2090 kakl 317 case 'M':
318 motor (1); // vysunuto
2088 kakl 319 break;
2092 kakl 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  
2090 kakl 330 case 'A':
331 digitalWrite(FW1, HIGH);
2088 kakl 332 break;
2090 kakl 333 case 'a':
334 digitalWrite(FW1, LOW);
2088 kakl 335 break;
2090 kakl 336 case 'B':
337 digitalWrite(FW2, HIGH);
2088 kakl 338 break;
2090 kakl 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;
2088 kakl 348 }
2091 kakl 349 telemetrie ();
2090 kakl 350 Serial.flush ();
351 // /for (serInIndx = 100; serInIndx > 0; serInIndx--)
352 // serInString[serInIndx] = ' ';
2088 kakl 353 }
354  
2090 kakl 355 // delay(100);
356 }