Rev Author Line No. Line
2088 kakl 1 #include <OneWire.h>
2 #include <Stepper.h>
3 #include <Wire.h>
4  
5 #define light0 0x44 // A0 = L (I2C light0)
6 #define light1 0x45 // A0 = H (I2C light1)
7  
8 int lights[] = {light0, light1};
9  
10 #define LAMP1 13 // Callibration Lamp 1
11 #define LAMP2 6 // Callibration Lamp 2
12  
13 int lamps[] = {LAMP1, LAMP2};
14  
15 #define FW1 7 // FilterWheel 1
16 #define FW2 8 // FilterWheel 1
17 #define FW3 3 // FilterWheel 1
18  
19 int filters[] = {FW1, FW2, FW3};
20  
21 int motors[] = {-1, 1};
22  
23 const int steps = 200; //3200; // change this to fit the number of steps
24 const int sspeed = 100; // stepper motor speed
25  
26 // initialize the stepper library on pins
27 #define M1 9
28 #define M2 10
29 #define M3 11
30 #define M4 12
31 Stepper myStepper(steps, M1,M2,M3,M4);
32  
33 // DS18S20 Temperature chip
34 OneWire ds(5); // 1-Wire pin
35 byte addr[2][8]; // 2x 1-Wire Address
36  
37 char serInString[100];
38 int serInIndx = 0;
39 int in1, in2;
40  
41 void setup()
42 {
43 pinMode(LAMP1, OUTPUT);
44 pinMode(LAMP2, OUTPUT);
45 pinMode(FW1, OUTPUT);
46 pinMode(FW2, OUTPUT);
47 pinMode(FW3, OUTPUT);
48  
49 digitalWrite(LAMP1, HIGH); // All outputs OFF
50 digitalWrite(LAMP2, HIGH);
51 digitalWrite(FW1, HIGH);
52 digitalWrite(FW2, HIGH);
53 digitalWrite(FW3, HIGH);
54  
55 // initialize the serial port:
56 Serial.begin(9600);
57  
58 Wire.begin(); // join i2c bus
59  
60 // OneWire
61 ds.reset_search();
62 if (!ds.search(addr[0])) // search for next thermometer
63 {
64 Serial.print ("1st thermometer error.");
65 ds.reset_search();
66 delay(250);
67 return;
68 }
69 if (!ds.search(addr[1])) // search for next thermometer
70 {
71 Serial.print ("2nd thermometer error.");
72 ds.reset_search();
73 delay(250);
74 return;
75 }
76 }
77  
78 void motor (int arg)
79 {
80 myStepper.setSpeed(sspeed/2);
81 myStepper.step(arg * 30);
82 myStepper.setSpeed(sspeed);
83 myStepper.step(arg * (steps-50));
84 myStepper.setSpeed(sspeed/2);
85 myStepper.step(arg * 20);
86 delay(50);
87 digitalWrite(M1, LOW);
88 digitalWrite(M2, LOW);
89 digitalWrite(M3, LOW);
90 digitalWrite(M4, LOW);
91 }
92  
93 int light (int arg)
94 {
95 int LSB = 0, MSB = 0; // data from light
96  
97 // Setup device
98 Wire.beginTransmission(lights[arg]);
99 Wire.send(0x00); // command register
100 Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx])
101 Wire.endTransmission(); // stop transmitting
102  
103 // Delay for measurement, maybe 100ms is enough, maybe not
104 delay(110);
105  
106 // LSB
107 Wire.beginTransmission(lights[arg]);
108 Wire.send(0x01); // sends light0
109 Wire.endTransmission(); // stop transmitting
110 // Connect to device and request one byte
111 Wire.beginTransmission(lights[arg]);
112 Wire.requestFrom(lights[arg], 1);
113 LSB = Wire.receive();
114 Wire.endTransmission();
115  
116 // MSB
117 Wire.beginTransmission(lights[arg]);
118 Wire.send(0x02); // sends light0
119 Wire.endTransmission(); // stop transmitting
120 // Connect to device and request one byte
121 Wire.beginTransmission(lights[arg]);
122 Wire.requestFrom(lights[arg], 1);
123 MSB = Wire.receive();
124 Wire.endTransmission(); // stop transmitting
125  
126 return ((MSB << 8) + LSB);
127 }
128  
129 int temperature (int arg)
130 {
131 int i, Temp;
132 byte data[12];
133  
134 if (OneWire::crc8 (addr[arg], 7) != addr[arg][7])
135 {
136 Serial.print("CRC is not valid!\n");
137 return 0;
138 }
139  
140 ds.reset();
141 ds.select(addr[arg]);
142 ds.write(0x44, 1); // start conversion, with parasite power on at the end
143  
144 delay(800); // maybe 750ms is enough, maybe not
145  
146 ds.reset();
147 ds.select(addr[arg]);
148 ds.write(0xBE); // Read Scratchpad
149  
150 for ( i = 0; i < 9; i++) // we need 9 bytes
151 {
152 data[i] = ds.read();
153 }
154  
155 Temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature
156 Temp = Temp >> 4; //divide by 16 to get pure celcius readout
157  
158 return Temp;
159 }
160  
161 void accelerometer ()
162 {
163 Serial.print("X=");
164 Serial.print(analogRead(A0)-512, DEC);
165 Serial.print(" Y=");
166 Serial.print(analogRead(A1)-512, DEC);
167 Serial.print(" Z=");
168 Serial.println(analogRead(A2)-512, DEC);
169 }
170  
171 void readSerialString ()
172 {
173 if(Serial.available())
174 {
175 while (Serial.available())
176 {
177 serInString[serInIndx] = Serial.read();
178 serInIndx++;
179 }
180 }
181 }
182  
183 void loop()
184 {
185 readSerialString();
186  
187 if( serInIndx > 0)
188 {
189 in1 = serInString[1] - '0';
190  
191 switch (serInString[0])
192 {
193 case '?':
194 Serial.println ("Device queries:");
195 Serial.println (" l[0,1] light in luxes");
196 Serial.println (" t[0,1] temperature in Celsius degrees");
197 Serial.println (" F[0,1][0|1] switch filter wheel on (1) or off (0)");
198 Serial.println (" F[0,1]? check state of filter wheel");
199 Serial.println (" L[0,1][0|1] switch calibration lamp on (1) or off (0)");
200 Serial.println (" L[0,1]? check state of calibration lamp");
201 Serial.println (" M[0|1] motor rotation clockwise (1) or counterclockwise (0)");
202 break;
203 case 't':
204 Serial.println (temperature (in1));
205 break;
206 case 'l':
207 Serial.println (light (in1));
208 break;
209 case 'L':
210 if (serInString[2] == '?')
211 {
212 Serial.println (digitalRead (lamps[in1]) ? '0' : '1');
213 }
214 else
215 {
216 in2 = serInString[2] - '0';
217 digitalWrite(lamps[in1], in2 ? LOW : HIGH);
218 }
219 break;
220 case 'F':
221 if (serInString[2] == '?')
222 {
223 Serial.println (digitalRead (filters[in1]) ? '0' : '1');
224 }
225 else
226 {
227 in2 = serInString[2] - '0';
228 digitalWrite(filters[in1], in2 ? LOW : HIGH);
229 }
230 break;
231 case 'M':
232 motor (motors[in1]);
233 break;
234 }
235 for (serInIndx = 100; serInIndx > 0; serInIndx--)
236 serInString[serInIndx] = ' ';
237  
238 Serial.flush ();
239 }
240  
241 delay(100);
242 }
243  
244  
245