2030 |
kakl |
1 |
#include <OneWire.h> |
|
|
2 |
#include <Stepper.h> |
|
|
3 |
#include <Wire.h> |
|
|
4 |
|
|
|
5 |
#define address 0x44 // A0 = L (I2C address) |
|
|
6 |
|
|
|
7 |
const int steps = 200; //3200; // change this to fit the number of steps |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
// initialize the stepper library on pins |
|
|
11 |
Stepper myStepper(steps, 9,10,11,12); |
|
|
12 |
|
|
|
13 |
// DS18S20 Temperature chip |
|
|
14 |
OneWire ds(1); // 1-Wire |
|
|
15 |
byte addr[8]; // Addres |
|
|
16 |
|
|
|
17 |
void setup() |
|
|
18 |
{ |
|
|
19 |
pinMode(13, OUTPUT); // LED |
|
|
20 |
digitalWrite(13, LOW); // LED ON |
|
|
21 |
|
|
|
22 |
// initialize the serial port: |
|
|
23 |
Serial.begin(9600); |
|
|
24 |
|
|
|
25 |
// set the speed |
|
|
26 |
myStepper.setSpeed(8); |
|
|
27 |
|
|
|
28 |
Wire.begin(); // join i2c bus (address optional for master) |
|
|
29 |
|
|
|
30 |
// search for DS |
|
|
31 |
if ( !ds.search(addr)) |
|
|
32 |
{ |
|
|
33 |
ds.reset_search(); |
|
|
34 |
delay(250); |
|
|
35 |
return; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
if ( OneWire::crc8( addr, 7) != addr[7]) |
|
|
39 |
{ |
|
|
40 |
Serial.print("CRC is not valid!\n"); |
|
|
41 |
return; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
byte sense=0; |
|
|
48 |
|
|
|
49 |
void loop() |
|
|
50 |
{ |
|
|
51 |
byte i; |
|
|
52 |
byte present = 0; |
|
|
53 |
byte data[12]; |
|
|
54 |
byte inByte; |
|
|
55 |
int dd=0; |
|
|
56 |
|
|
|
57 |
// if we get a valid byte |
|
|
58 |
if (Serial.available() > 0) |
|
|
59 |
{ |
|
|
60 |
// get incoming byte: |
|
|
61 |
inByte = Serial.read(); |
|
|
62 |
Serial.print("Prijat znak: "); |
|
|
63 |
Serial.print( inByte, HEX); |
|
|
64 |
Serial.println(); |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
if(sense) |
|
|
68 |
{ |
|
|
69 |
digitalWrite(13, LOW); // blik |
|
|
70 |
// step one revolution in one direction: |
|
|
71 |
Serial.println("clockwise"); |
|
|
72 |
myStepper.step(steps); |
|
|
73 |
delay(50); |
|
|
74 |
digitalWrite(9, LOW); |
|
|
75 |
digitalWrite(10, LOW); |
|
|
76 |
digitalWrite(11, LOW); |
|
|
77 |
digitalWrite(12, LOW); |
|
|
78 |
delay(500); |
|
|
79 |
sense=0; |
|
|
80 |
} |
|
|
81 |
else |
|
|
82 |
{ |
|
|
83 |
digitalWrite(13, HIGH); // blik |
|
|
84 |
// step one revolution in the other direction: |
|
|
85 |
Serial.println("counterclockwise"); |
|
|
86 |
myStepper.step(-steps); |
|
|
87 |
delay(50); |
|
|
88 |
digitalWrite(9, LOW); |
|
|
89 |
digitalWrite(10, LOW); |
|
|
90 |
digitalWrite(11, LOW); |
|
|
91 |
digitalWrite(12, LOW); |
|
|
92 |
delay(500); |
|
|
93 |
sense=1; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
// 1-Wire bus |
|
|
97 |
ds.reset(); |
|
|
98 |
ds.select(addr); |
|
|
99 |
ds.write(0x44,1); // start conversion |
|
|
100 |
delay(1000); // maybe 750ms is enough, maybe not |
|
|
101 |
present = ds.reset(); |
|
|
102 |
ds.select(addr); |
|
|
103 |
ds.write(0xBE); // Read Scratchpad |
|
|
104 |
|
|
|
105 |
Serial.print("P="); |
|
|
106 |
Serial.print(present,HEX); |
|
|
107 |
Serial.print(" "); |
|
|
108 |
for ( i = 0; i < 9; i++) // we need 9 bytes |
|
|
109 |
{ |
|
|
110 |
data[i] = ds.read(); |
|
|
111 |
Serial.print(data[i], HEX); |
|
|
112 |
Serial.print(" "); |
|
|
113 |
} |
|
|
114 |
Serial.print(" CRC="); |
|
|
115 |
Serial.print( OneWire::crc8( data, 8), HEX); |
|
|
116 |
Serial.println(); |
|
|
117 |
|
|
|
118 |
Serial.print("Light0: COMMAND="); |
|
|
119 |
|
|
|
120 |
// Setup device |
|
|
121 |
Wire.beginTransmission(address); |
|
|
122 |
Wire.send(0x00); // sends address |
|
|
123 |
Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
124 |
Wire.endTransmission(); // stop transmitting |
|
|
125 |
|
|
|
126 |
// Delay for measurement |
|
|
127 |
digitalWrite(13, HIGH); // set the LED on |
|
|
128 |
delay(500); |
|
|
129 |
digitalWrite(13, LOW); // set the LED off |
|
|
130 |
delay(500); |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
// Connect to device and set register address |
|
|
134 |
Wire.beginTransmission(address); |
|
|
135 |
Wire.send(0x00); // sends address |
|
|
136 |
Wire.endTransmission(); // stop transmitting |
|
|
137 |
// Connect to device and request one byte |
|
|
138 |
Wire.beginTransmission(address); |
|
|
139 |
Wire.requestFrom(address, 1); |
|
|
140 |
dd = Wire.receive(); |
|
|
141 |
Wire.endTransmission(); // stop transmitting |
|
|
142 |
Serial.print(dd, HEX); |
|
|
143 |
|
|
|
144 |
Serial.print(" LSB="); |
|
|
145 |
// Connect to device and set register address |
|
|
146 |
Wire.beginTransmission(address); |
|
|
147 |
Wire.send(0x01); // sends address |
|
|
148 |
Wire.endTransmission(); // stop transmitting |
|
|
149 |
// Connect to device and request one byte |
|
|
150 |
Wire.beginTransmission(address); |
|
|
151 |
Wire.requestFrom(address, 1); |
|
|
152 |
dd = Wire.receive(); |
|
|
153 |
Wire.endTransmission(); // stop transmitting |
|
|
154 |
Serial.print(dd, HEX); |
|
|
155 |
|
|
|
156 |
Serial.print(" MSB="); |
|
|
157 |
// Connect to device and set register address |
|
|
158 |
Wire.beginTransmission(address); |
|
|
159 |
Wire.send(0x02); // sends address |
|
|
160 |
Wire.endTransmission(); // stop transmitting |
|
|
161 |
// Connect to device and request one byte |
|
|
162 |
Wire.beginTransmission(address); |
|
|
163 |
Wire.requestFrom(address, 1); |
|
|
164 |
dd = Wire.receive(); |
|
|
165 |
Wire.endTransmission(); // stop transmitting |
|
|
166 |
Serial.print(dd, HEX); |
|
|
167 |
|
|
|
168 |
Serial.print("\n"); |
|
|
169 |
|
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|