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