| Line 1... |
Line 1... |
| 1 |
#include <OneWire.h> |
1 |
#include <OneWire.h> |
| 2 |
#include <Stepper.h> |
2 |
#include <Stepper.h> |
| 3 |
#include <Wire.h> |
3 |
#include <Wire.h> |
| 4 |
|
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 |
|
| 5 |
#define light0 0x44 // A0 = L (I2C light0) |
25 |
#define light0 0x44 // A0 = L (I2C light0) |
| 6 |
#define light1 0x45 // A0 = H (I2C light1) |
26 |
#define light1 0x45 // A0 = H (I2C light1) |
| 7 |
|
27 |
|
| 8 |
int lights[] = {light0, light1}; |
28 |
int lights[] = {light0, light1}; |
| 9 |
|
29 |
|
| Line 16... |
Line 36... |
| 16 |
#define FW2 8 // [PB0 - yellow] Grism Wheel 2-nd from light |
36 |
#define FW2 8 // [PB0 - yellow] Grism Wheel 2-nd from light |
| 17 |
#define FW3 3 // [PD3 - blue] Filter Wheel 3-rd from light |
37 |
#define FW3 3 // [PD3 - blue] Filter Wheel 3-rd from light |
| 18 |
|
38 |
|
| 19 |
int filters[] = {FW1, FW2, FW3}; |
39 |
int filters[] = {FW1, FW2, FW3}; |
| 20 |
|
40 |
|
| 21 |
int motors[] = {-1, 1}; |
- |
|
| 22 |
|
- |
|
| 23 |
const int steps = 3500; // change this to fit the number of steps |
41 |
const int steps = 3500; // change this to fit the number of steps |
| 24 |
const int sspeed = 10; // max. 15 // stepper motor speed |
42 |
const int sspeed = 10; // max. 15 // stepper motor speed |
| 25 |
|
43 |
|
| 26 |
// initialize the stepper library on pins |
44 |
// initialize the stepper library on pins |
| 27 |
#define M1 9 |
45 |
#define M1 9 |
| Line 36... |
Line 54... |
| 36 |
|
54 |
|
| 37 |
char serInString[100]; |
55 |
char serInString[100]; |
| 38 |
int serInIndx = 0; |
56 |
int serInIndx = 0; |
| 39 |
int in1, in2; |
57 |
int in1, in2; |
| 40 |
|
58 |
|
| 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) |
59 |
void motor (int arg) |
| 79 |
{ |
60 |
{ |
| 80 |
myStepper.setSpeed(1); |
61 |
myStepper.setSpeed(1); |
| 81 |
myStepper.step(arg * 10); |
62 |
myStepper.step(arg * 10); |
| 82 |
myStepper.setSpeed(sspeed); |
63 |
myStepper.setSpeed(sspeed); |
| Line 123... |
Line 104... |
| 123 |
return ((MSB << 8) + LSB); |
104 |
return ((MSB << 8) + LSB); |
| 124 |
} |
105 |
} |
| 125 |
|
106 |
|
| 126 |
int temperature (int arg) |
107 |
int temperature (int arg) |
| 127 |
{ |
108 |
{ |
| 128 |
int i, Temp; |
109 |
int i, temp; |
| 129 |
byte data[12]; |
110 |
byte data[12]; |
| 130 |
|
111 |
|
| 131 |
if (OneWire::crc8 (addr[arg], 7) != addr[arg][7]) |
112 |
if (OneWire::crc8 (addr[arg], 7) != addr[arg][7]) |
| 132 |
{ |
113 |
{ |
| 133 |
Serial.print("CRC is not valid!\n"); |
114 |
Serial.print("CRC is not valid!\n"); |
| Line 147... |
Line 128... |
| 147 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
128 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
| 148 |
{ |
129 |
{ |
| 149 |
data[i] = ds.read(); |
130 |
data[i] = ds.read(); |
| 150 |
} |
131 |
} |
| 151 |
|
132 |
|
| 152 |
Temp = (data[1] << 8) + data[0]; //take the two bytes from the response relating to temperature |
133 |
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 |
134 |
// temp = temp >> 4; //divide by 16 to get pure celcius readout |
| - |
|
135 |
|
| - |
|
136 |
return temp; |
| - |
|
137 |
} |
| - |
|
138 |
|
| - |
|
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); |
| - |
|
149 |
} |
| 154 |
|
150 |
|
| - |
|
151 |
void setup() |
| - |
|
152 |
{ |
| - |
|
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); |
| 155 |
return Temp; |
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 (); |
| 156 |
} |
185 |
} |
| 157 |
|
186 |
|
| 158 |
void accelerometer () |
187 |
void telemetrie () |
| 159 |
{ |
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); |
| 160 |
Serial.print("X="); |
197 |
Serial.print ("."); |
| 161 |
Serial.print(analogRead(A0)-512, DEC); |
198 |
Serial.print (temperature (0) % 16); |
| 162 |
Serial.print(" Y="); |
199 |
Serial.print (",T2="); |
| 163 |
Serial.print(analogRead(A1)-512, DEC); |
200 |
Serial.print (temperature (1) >> 4); |
| 164 |
Serial.print(" Z="); |
201 |
Serial.print ("."); |
| 165 |
Serial.println(analogRead(A2)-512, DEC); |
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 (); |
| 166 |
} |
208 |
} |
| 167 |
|
209 |
|
| 168 |
void readSerialString () |
210 |
void readSerialString () |
| 169 |
{ |
211 |
{ |
| 170 |
serInIndx=0; |
212 |
serInIndx=0; |
| Line 178... |
Line 220... |
| 178 |
Serial.print("\r\n="); |
220 |
Serial.print("\r\n="); |
| 179 |
} |
221 |
} |
| 180 |
|
222 |
|
| 181 |
void loop() |
223 |
void loop() |
| 182 |
{ |
224 |
{ |
| 183 |
readSerialString(); |
225 |
// readSerialString(); |
| 184 |
|
226 |
|
| 185 |
if( serInIndx > 0) |
227 |
if (Serial.available()) |
| 186 |
{ |
228 |
{ |
| 187 |
in1 = serInString[1] - '0'; |
- |
|
| 188 |
|
- |
|
| 189 |
switch (serInString[0]) |
229 |
switch (Serial.read()) |
| 190 |
{ |
230 |
{ |
| 191 |
case '?': |
231 |
case 'i': // inicializace |
| 192 |
Serial.println ("Device queries:"); |
- |
|
| 193 |
Serial.println (" l[0,1] light in luxes"); |
- |
|
| 194 |
Serial.println (" t[0,1] temperature in Celsius degrees"); |
- |
|
| 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"); |
- |
|
| 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; |
232 |
inicializace (); |
| 201 |
case 't': |
- |
|
| 202 |
Serial.println (temperature (in1)); |
- |
|
| 203 |
break; |
233 |
telemetrie (); |
| 204 |
case 'l': |
- |
|
| 205 |
Serial.println (light (in1)); |
- |
|
| 206 |
break; |
234 |
break; |
| 207 |
case 'L': |
235 |
case 'm': |
| 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; |
236 |
motor (-1); |
| 218 |
case 'F': |
- |
|
| 219 |
if (serInString[2] == '?') |
- |
|
| 220 |
{ |
- |
|
| 221 |
Serial.println (digitalRead (filters[in1]) ? '0' : '1'); |
- |
|
| 222 |
} |
- |
|
| 223 |
else |
237 |
telemetrie (); |
| 224 |
{ |
- |
|
| 225 |
in2 = serInString[2] - '0'; |
- |
|
| 226 |
digitalWrite(filters[in1], in2 ? LOW : HIGH); |
- |
|
| 227 |
} |
- |
|
| 228 |
break; |
238 |
break; |
| 229 |
case 'M': |
239 |
case 'M': |
| - |
|
240 |
motor (1); // vysunuto |
| 230 |
motor (motors[in1]); |
241 |
telemetrie (); |
| - |
|
242 |
break; |
| - |
|
243 |
case 'A': |
| - |
|
244 |
digitalWrite(FW1, HIGH); |
| - |
|
245 |
telemetrie (); |
| - |
|
246 |
break; |
| - |
|
247 |
case 'a': |
| - |
|
248 |
digitalWrite(FW1, LOW); |
| - |
|
249 |
telemetrie (); |
| - |
|
250 |
break; |
| - |
|
251 |
case 'B': |
| - |
|
252 |
digitalWrite(FW2, HIGH); |
| - |
|
253 |
telemetrie (); |
| - |
|
254 |
break; |
| - |
|
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: |
| 231 |
break; |
268 |
break; |
| 232 |
} |
269 |
} |
| - |
|
270 |
Serial.flush (); |
| 233 |
for (serInIndx = 100; serInIndx > 0; serInIndx--) |
271 |
// /for (serInIndx = 100; serInIndx > 0; serInIndx--) |
| 234 |
serInString[serInIndx] = ' '; |
272 |
// serInString[serInIndx] = ' '; |
| 235 |
} |
273 |
} |
| 236 |
|
274 |
|
| 237 |
delay(100); |
275 |
// delay(100); |
| 238 |
} |
276 |
} |
| 239 |
|
- |
|
| 240 |
|
- |
|
| 241 |
|
- |
|