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
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
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);
57 digitalWrite(FW1, HIGH);
58 digitalWrite(FW2, HIGH);
59 digitalWrite(FW3, HIGH);
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/2);
80 myStepper.step(30);
81 myStepper.setSpeed(sspeed);
82 myStepper.step(steps-50);
83 myStepper.setSpeed(sspeed/2);
84 myStepper.step(20);
2030 kakl 85 delay(50);
2035 kakl 86 digitalWrite(M1, LOW);
87 digitalWrite(M2, LOW);
88 digitalWrite(M3, LOW);
89 digitalWrite(M4, LOW);
2030 kakl 90 }
91 else
92 {
2038 kakl 93 // one revolution in the other direction:
2030 kakl 94 Serial.println("counterclockwise");
2033 kakl 95 myStepper.setSpeed(sspeed/2);
96 myStepper.step(-30);
97 myStepper.setSpeed(sspeed);
98 myStepper.step(-(steps-50));
99 myStepper.setSpeed(sspeed/2);
100 myStepper.step(-20);
2030 kakl 101 delay(50);
2035 kakl 102 digitalWrite(M1, LOW);
103 digitalWrite(M2, LOW);
104 digitalWrite(M3, LOW);
105 digitalWrite(M4, LOW);
2030 kakl 106 }
2035 kakl 107 sense=!sense;
108 digitalWrite(LAMP2, LOW); // blik
109  
2036 kakl 110 //--------------------------------------------------------- 1-Wire bus
111 ds.reset_search();
112 for(n=0;n<2;n++)
113 {
2038 kakl 114 if ( !ds.search(addr)) // search for next thermometer
2036 kakl 115 {
116 continue;
117 }
2033 kakl 118  
2036 kakl 119 Serial.print("R=");
120 for( i = 0; i < 8; i++)
121 {
122 Serial.print(addr[i], HEX);
123 Serial.print(" ");
124 }
125  
126 if ( OneWire::crc8( addr, 7) != addr[7])
127 {
128 Serial.print("CRC is not valid!\n");
129 }
130  
131 ds.reset();
132 ds.select(addr);
133 ds.write(0x44,1); // start conversion, with parasite power on at the end
134  
135 delay(800); // maybe 750ms is enough, maybe not
136 digitalWrite(FW1, LOW); // blik
137  
138 present = ds.reset();
139 ds.select(addr);
140 ds.write(0xBE); // Read Scratchpad
141  
142 Serial.print("P=");
143 Serial.print(present,HEX);
144 Serial.print(" ");
145 for ( i = 0; i < 9; i++) { // we need 9 bytes
146 data[i] = ds.read();
147 Serial.print(data[i], HEX);
148 Serial.print(" ");
149 }
150 Serial.print(" CRC=");
151 Serial.print( OneWire::crc8( data, 8), HEX);
152 Serial.println();
2030 kakl 153 }
154  
2036 kakl 155 //------------------------------------------------------- Light 0
2030 kakl 156 Serial.print("Light0: COMMAND=");
157 // Setup device
2033 kakl 158 Wire.beginTransmission(light0);
2038 kakl 159 Wire.send(0x00); // command register
2030 kakl 160 Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx])
161 Wire.endTransmission(); // stop transmitting
162  
2035 kakl 163 // Delay for measurement, maybe 100ms is enough, maybe not
164 delay(110);
165 digitalWrite(FW2, LOW); // blik
2030 kakl 166  
2033 kakl 167 // Connect to device and set register light0
168 Wire.beginTransmission(light0);
169 Wire.send(0x00); // sends light0
2030 kakl 170 Wire.endTransmission(); // stop transmitting
171 // Connect to device and request one byte
2033 kakl 172 Wire.beginTransmission(light0);
173 Wire.requestFrom(light0, 1);
2030 kakl 174 dd = Wire.receive();
175 Wire.endTransmission(); // stop transmitting
176 Serial.print(dd, HEX);
177  
178 Serial.print(" LSB=");
2033 kakl 179 // Connect to device and set register light0
180 Wire.beginTransmission(light0);
181 Wire.send(0x01); // sends light0
2030 kakl 182 Wire.endTransmission(); // stop transmitting
183 // Connect to device and request one byte
2033 kakl 184 Wire.beginTransmission(light0);
185 Wire.requestFrom(light0, 1);
2030 kakl 186 dd = Wire.receive();
187 Wire.endTransmission(); // stop transmitting
188 Serial.print(dd, HEX);
189  
190 Serial.print(" MSB=");
2033 kakl 191 // Connect to device and set register light0
192 Wire.beginTransmission(light0);
193 Wire.send(0x02); // sends light0
2030 kakl 194 Wire.endTransmission(); // stop transmitting
195 // Connect to device and request one byte
2033 kakl 196 Wire.beginTransmission(light0);
197 Wire.requestFrom(light0, 1);
2030 kakl 198 dd = Wire.receive();
199 Wire.endTransmission(); // stop transmitting
2033 kakl 200 Serial.println(dd, HEX);
201  
2036 kakl 202 //------------------------------------------------------- Light 1
2033 kakl 203 Serial.print("Light1: COMMAND=");
204 // Setup device
205 Wire.beginTransmission(light1);
2038 kakl 206 Wire.send(0x00); // command register
2033 kakl 207 Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx])
208 Wire.endTransmission(); // stop transmitting
209  
2035 kakl 210 // Delay for measurement, maybe 100ms is enough, maybe not
211 delay(110);
212 digitalWrite(FW3, LOW); // blik
2033 kakl 213  
214 // Connect to device and set register light0
215 Wire.beginTransmission(light1);
216 Wire.send(0x00); // sends light0
217 Wire.endTransmission(); // stop transmitting
218 // Connect to device and request one byte
219 Wire.beginTransmission(light1);
220 Wire.requestFrom(light1, 1);
221 dd = Wire.receive();
222 Wire.endTransmission(); // stop transmitting
2030 kakl 223 Serial.print(dd, HEX);
224  
2033 kakl 225 Serial.print(" LSB=");
226 // Connect to device and set register light0
227 Wire.beginTransmission(light1);
228 Wire.send(0x01); // sends light0
229 Wire.endTransmission(); // stop transmitting
230 // Connect to device and request one byte
231 Wire.beginTransmission(light1);
232 Wire.requestFrom(light1, 1);
233 dd = Wire.receive();
234 Wire.endTransmission(); // stop transmitting
235 Serial.print(dd, HEX);
2030 kakl 236  
2033 kakl 237 Serial.print(" MSB=");
238 // Connect to device and set register light0
239 Wire.beginTransmission(light1);
240 Wire.send(0x02); // sends light0
241 Wire.endTransmission(); // stop transmitting
242 // Connect to device and request one byte
243 Wire.beginTransmission(light1);
244 Wire.requestFrom(light1, 1);
245 dd = Wire.receive();
246 Wire.endTransmission(); // stop transmitting
247 Serial.println(dd, HEX);
248  
2036 kakl 249 //-------------------------------------------------- Accelerometer
2033 kakl 250 Serial.print("X=");
2034 kakl 251 Serial.print(analogRead(A0)-512, DEC);
2033 kakl 252 Serial.print(" Y=");
2034 kakl 253 Serial.print(analogRead(A1)-512, DEC);
2033 kakl 254 Serial.print(" Z=");
2034 kakl 255 Serial.println(analogRead(A2)-512, DEC);
2030 kakl 256 }
257  
258  
259