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