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