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  
30 #define LAMP1 13 // Callibration Lamp 1
31 #define LAMP2 6 // Callibration Lamp 2
32  
33 int lamps[] = {LAMP1, LAMP2};
34  
2089 kakl 35 #define FW1 7 // [PD7 - red] Slit Wheel 1-st from light
36 #define FW2 8 // [PB0 - yellow] Grism Wheel 2-nd from light
37 #define FW3 3 // [PD3 - blue] Filter Wheel 3-rd from light
2088 kakl 38  
39 int filters[] = {FW1, FW2, FW3};
40  
2089 kakl 41 const int steps = 3500; // change this to fit the number of steps
42 const int sspeed = 10; // max. 15 // stepper motor speed
2088 kakl 43  
44 // initialize the stepper library on pins
45 #define M1 9
46 #define M2 10
47 #define M3 11
48 #define M4 12
49 Stepper myStepper(steps, M1,M2,M3,M4);
50  
51 // DS18S20 Temperature chip
52 OneWire ds(5); // 1-Wire pin
53 byte addr[2][8]; // 2x 1-Wire Address
54  
55 char serInString[100];
56 int serInIndx = 0;
57 int in1, in2;
58  
59 void motor (int arg)
60 {
2089 kakl 61 myStepper.setSpeed(1);
62 myStepper.step(arg * 10);
2088 kakl 63 myStepper.setSpeed(sspeed);
2089 kakl 64 myStepper.step(arg * steps);
2088 kakl 65 digitalWrite(M1, LOW);
66 digitalWrite(M2, LOW);
67 digitalWrite(M3, LOW);
68 digitalWrite(M4, LOW);
69 }
70  
71 int light (int arg)
72 {
73 int LSB = 0, MSB = 0; // data from light
74  
75 // Setup device
76 Wire.beginTransmission(lights[arg]);
77 Wire.send(0x00); // command register
78 Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx])
79 Wire.endTransmission(); // stop transmitting
80  
81 // Delay for measurement, maybe 100ms is enough, maybe not
82 delay(110);
83  
84 // LSB
85 Wire.beginTransmission(lights[arg]);
86 Wire.send(0x01); // sends light0
87 Wire.endTransmission(); // stop transmitting
88 // Connect to device and request one byte
89 Wire.beginTransmission(lights[arg]);
90 Wire.requestFrom(lights[arg], 1);
91 LSB = Wire.receive();
92 Wire.endTransmission();
93  
94 // MSB
95 Wire.beginTransmission(lights[arg]);
96 Wire.send(0x02); // sends light0
97 Wire.endTransmission(); // stop transmitting
98 // Connect to device and request one byte
99 Wire.beginTransmission(lights[arg]);
100 Wire.requestFrom(lights[arg], 1);
101 MSB = Wire.receive();
102 Wire.endTransmission(); // stop transmitting
103  
104 return ((MSB << 8) + LSB);
105 }
106  
107 int temperature (int arg)
108 {
2090 kakl 109 int i, temp;
2088 kakl 110 byte data[12];
111  
112 if (OneWire::crc8 (addr[arg], 7) != addr[arg][7])
113 {
114 Serial.print("CRC is not valid!\n");
115 return 0;
116 }
117  
118 ds.reset();
119 ds.select(addr[arg]);
120 ds.write(0x44, 1); // start conversion, with parasite power on at the end
121  
122 delay(800); // maybe 750ms is enough, maybe not
123  
124 ds.reset();
125 ds.select(addr[arg]);
126 ds.write(0xBE); // Read Scratchpad
127  
128 for ( i = 0; i < 9; i++) // we need 9 bytes
129 {
130 data[i] = ds.read();
131 }
132  
2090 kakl 133 temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature
134 // temp = temp >> 4; //divide by 16 to get pure celcius readout
135  
136 return temp;
137 }
2088 kakl 138  
2090 kakl 139 void inicializace ()
140 {
141 // vysune se motor
142 motor (1);
143 // vsechny vystupy off
144 // digitalWrite(LAMP1, LOW);
145 // digitalWrite(LAMP2, LOW);
146 digitalWrite(FW1, LOW);
147 digitalWrite(FW2, LOW);
148 digitalWrite(FW3, LOW);
2088 kakl 149 }
150  
2090 kakl 151 void setup()
2088 kakl 152 {
2090 kakl 153 // pinMode(LAMP1, OUTPUT);
154 // pinMode(LAMP2, OUTPUT);
155 pinMode(FW1, OUTPUT);
156 pinMode(FW2, OUTPUT);
157 pinMode(FW3, OUTPUT);
158  
159 pinMode(6, OUTPUT);
160 analogWrite(6, 250);
161  
162 // initialize the serial port:
163 Serial.begin(9600);
164  
165 Wire.begin(); // join i2c bus
166  
167 // OneWire
168 ds.reset_search();
169 if (!ds.search(addr[0])) // search for next thermometer
170 {
171 Serial.print ("1st thermometer error.");
172 ds.reset_search();
173 delay(250);
174 return;
175 }
176 if (!ds.search(addr[1])) // search for next thermometer
177 {
178 Serial.print ("2nd thermometer error.");
179 ds.reset_search();
180 delay(250);
181 return;
182 }
183  
184 inicializace ();
2088 kakl 185 }
186  
2090 kakl 187 void telemetrie ()
188 {
189 Serial.print ("FW1=");
190 Serial.print (digitalRead (filters[0]) ? '1' : '0');
191 Serial.print (",FW2=");
192 Serial.print (digitalRead (filters[1]) ? '1' : '0');
193 Serial.print (",FW3=");
194 Serial.print (digitalRead (filters[2]) ? '1' : '0');
195 Serial.print (",T1=");
196 Serial.print (temperature (0) >> 4);
197 Serial.print (".");
198 Serial.print (temperature (0) % 16);
199 Serial.print (",T2=");
200 Serial.print (temperature (1) >> 4);
201 Serial.print (".");
202 Serial.print (temperature (1) % 16);
203 Serial.print (",L1=");
204 Serial.print (light (0));
205 Serial.print (",L2=");
206 Serial.print (light (1));
207 Serial.println ();
208 }
209  
2088 kakl 210 void readSerialString ()
211 {
2089 kakl 212 serInIndx=0;
213 do
2088 kakl 214 {
2089 kakl 215 while(Serial.available()==0);
216 serInString[serInIndx] = Serial.read();
217 Serial.print(serInString[serInIndx]);
218 serInIndx++;
219 } while ((serInString[serInIndx-1]!='\n')&&(serInString[serInIndx-1]!='.'));
220 Serial.print("\r\n=");
2088 kakl 221 }
222  
223 void loop()
224 {
2090 kakl 225 // readSerialString();
2088 kakl 226  
2090 kakl 227 if (Serial.available())
228 {
229 switch (Serial.read())
2088 kakl 230 {
2090 kakl 231 case 'i': // inicializace
232 inicializace ();
233 telemetrie ();
2088 kakl 234 break;
2090 kakl 235 case 'm':
236 motor (-1);
237 telemetrie ();
2088 kakl 238 break;
2090 kakl 239 case 'M':
240 motor (1); // vysunuto
241 telemetrie ();
2088 kakl 242 break;
2090 kakl 243 case 'A':
244 digitalWrite(FW1, HIGH);
245 telemetrie ();
2088 kakl 246 break;
2090 kakl 247 case 'a':
248 digitalWrite(FW1, LOW);
249 telemetrie ();
2088 kakl 250 break;
2090 kakl 251 case 'B':
252 digitalWrite(FW2, HIGH);
253 telemetrie ();
2088 kakl 254 break;
2090 kakl 255 case 'b':
256 digitalWrite(FW2, LOW);
257 telemetrie ();
258 break;
259 case 'C':
260 digitalWrite(FW3, HIGH);
261 telemetrie ();
262 break;
263 case 'c':
264 digitalWrite(FW3, LOW);
265 telemetrie ();
266 break;
267 default:
268 break;
2088 kakl 269 }
2090 kakl 270 Serial.flush ();
271 // /for (serInIndx = 100; serInIndx > 0; serInIndx--)
272 // serInString[serInIndx] = ' ';
2088 kakl 273 }
274  
2090 kakl 275 // delay(100);
276 }