2194 |
kaklik |
1 |
// I2C Light Sensor |
|
|
2 |
|
|
|
3 |
#include <Wire.h> |
2240 |
kaklik |
4 |
#include <math.h> |
2194 |
kaklik |
5 |
|
|
|
6 |
#define address 0x44 // A0 = L |
2240 |
kaklik |
7 |
#define SENSE_VIS 0 |
|
|
8 |
#define SENSE_IR 1 |
2194 |
kaklik |
9 |
|
2240 |
kaklik |
10 |
#define LIGHT_AUTORANGE 0 |
|
|
11 |
#define LIGHT_RANGE1 1 |
|
|
12 |
#define LIGHT_RANGE2 2 |
|
|
13 |
#define LIGHT_RANGE3 3 |
|
|
14 |
#define LIGHT_RANGE4 4 |
|
|
15 |
|
|
|
16 |
|
2194 |
kaklik |
17 |
void setup() |
|
|
18 |
{ |
|
|
19 |
Wire.begin(); // join i2c bus (address optional for master) |
2240 |
kaklik |
20 |
// I2C pins PD4, PD5 |
|
|
21 |
// |
2194 |
kaklik |
22 |
pinMode(3, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
23 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
|
|
24 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
int data = 0; |
2240 |
kaklik |
28 |
int light_sensor_setup; |
2194 |
kaklik |
29 |
|
2240 |
kaklik |
30 |
void led_blink() |
2194 |
kaklik |
31 |
{ |
2240 |
kaklik |
32 |
digitalWrite(3, HIGH); // set the LED on |
|
|
33 |
delay(500); |
|
|
34 |
digitalWrite(3, LOW); // set the LED off |
|
|
35 |
delay(500); |
|
|
36 |
} |
2194 |
kaklik |
37 |
|
|
|
38 |
|
2240 |
kaklik |
39 |
int set_light_sensor(int mode) |
|
|
40 |
{ |
|
|
41 |
int command; |
|
|
42 |
|
|
|
43 |
switch (mode) |
2194 |
kaklik |
44 |
{ |
2240 |
kaklik |
45 |
case SENSE_VIS: |
|
|
46 |
{ |
|
|
47 |
command=0b11000000; // setup (eye light sensing; one time measurement; measurement range 1) |
|
|
48 |
break; |
2194 |
kaklik |
49 |
} |
2240 |
kaklik |
50 |
|
|
|
51 |
case SENSE_IR: |
|
|
52 |
{ |
|
|
53 |
command=0b11100000; // setup (eye light sensing; measurement range 2 [4000 lx]) |
|
|
54 |
break; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
default: |
|
|
58 |
return 3; |
2194 |
kaklik |
59 |
} |
|
|
60 |
|
2240 |
kaklik |
61 |
// Setup device |
2194 |
kaklik |
62 |
Wire.beginTransmission(address); |
2240 |
kaklik |
63 |
Wire.write(0x00); // sends address |
|
|
64 |
Wire.write(command); // setup (eye light sensing; one time measurement; measurement range 1) |
2194 |
kaklik |
65 |
Wire.endTransmission(); // stop transmitting |
2240 |
kaklik |
66 |
|
|
|
67 |
|
|
|
68 |
// Connect to device and set register address |
2194 |
kaklik |
69 |
Wire.beginTransmission(address); |
2240 |
kaklik |
70 |
Wire.write(0x00); // sends address (command register) |
2194 |
kaklik |
71 |
Wire.endTransmission(); // stop transmitting |
2240 |
kaklik |
72 |
|
|
|
73 |
// verify written command byte |
|
|
74 |
Wire.beginTransmission(address); |
2194 |
kaklik |
75 |
Wire.requestFrom(address, 1); |
2240 |
kaklik |
76 |
if (command != Wire.read()) |
|
|
77 |
{ |
|
|
78 |
return 4; |
|
|
79 |
Serial.print(data, BIN); |
|
|
80 |
} |
2194 |
kaklik |
81 |
Wire.endTransmission(); // stop transmitting |
2240 |
kaklik |
82 |
light_sensor_setup=command; |
|
|
83 |
} |
2194 |
kaklik |
84 |
|
2240 |
kaklik |
85 |
float get_light_measurement() |
|
|
86 |
{ |
|
|
87 |
int ret=0; |
2194 |
kaklik |
88 |
|
2240 |
kaklik |
89 |
// Connect to device and set register address |
2194 |
kaklik |
90 |
Wire.beginTransmission(address); |
2240 |
kaklik |
91 |
Wire.write(0x01); // sends address of LSB reagister |
2194 |
kaklik |
92 |
Wire.endTransmission(); // stop transmitting |
|
|
93 |
|
|
|
94 |
// Connect to device and request one byte |
|
|
95 |
Wire.beginTransmission(address); |
|
|
96 |
Wire.requestFrom(address, 1); |
2240 |
kaklik |
97 |
ret = Wire.read(); |
2194 |
kaklik |
98 |
Wire.endTransmission(); // stop transmitting |
|
|
99 |
|
|
|
100 |
// Connect to device and set register address |
|
|
101 |
Wire.beginTransmission(address); |
2240 |
kaklik |
102 |
Wire.write(0x02); // sends address of MSB register |
2194 |
kaklik |
103 |
Wire.endTransmission(); // stop transmitting |
|
|
104 |
|
|
|
105 |
// Connect to device and request one byte |
|
|
106 |
Wire.beginTransmission(address); |
|
|
107 |
Wire.requestFrom(address, 1); |
2240 |
kaklik |
108 |
ret +=256 * Wire.read(); |
2194 |
kaklik |
109 |
Wire.endTransmission(); // stop transmitting |
2240 |
kaklik |
110 |
|
|
|
111 |
return (1000.0/pow(2.0,16)*ret); |
2194 |
kaklik |
112 |
} |
|
|
113 |
|
2240 |
kaklik |
114 |
void loop() |
|
|
115 |
{ |
|
|
116 |
int lux=0; |
|
|
117 |
|
|
|
118 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
|
|
119 |
led_blink(); // Delay for measurement |
|
|
120 |
Serial.print("lux="); |
|
|
121 |
Serial.println(get_light_measurement(),2); |
|
|
122 |
|
|
|
123 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
|
|
124 |
led_blink(); // Delay for measurement |
|
|
125 |
Serial.print("luxIR="); |
|
|
126 |
Serial.println(get_light_measurement(), 2); // data print |
|
|
127 |
} |
|
|
128 |
|