Line 1... |
Line 1... |
1 |
// I2C Light Sensor |
1 |
// I2C Light Sensor |
2 |
|
2 |
|
3 |
#include <Wire.h> |
3 |
#include <Wire.h> |
- |
|
4 |
#include <math.h> |
4 |
|
5 |
|
5 |
#define address 0x44 // A0 = L |
6 |
#define address 0x44 // A0 = L |
6 |
#define SENSE_VIS 0 |
7 |
#define SENSE_VIS 0 |
7 |
#define SENSE_IR 1 |
8 |
#define SENSE_IR 1 |
8 |
|
9 |
|
Line 16... |
Line 17... |
16 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
17 |
pinMode(5, OUTPUT); // LED pro blikani, aby bylo videt, ze to neco dela |
17 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
18 |
Serial.begin(9600); // Zmerena intenzita osvetleni se bude vypisovat na seriovou linku |
18 |
} |
19 |
} |
19 |
|
20 |
|
20 |
int data = 0; |
21 |
int data = 0; |
- |
|
22 |
int light_sensor_setup; |
21 |
|
23 |
|
22 |
void led_blink() |
24 |
void led_blink() |
23 |
{ |
25 |
{ |
24 |
digitalWrite(3, HIGH); // set the LED on |
26 |
digitalWrite(3, HIGH); // set the LED on |
25 |
delay(500); |
27 |
delay(500); |
Line 69... |
Line 71... |
69 |
{ |
71 |
{ |
70 |
return 4; |
72 |
return 4; |
71 |
Serial.print(data, BIN); |
73 |
Serial.print(data, BIN); |
72 |
} |
74 |
} |
73 |
Wire.endTransmission(); // stop transmitting |
75 |
Wire.endTransmission(); // stop transmitting |
74 |
|
- |
|
- |
|
76 |
light_sensor_setup=command; |
75 |
} |
77 |
} |
76 |
|
78 |
|
77 |
int get_light_measurement() |
79 |
float get_light_measurement() |
78 |
{ |
80 |
{ |
79 |
int lux=0; |
81 |
int ret=0; |
80 |
|
82 |
|
81 |
// Connect to device and set register address |
83 |
// Connect to device and set register address |
82 |
Wire.beginTransmission(address); |
84 |
Wire.beginTransmission(address); |
83 |
Wire.send(0x01); // sends address of LSB reagister |
85 |
Wire.send(0x01); // sends address of LSB reagister |
84 |
Wire.endTransmission(); // stop transmitting |
86 |
Wire.endTransmission(); // stop transmitting |
85 |
|
87 |
|
86 |
// Connect to device and request one byte |
88 |
// Connect to device and request one byte |
87 |
Wire.beginTransmission(address); |
89 |
Wire.beginTransmission(address); |
88 |
Wire.requestFrom(address, 1); |
90 |
Wire.requestFrom(address, 1); |
89 |
data = Wire.receive(); |
91 |
ret = Wire.receive(); |
90 |
Wire.endTransmission(); // stop transmitting |
92 |
Wire.endTransmission(); // stop transmitting |
91 |
lux=data; |
- |
|
92 |
|
93 |
|
93 |
// Connect to device and set register address |
94 |
// Connect to device and set register address |
94 |
Wire.beginTransmission(address); |
95 |
Wire.beginTransmission(address); |
95 |
Wire.send(0x02); // sends address of MSB register |
96 |
Wire.send(0x02); // sends address of MSB register |
96 |
Wire.endTransmission(); // stop transmitting |
97 |
Wire.endTransmission(); // stop transmitting |
97 |
|
98 |
|
98 |
// Connect to device and request one byte |
99 |
// Connect to device and request one byte |
99 |
Wire.beginTransmission(address); |
100 |
Wire.beginTransmission(address); |
100 |
Wire.requestFrom(address, 1); |
101 |
Wire.requestFrom(address, 1); |
101 |
data = Wire.receive(); |
102 |
ret +=256 * Wire.receive(); |
102 |
Wire.endTransmission(); // stop transmitting |
103 |
Wire.endTransmission(); // stop transmitting |
103 |
|
104 |
|
104 |
lux+=data*256; |
- |
|
105 |
return lux; |
105 |
return (1000.0/pow(2.0,16)*ret); |
106 |
} |
106 |
} |
107 |
|
107 |
|
108 |
void loop() |
108 |
void loop() |
109 |
{ |
109 |
{ |
110 |
int lux=0; |
110 |
int lux=0; |
111 |
|
111 |
|
112 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
112 |
set_light_sensor(SENSE_VIS); //setup sensor for visible measuring |
113 |
led_blink(); // Delay for measurement |
113 |
led_blink(); // Delay for measurement |
114 |
Serial.print("lux="); |
114 |
Serial.print("lux="); |
115 |
Serial.println((unsigned)get_light_measurement(), DEC); |
115 |
Serial.println(get_light_measurement(),2); |
116 |
|
116 |
|
117 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
117 |
set_light_sensor(SENSE_IR); // setup sensor for infrared measuring |
118 |
led_blink(); // Delay for measurement |
118 |
led_blink(); // Delay for measurement |
119 |
Serial.print("luxIR="); |
119 |
Serial.print("luxIR="); |
120 |
Serial.println((unsigned)get_light_measurement(), DEC); // data print |
120 |
Serial.println(get_light_measurement(), 2); // data print |
121 |
} |
121 |
} |
122 |
|
122 |
|