2194 |
kaklik |
1 |
// I2C Light Sensor |
|
|
2 |
|
|
|
3 |
#include <Wire.h> |
|
|
4 |
|
|
|
5 |
#define address 0x44 // A0 = L |
|
|
6 |
|
|
|
7 |
#include "WProgram.h" |
|
|
8 |
void setup(); |
|
|
9 |
void loop(); |
|
|
10 |
void setup() |
|
|
11 |
{ |
|
|
12 |
Wire.begin(); // join i2c bus (address optional for master) |
|
|
13 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
14 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
15 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
|
|
16 |
} |
|
|
17 |
|
|
|
18 |
int data = 0; |
|
|
19 |
|
|
|
20 |
void loop() |
|
|
21 |
{ |
|
|
22 |
int lux=0; |
|
|
23 |
|
|
|
24 |
Serial.print("lux="); |
|
|
25 |
|
|
|
26 |
// Setup device |
|
|
27 |
digitalWrite(5, HIGH); |
|
|
28 |
|
|
|
29 |
Wire.beginTransmission(address); |
|
|
30 |
Wire.send(0x00); // sends address |
|
|
31 |
Wire.send(0b11000000); // setup (eye light sensing; one time measurement; measurement range 1) |
|
|
32 |
Wire.endTransmission(); // stop transmitting |
|
|
33 |
|
|
|
34 |
// Delay for measurement |
|
|
35 |
{ |
|
|
36 |
long n; |
|
|
37 |
for(n=0;n<66000;n++) |
|
|
38 |
{ |
|
|
39 |
digitalWrite(5, LOW); // set the LED off |
|
|
40 |
// delay(500); |
|
|
41 |
digitalWrite(5, HIGH); // set the LED on |
|
|
42 |
delayMicroseconds(100); |
|
|
43 |
// delay(500); |
|
|
44 |
} |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
// Connect to device and set register address |
|
|
49 |
Wire.beginTransmission(address); |
|
|
50 |
Wire.send(0x00); // sends address |
|
|
51 |
Wire.endTransmission(); // stop transmitting |
|
|
52 |
// Connect to device and request one byte |
|
|
53 |
Wire.beginTransmission(address); |
|
|
54 |
Wire.requestFrom(address, 1); |
|
|
55 |
data = Wire.receive(); |
|
|
56 |
Wire.endTransmission(); // stop transmitting |
|
|
57 |
// Serial.print(data, HEX); |
|
|
58 |
|
|
|
59 |
// Serial.print(" LSB="); |
|
|
60 |
// Connect to device and set register address |
|
|
61 |
Wire.beginTransmission(address); |
|
|
62 |
Wire.send(0x01); // sends address |
|
|
63 |
Wire.endTransmission(); // stop transmitting |
|
|
64 |
// Connect to device and request one byte |
|
|
65 |
Wire.beginTransmission(address); |
|
|
66 |
Wire.requestFrom(address, 1); |
|
|
67 |
data = Wire.receive(); |
|
|
68 |
Wire.endTransmission(); // stop transmitting |
|
|
69 |
// Serial.print(data, HEX); |
|
|
70 |
lux=data; |
|
|
71 |
|
|
|
72 |
// Serial.print(" MSB="); |
|
|
73 |
// Connect to device and set register address |
|
|
74 |
Wire.beginTransmission(address); |
|
|
75 |
Wire.send(0x02); // sends address |
|
|
76 |
Wire.endTransmission(); // stop transmitting |
|
|
77 |
// Connect to device and request one byte |
|
|
78 |
Wire.beginTransmission(address); |
|
|
79 |
Wire.requestFrom(address, 1); |
|
|
80 |
data = Wire.receive(); |
|
|
81 |
Wire.endTransmission(); // stop transmitting |
|
|
82 |
// Serial.print(data, HEX); |
|
|
83 |
lux+=data*256; |
|
|
84 |
|
|
|
85 |
/* |
|
|
86 |
Serial.print((unsigned)lux, DEC); |
|
|
87 |
|
|
|
88 |
Serial.print(" luxIR="); |
|
|
89 |
|
|
|
90 |
// Setup device |
|
|
91 |
Wire.beginTransmission(address); |
|
|
92 |
Wire.send(0x00); // sends address |
|
|
93 |
Wire.send(0b11100000); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
94 |
Wire.endTransmission(); // stop transmitting |
|
|
95 |
|
|
|
96 |
// Delay for measurement |
|
|
97 |
digitalWrite(3, HIGH); // set the LED on |
|
|
98 |
delay(500); |
|
|
99 |
digitalWrite(3, LOW); // set the LED off |
|
|
100 |
delay(500); |
|
|
101 |
|
|
|
102 |
|
|
|
103 |
// Connect to device and set register address |
|
|
104 |
Wire.beginTransmission(address); |
|
|
105 |
Wire.send(0x00); // sends address |
|
|
106 |
Wire.endTransmission(); // stop transmitting |
|
|
107 |
// Connect to device and request one byte |
|
|
108 |
Wire.beginTransmission(address); |
|
|
109 |
Wire.requestFrom(address, 1); |
|
|
110 |
data = Wire.receive(); |
|
|
111 |
Wire.endTransmission(); // stop transmitting |
|
|
112 |
// Serial.print(data, HEX); |
|
|
113 |
|
|
|
114 |
// Serial.print(" LSB="); |
|
|
115 |
// Connect to device and set register address |
|
|
116 |
Wire.beginTransmission(address); |
|
|
117 |
Wire.send(0x01); // sends address |
|
|
118 |
Wire.endTransmission(); // stop transmitting |
|
|
119 |
// Connect to device and request one byte |
|
|
120 |
Wire.beginTransmission(address); |
|
|
121 |
Wire.requestFrom(address, 1); |
|
|
122 |
data = Wire.receive(); |
|
|
123 |
Wire.endTransmission(); // stop transmitting |
|
|
124 |
// Serial.print(data, HEX); |
|
|
125 |
lux=data; |
|
|
126 |
|
|
|
127 |
// Serial.print(" MSB="); |
|
|
128 |
// Connect to device and set register address |
|
|
129 |
Wire.beginTransmission(address); |
|
|
130 |
Wire.send(0x02); // sends address |
|
|
131 |
Wire.endTransmission(); // stop transmitting |
|
|
132 |
// Connect to device and request one byte |
|
|
133 |
Wire.beginTransmission(address); |
|
|
134 |
Wire.requestFrom(address, 1); |
|
|
135 |
data = Wire.receive(); |
|
|
136 |
Wire.endTransmission(); // stop transmitting |
|
|
137 |
// Serial.print(data, HEX); |
|
|
138 |
lux+=data*256; |
|
|
139 |
*/ |
|
|
140 |
Serial.println((unsigned)lux, DEC); |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
|