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