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