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