30,23 → 30,21 |
#define LIGHT_SENSE_VIS 0 |
#define LIGHT_SENSE_IR 1 |
|
#define LIGHT_TIMING_INTERNAL_16 0b000 |
#define LIGHT_TIMING_INTERNAL_12 0b001 |
#define LIGHT_TIMING_INTERNAL_8 0b010 |
#define LIGHT_TIMING_INTERNAL_4 0b011 |
#define LIGHT_TIMING_EXTERNAL_ADC 0b100 |
#define LIGHT_TIMING_EXTERNAL_TIMER 0b101 |
#define LIGHT_INTERNAL_16 0b00000000 |
#define LIGHT_INTERNAL_12 0b00000100 |
#define LIGHT_INTERNAL_8 0b00001000 |
#define LIGHT_INTERNAL_4 0b00001100 |
#define LIGHT_EXTERNAL_ADC 0b00010000 |
#define LIGHT_EXTERNAL_TIMER 0b00010100 |
|
//#define LIGHT_AUTORANGE 0 |
|
#define LIGHT_RANGE1 0b00 |
#define LIGHT_RANGE2 0b01 |
#define LIGHT_RANGE3 0b10 |
#define LIGHT_RANGE4 0b11 |
#define LIGHT_RANGE1 0b00000000 |
#define LIGHT_RANGE2 0b00000001 |
#define LIGHT_RANGE3 0b00000010 |
#define LIGHT_RANGE4 0b00000011 |
|
char filename[13]; |
int data = 0; |
int light_sensor_setup; |
|
void setup() |
{ |
90,6 → 88,12 |
delay(500); |
} |
|
/*int light_sensor_write( unsigned int data, unsigned int address) |
{ |
} |
*/ |
|
|
int set_light_sensor(int en, int mode, int light, int res, int range) |
{ |
int command=0; |
110,7 → 114,6 |
Wire.beginTransmission(address); |
Wire.send(0x00); // sends address (command register) |
Wire.endTransmission(); // stop transmitting |
|
// verify written command byte |
Wire.beginTransmission(address); |
Wire.requestFrom(address, 1); |
119,17 → 122,18 |
Serial.println("Error in sensor setting"); |
return 4; |
} |
Wire.endTransmission(); // stop transmitting |
light_sensor_setup=command; |
Serial.println(command, BIN); |
|
Wire.endTransmission(); // stop transmitting |
return 0; |
} |
|
float get_light_measurement() |
{ |
int ret=0; |
unsigned int ret=0; |
unsigned int setting=0; // variable to storage readed settings |
|
unsigned long int resolution; // parsed ADC bit resolution |
unsigned int range; // parsed measurement range |
|
// Connect to device and set register address |
Wire.beginTransmission(address); |
Wire.send(0x01); // sends address of LSB reagister |
152,7 → 156,55 |
ret +=256 * Wire.receive(); |
Wire.endTransmission(); // stop transmitting |
|
return (64000.0/pow(2.0,16)*ret); |
|
// Connect to device and set register address |
Wire.beginTransmission(address); |
Wire.send(0x00); // sends address (command register) |
Wire.endTransmission(); // stop transmitting |
// get sensor setting |
Wire.beginTransmission(address); |
Wire.requestFrom(address, 1); |
setting = Wire.receive(); |
Wire.endTransmission(); // stop transmitting |
|
switch (setting & 0b00011100) // determine ADC resolution |
{ |
case LIGHT_INTERNAL_16: |
resolution=65536; |
break; |
|
case LIGHT_INTERNAL_12: |
resolution=4096; |
break; |
|
case LIGHT_INTERNAL_8: |
resolution=256; |
break; |
|
case LIGHT_INTERNAL_4: |
resolution=16; |
break; |
} |
|
switch (setting & 0b00000011) // determine measurement range |
{ |
case LIGHT_RANGE1: |
range=1000; |
break; |
|
case LIGHT_RANGE2: |
range=4000; |
break; |
|
case LIGHT_RANGE3: |
range=16000; |
break; |
|
case LIGHT_RANGE4: |
range=64000; |
break; |
} |
return ((float)range/resolution*ret); // calculate output value |
} |
|
void loop() |
161,13 → 213,13 |
float luxIR=0; |
float time; |
|
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); //setup sensor for visible measuring |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_VIS, LIGHT_INTERNAL_16, LIGHT_RANGE2); //setup sensor for visible measuring |
led_blink(); // Delay for measurement |
luxVIS=get_light_measurement(); |
|
time=millis()/1000.0; |
|
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_TIMING_INTERNAL_16, LIGHT_RANGE4); // setup sensor for infrared measuring |
set_light_sensor(LIGHT_ENABLE, LIGHT_ONETIME, LIGHT_SENSE_IR, LIGHT_INTERNAL_16, LIGHT_RANGE2); // setup sensor for infrared measuring |
led_blink(); // Delay for measurement |
luxIR=get_light_measurement(); |
|