2030 |
kakl |
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 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
|
|
12 |
} |
|
|
13 |
|
|
|
14 |
int data = 0; |
|
|
15 |
|
|
|
16 |
void loop() |
|
|
17 |
{ |
|
|
18 |
Serial.print("COMMAND="); |
|
|
19 |
|
|
|
20 |
// Setup device |
|
|
21 |
Wire.beginTransmission(address); |
|
|
22 |
Wire.send(0x00); // sends address |
|
|
23 |
Wire.send(0b11000001); // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
24 |
Wire.endTransmission(); // stop transmitting |
|
|
25 |
|
|
|
26 |
// Delay for measurement |
|
|
27 |
digitalWrite(3, HIGH); // set the LED on |
|
|
28 |
delay(500); |
|
|
29 |
digitalWrite(3, LOW); // set the LED off |
|
|
30 |
delay(500); |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
// Connect to device and set register address |
|
|
34 |
Wire.beginTransmission(address); |
|
|
35 |
Wire.send(0x00); // sends address |
|
|
36 |
Wire.endTransmission(); // stop transmitting |
|
|
37 |
// Connect to device and request one byte |
|
|
38 |
Wire.beginTransmission(address); |
|
|
39 |
Wire.requestFrom(address, 1); |
|
|
40 |
data = Wire.receive(); |
|
|
41 |
Wire.endTransmission(); // stop transmitting |
|
|
42 |
Serial.print(data, HEX); |
|
|
43 |
|
|
|
44 |
Serial.print(" LSB="); |
|
|
45 |
// Connect to device and set register address |
|
|
46 |
Wire.beginTransmission(address); |
|
|
47 |
Wire.send(0x01); // 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(" MSB="); |
|
|
57 |
// Connect to device and set register address |
|
|
58 |
Wire.beginTransmission(address); |
|
|
59 |
Wire.send(0x02); // 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 |
|
|
|
68 |
Serial.print("\n"); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
|