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