2030 |
kakl |
1 |
#include <OneWire.h> |
|
|
2 |
#include <Stepper.h> |
|
|
3 |
#include <Wire.h> |
|
|
4 |
|
2033 |
kakl |
5 |
#define light0 0x44 // A0 = L (I2C light0) |
2038 |
kakl |
6 |
#define light1 0x45 // A0 = H (I2C light1) |
2030 |
kakl |
7 |
|
2035 |
kakl |
8 |
#define LAMP1 13 // Callibration Lamp 1 |
|
|
9 |
#define LAMP2 6 // Callibration Lamp 2 |
|
|
10 |
#define FW1 7 // FilterWheel 1 |
2089 |
kakl |
11 |
#define FW2 8 // FilterWheel 2 |
|
|
12 |
#define FW3 3 // FilterWheel 3 |
2035 |
kakl |
13 |
|
2087 |
kakl |
14 |
const int steps = 3500; //3200; // change this to fit the number of steps |
|
|
15 |
const int sspeed = 15; // stepper motor speed |
2030 |
kakl |
16 |
|
|
|
17 |
// initialize the stepper library on pins |
2035 |
kakl |
18 |
#define M1 9 |
|
|
19 |
#define M2 10 |
|
|
20 |
#define M3 11 |
|
|
21 |
#define M4 12 |
|
|
22 |
Stepper myStepper(steps, M1,M2,M3,M4); |
2030 |
kakl |
23 |
|
|
|
24 |
// DS18S20 Temperature chip |
2038 |
kakl |
25 |
OneWire ds(5); // 1-Wire pin |
2030 |
kakl |
26 |
|
2038 |
kakl |
27 |
byte addr[8]; // 1-Wire Address |
|
|
28 |
boolean sense; // Sense of revolution |
|
|
29 |
|
2030 |
kakl |
30 |
void setup() |
|
|
31 |
{ |
2035 |
kakl |
32 |
sense=true; |
|
|
33 |
|
|
|
34 |
pinMode(LAMP1, OUTPUT); |
|
|
35 |
pinMode(LAMP2, OUTPUT); |
|
|
36 |
pinMode(FW1, OUTPUT); |
|
|
37 |
pinMode(FW2, OUTPUT); |
|
|
38 |
pinMode(FW3, OUTPUT); |
2030 |
kakl |
39 |
|
2098 |
kakl |
40 |
pinMode(6, OUTPUT); |
|
|
41 |
analogWrite(6, 128); |
|
|
42 |
|
2030 |
kakl |
43 |
// initialize the serial port: |
|
|
44 |
Serial.begin(9600); |
|
|
45 |
|
2038 |
kakl |
46 |
Wire.begin(); // join i2c bus |
2030 |
kakl |
47 |
} |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
void loop() |
|
|
51 |
{ |
2098 |
kakl |
52 |
|
|
|
53 |
while(TRUE); |
|
|
54 |
|
2038 |
kakl |
55 |
byte i,n; // for fors |
|
|
56 |
byte present = 0; // for 1-Wire |
|
|
57 |
byte data[12]; // data from temperature |
|
|
58 |
byte inByte; // RS232 data |
|
|
59 |
int dd=0; // data from light |
2030 |
kakl |
60 |
|
2035 |
kakl |
61 |
digitalWrite(LAMP1, HIGH); // All outputs OFF |
|
|
62 |
digitalWrite(LAMP2, HIGH); |
2098 |
kakl |
63 |
digitalWrite(FW1, HIGH); |
|
|
64 |
digitalWrite(FW2, HIGH); |
|
|
65 |
digitalWrite(FW3, HIGH); |
2035 |
kakl |
66 |
delay(300); |
|
|
67 |
digitalWrite(LAMP1, LOW); // blik |
|
|
68 |
|
2036 |
kakl |
69 |
//--------------------------------------------------------- Serial Input |
2030 |
kakl |
70 |
// if we get a valid byte |
|
|
71 |
if (Serial.available() > 0) |
|
|
72 |
{ |
|
|
73 |
// get incoming byte: |
|
|
74 |
inByte = Serial.read(); |
2038 |
kakl |
75 |
Serial.print("Incoming char: "); |
2030 |
kakl |
76 |
Serial.print( inByte, HEX); |
|
|
77 |
Serial.println(); |
|
|
78 |
} |
|
|
79 |
|
2034 |
kakl |
80 |
//--------------------------------------------------------- Motor |
2035 |
kakl |
81 |
if (sense) |
2030 |
kakl |
82 |
{ |
2038 |
kakl |
83 |
// one revolution in one direction |
2030 |
kakl |
84 |
Serial.println("clockwise"); |
2033 |
kakl |
85 |
myStepper.setSpeed(sspeed); |
2087 |
kakl |
86 |
myStepper.step(steps); |
2030 |
kakl |
87 |
delay(50); |
2035 |
kakl |
88 |
digitalWrite(M1, LOW); |
|
|
89 |
digitalWrite(M2, LOW); |
|
|
90 |
digitalWrite(M3, LOW); |
|
|
91 |
digitalWrite(M4, LOW); |
2030 |
kakl |
92 |
} |
|
|
93 |
else |
|
|
94 |
{ |
2038 |
kakl |
95 |
// one revolution in the other direction: |
2030 |
kakl |
96 |
Serial.println("counterclockwise"); |
2033 |
kakl |
97 |
myStepper.setSpeed(sspeed/2); |
|
|
98 |
myStepper.step(-30); |
|
|
99 |
myStepper.setSpeed(sspeed); |
|
|
100 |
myStepper.step(-(steps-50)); |
|
|
101 |
myStepper.setSpeed(sspeed/2); |
|
|
102 |
myStepper.step(-20); |
2030 |
kakl |
103 |
delay(50); |
2035 |
kakl |
104 |
digitalWrite(M1, LOW); |
|
|
105 |
digitalWrite(M2, LOW); |
|
|
106 |
digitalWrite(M3, LOW); |
|
|
107 |
digitalWrite(M4, LOW); |
2030 |
kakl |
108 |
} |
2035 |
kakl |
109 |
sense=!sense; |
|
|
110 |
digitalWrite(LAMP2, LOW); // blik |
|
|
111 |
|
2036 |
kakl |
112 |
//--------------------------------------------------------- 1-Wire bus |
|
|
113 |
ds.reset_search(); |
|
|
114 |
for(n=0;n<2;n++) |
|
|
115 |
{ |
2038 |
kakl |
116 |
if ( !ds.search(addr)) // search for next thermometer |
2036 |
kakl |
117 |
{ |
|
|
118 |
continue; |
|
|
119 |
} |
2033 |
kakl |
120 |
|
2036 |
kakl |
121 |
Serial.print("R="); |
|
|
122 |
for( i = 0; i < 8; i++) |
|
|
123 |
{ |
|
|
124 |
Serial.print(addr[i], HEX); |
|
|
125 |
Serial.print(" "); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
if ( OneWire::crc8( addr, 7) != addr[7]) |
|
|
129 |
{ |
|
|
130 |
Serial.print("CRC is not valid!\n"); |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
ds.reset(); |
|
|
134 |
ds.select(addr); |
|
|
135 |
ds.write(0x44,1); // start conversion, with parasite power on at the end |
|
|
136 |
|
|
|
137 |
delay(800); // maybe 750ms is enough, maybe not |
2098 |
kakl |
138 |
digitalWrite(FW1, LOW); // blik |
2036 |
kakl |
139 |
|
|
|
140 |
present = ds.reset(); |
|
|
141 |
ds.select(addr); |
|
|
142 |
ds.write(0xBE); // Read Scratchpad |
|
|
143 |
|
|
|
144 |
Serial.print("P="); |
|
|
145 |
Serial.print(present,HEX); |
|
|
146 |
Serial.print(" "); |
|
|
147 |
for ( i = 0; i < 9; i++) { // we need 9 bytes |
|
|
148 |
data[i] = ds.read(); |
|
|
149 |
Serial.print(data[i], HEX); |
|
|
150 |
Serial.print(" "); |
|
|
151 |
} |
|
|
152 |
Serial.print(" CRC="); |
|
|
153 |
Serial.print( OneWire::crc8( data, 8), HEX); |
|
|
154 |
Serial.println(); |
2030 |
kakl |
155 |
} |
|
|
156 |
|
2036 |
kakl |
157 |
//------------------------------------------------------- Light 0 |
2030 |
kakl |
158 |
Serial.print("Light0: COMMAND="); |
|
|
159 |
// Setup device |
2033 |
kakl |
160 |
Wire.beginTransmission(light0); |
2038 |
kakl |
161 |
Wire.send(0x00); // command register |
2087 |
kakl |
162 |
Wire.send(0b11000000); // setup (eye light sensing; measurement range 1 [1000 lx]) |
2030 |
kakl |
163 |
Wire.endTransmission(); // stop transmitting |
|
|
164 |
|
2035 |
kakl |
165 |
// Delay for measurement, maybe 100ms is enough, maybe not |
|
|
166 |
delay(110); |
2098 |
kakl |
167 |
digitalWrite(FW2, LOW); // blik |
2030 |
kakl |
168 |
|
2033 |
kakl |
169 |
// Connect to device and set register light0 |
|
|
170 |
Wire.beginTransmission(light0); |
|
|
171 |
Wire.send(0x00); // sends light0 |
2030 |
kakl |
172 |
Wire.endTransmission(); // stop transmitting |
|
|
173 |
// Connect to device and request one byte |
2033 |
kakl |
174 |
Wire.beginTransmission(light0); |
|
|
175 |
Wire.requestFrom(light0, 1); |
2030 |
kakl |
176 |
dd = Wire.receive(); |
|
|
177 |
Wire.endTransmission(); // stop transmitting |
|
|
178 |
Serial.print(dd, HEX); |
|
|
179 |
|
|
|
180 |
Serial.print(" LSB="); |
2033 |
kakl |
181 |
// Connect to device and set register light0 |
|
|
182 |
Wire.beginTransmission(light0); |
|
|
183 |
Wire.send(0x01); // sends light0 |
2030 |
kakl |
184 |
Wire.endTransmission(); // stop transmitting |
|
|
185 |
// Connect to device and request one byte |
2033 |
kakl |
186 |
Wire.beginTransmission(light0); |
|
|
187 |
Wire.requestFrom(light0, 1); |
2030 |
kakl |
188 |
dd = Wire.receive(); |
|
|
189 |
Wire.endTransmission(); // stop transmitting |
|
|
190 |
Serial.print(dd, HEX); |
|
|
191 |
|
|
|
192 |
Serial.print(" MSB="); |
2033 |
kakl |
193 |
// Connect to device and set register light0 |
|
|
194 |
Wire.beginTransmission(light0); |
|
|
195 |
Wire.send(0x02); // sends light0 |
2030 |
kakl |
196 |
Wire.endTransmission(); // stop transmitting |
|
|
197 |
// Connect to device and request one byte |
2033 |
kakl |
198 |
Wire.beginTransmission(light0); |
|
|
199 |
Wire.requestFrom(light0, 1); |
2030 |
kakl |
200 |
dd = Wire.receive(); |
|
|
201 |
Wire.endTransmission(); // stop transmitting |
2033 |
kakl |
202 |
Serial.println(dd, HEX); |
|
|
203 |
|
2036 |
kakl |
204 |
//------------------------------------------------------- Light 1 |
2033 |
kakl |
205 |
Serial.print("Light1: COMMAND="); |
|
|
206 |
// Setup device |
|
|
207 |
Wire.beginTransmission(light1); |
2038 |
kakl |
208 |
Wire.send(0x00); // command register |
2033 |
kakl |
209 |
Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
210 |
Wire.endTransmission(); // stop transmitting |
|
|
211 |
|
2035 |
kakl |
212 |
// Delay for measurement, maybe 100ms is enough, maybe not |
|
|
213 |
delay(110); |
2098 |
kakl |
214 |
digitalWrite(FW3, LOW); // blik |
2033 |
kakl |
215 |
|
|
|
216 |
// Connect to device and set register light0 |
|
|
217 |
Wire.beginTransmission(light1); |
|
|
218 |
Wire.send(0x00); // sends light0 |
|
|
219 |
Wire.endTransmission(); // stop transmitting |
|
|
220 |
// Connect to device and request one byte |
|
|
221 |
Wire.beginTransmission(light1); |
|
|
222 |
Wire.requestFrom(light1, 1); |
|
|
223 |
dd = Wire.receive(); |
|
|
224 |
Wire.endTransmission(); // stop transmitting |
2030 |
kakl |
225 |
Serial.print(dd, HEX); |
|
|
226 |
|
2033 |
kakl |
227 |
Serial.print(" LSB="); |
|
|
228 |
// Connect to device and set register light0 |
|
|
229 |
Wire.beginTransmission(light1); |
|
|
230 |
Wire.send(0x01); // sends light0 |
|
|
231 |
Wire.endTransmission(); // stop transmitting |
|
|
232 |
// Connect to device and request one byte |
|
|
233 |
Wire.beginTransmission(light1); |
|
|
234 |
Wire.requestFrom(light1, 1); |
|
|
235 |
dd = Wire.receive(); |
|
|
236 |
Wire.endTransmission(); // stop transmitting |
|
|
237 |
Serial.print(dd, HEX); |
2030 |
kakl |
238 |
|
2033 |
kakl |
239 |
Serial.print(" MSB="); |
|
|
240 |
// Connect to device and set register light0 |
|
|
241 |
Wire.beginTransmission(light1); |
|
|
242 |
Wire.send(0x02); // sends light0 |
|
|
243 |
Wire.endTransmission(); // stop transmitting |
|
|
244 |
// Connect to device and request one byte |
|
|
245 |
Wire.beginTransmission(light1); |
|
|
246 |
Wire.requestFrom(light1, 1); |
|
|
247 |
dd = Wire.receive(); |
|
|
248 |
Wire.endTransmission(); // stop transmitting |
|
|
249 |
Serial.println(dd, HEX); |
|
|
250 |
|
2036 |
kakl |
251 |
//-------------------------------------------------- Accelerometer |
2033 |
kakl |
252 |
Serial.print("X="); |
2034 |
kakl |
253 |
Serial.print(analogRead(A0)-512, DEC); |
2033 |
kakl |
254 |
Serial.print(" Y="); |
2034 |
kakl |
255 |
Serial.print(analogRead(A1)-512, DEC); |
2033 |
kakl |
256 |
Serial.print(" Z="); |
2034 |
kakl |
257 |
Serial.println(analogRead(A2)-512, DEC); |
2030 |
kakl |
258 |
} |
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|