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