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