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 |
|
2089 |
kakl |
15 |
#define FW1 7 // [PD7 - red] Slit Wheel 1-st from light |
|
|
16 |
#define FW2 8 // [PB0 - yellow] Grism Wheel 2-nd from light |
|
|
17 |
#define FW3 3 // [PD3 - blue] Filter Wheel 3-rd from light |
2088 |
kakl |
18 |
|
|
|
19 |
int filters[] = {FW1, FW2, FW3}; |
|
|
20 |
|
|
|
21 |
int motors[] = {-1, 1}; |
|
|
22 |
|
2089 |
kakl |
23 |
const int steps = 3500; // change this to fit the number of steps |
|
|
24 |
const int sspeed = 10; // max. 15 // stepper motor speed |
2088 |
kakl |
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 |
{ |
2089 |
kakl |
80 |
myStepper.setSpeed(1); |
|
|
81 |
myStepper.step(arg * 10); |
2088 |
kakl |
82 |
myStepper.setSpeed(sspeed); |
2089 |
kakl |
83 |
myStepper.step(arg * steps); |
2088 |
kakl |
84 |
digitalWrite(M1, LOW); |
|
|
85 |
digitalWrite(M2, LOW); |
|
|
86 |
digitalWrite(M3, LOW); |
|
|
87 |
digitalWrite(M4, LOW); |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
int light (int arg) |
|
|
91 |
{ |
|
|
92 |
int LSB = 0, MSB = 0; // data from light |
|
|
93 |
|
|
|
94 |
// Setup device |
|
|
95 |
Wire.beginTransmission(lights[arg]); |
|
|
96 |
Wire.send(0x00); // command register |
|
|
97 |
Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
98 |
Wire.endTransmission(); // stop transmitting |
|
|
99 |
|
|
|
100 |
// Delay for measurement, maybe 100ms is enough, maybe not |
|
|
101 |
delay(110); |
|
|
102 |
|
|
|
103 |
// LSB |
|
|
104 |
Wire.beginTransmission(lights[arg]); |
|
|
105 |
Wire.send(0x01); // sends light0 |
|
|
106 |
Wire.endTransmission(); // stop transmitting |
|
|
107 |
// Connect to device and request one byte |
|
|
108 |
Wire.beginTransmission(lights[arg]); |
|
|
109 |
Wire.requestFrom(lights[arg], 1); |
|
|
110 |
LSB = Wire.receive(); |
|
|
111 |
Wire.endTransmission(); |
|
|
112 |
|
|
|
113 |
// MSB |
|
|
114 |
Wire.beginTransmission(lights[arg]); |
|
|
115 |
Wire.send(0x02); // sends light0 |
|
|
116 |
Wire.endTransmission(); // stop transmitting |
|
|
117 |
// Connect to device and request one byte |
|
|
118 |
Wire.beginTransmission(lights[arg]); |
|
|
119 |
Wire.requestFrom(lights[arg], 1); |
|
|
120 |
MSB = Wire.receive(); |
|
|
121 |
Wire.endTransmission(); // stop transmitting |
|
|
122 |
|
|
|
123 |
return ((MSB << 8) + LSB); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
int temperature (int arg) |
|
|
127 |
{ |
|
|
128 |
int i, Temp; |
|
|
129 |
byte data[12]; |
|
|
130 |
|
|
|
131 |
if (OneWire::crc8 (addr[arg], 7) != addr[arg][7]) |
|
|
132 |
{ |
|
|
133 |
Serial.print("CRC is not valid!\n"); |
|
|
134 |
return 0; |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
ds.reset(); |
|
|
138 |
ds.select(addr[arg]); |
|
|
139 |
ds.write(0x44, 1); // start conversion, with parasite power on at the end |
|
|
140 |
|
|
|
141 |
delay(800); // maybe 750ms is enough, maybe not |
|
|
142 |
|
|
|
143 |
ds.reset(); |
|
|
144 |
ds.select(addr[arg]); |
|
|
145 |
ds.write(0xBE); // Read Scratchpad |
|
|
146 |
|
|
|
147 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
|
|
148 |
{ |
|
|
149 |
data[i] = ds.read(); |
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
Temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature |
|
|
153 |
Temp = Temp >> 4; //divide by 16 to get pure celcius readout |
|
|
154 |
|
|
|
155 |
return Temp; |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
void accelerometer () |
|
|
159 |
{ |
|
|
160 |
Serial.print("X="); |
|
|
161 |
Serial.print(analogRead(A0)-512, DEC); |
|
|
162 |
Serial.print(" Y="); |
|
|
163 |
Serial.print(analogRead(A1)-512, DEC); |
|
|
164 |
Serial.print(" Z="); |
|
|
165 |
Serial.println(analogRead(A2)-512, DEC); |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
void readSerialString () |
|
|
169 |
{ |
2089 |
kakl |
170 |
serInIndx=0; |
|
|
171 |
do |
2088 |
kakl |
172 |
{ |
2089 |
kakl |
173 |
while(Serial.available()==0); |
|
|
174 |
serInString[serInIndx] = Serial.read(); |
|
|
175 |
Serial.print(serInString[serInIndx]); |
|
|
176 |
serInIndx++; |
|
|
177 |
} while ((serInString[serInIndx-1]!='\n')&&(serInString[serInIndx-1]!='.')); |
|
|
178 |
Serial.print("\r\n="); |
2088 |
kakl |
179 |
} |
|
|
180 |
|
|
|
181 |
void loop() |
|
|
182 |
{ |
|
|
183 |
readSerialString(); |
|
|
184 |
|
|
|
185 |
if( serInIndx > 0) |
|
|
186 |
{ |
|
|
187 |
in1 = serInString[1] - '0'; |
|
|
188 |
|
|
|
189 |
switch (serInString[0]) |
|
|
190 |
{ |
|
|
191 |
case '?': |
|
|
192 |
Serial.println ("Device queries:"); |
|
|
193 |
Serial.println (" l[0,1] light in luxes"); |
|
|
194 |
Serial.println (" t[0,1] temperature in Celsius degrees"); |
2089 |
kakl |
195 |
Serial.println (" F[0,1,2][0|1|?] switch filter wheel on (1) or off (0)"); |
|
|
196 |
Serial.println (" F[0,1,2]? check state of filter wheel"); |
2088 |
kakl |
197 |
Serial.println (" L[0,1][0|1] switch calibration lamp on (1) or off (0)"); |
|
|
198 |
Serial.println (" L[0,1]? check state of calibration lamp"); |
|
|
199 |
Serial.println (" M[0|1] motor rotation clockwise (1) or counterclockwise (0)"); |
|
|
200 |
break; |
|
|
201 |
case 't': |
|
|
202 |
Serial.println (temperature (in1)); |
|
|
203 |
break; |
|
|
204 |
case 'l': |
|
|
205 |
Serial.println (light (in1)); |
|
|
206 |
break; |
|
|
207 |
case 'L': |
|
|
208 |
if (serInString[2] == '?') |
|
|
209 |
{ |
|
|
210 |
Serial.println (digitalRead (lamps[in1]) ? '0' : '1'); |
|
|
211 |
} |
|
|
212 |
else |
|
|
213 |
{ |
|
|
214 |
in2 = serInString[2] - '0'; |
|
|
215 |
digitalWrite(lamps[in1], in2 ? LOW : HIGH); |
|
|
216 |
} |
|
|
217 |
break; |
|
|
218 |
case 'F': |
|
|
219 |
if (serInString[2] == '?') |
|
|
220 |
{ |
|
|
221 |
Serial.println (digitalRead (filters[in1]) ? '0' : '1'); |
|
|
222 |
} |
|
|
223 |
else |
|
|
224 |
{ |
|
|
225 |
in2 = serInString[2] - '0'; |
|
|
226 |
digitalWrite(filters[in1], in2 ? LOW : HIGH); |
|
|
227 |
} |
|
|
228 |
break; |
|
|
229 |
case 'M': |
|
|
230 |
motor (motors[in1]); |
|
|
231 |
break; |
|
|
232 |
} |
|
|
233 |
for (serInIndx = 100; serInIndx > 0; serInIndx--) |
|
|
234 |
serInString[serInIndx] = ' '; |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
delay(100); |
|
|
238 |
} |
|
|
239 |
|
|
|
240 |
|
|
|
241 |
|