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