diff --git a/slave/slave/lib/sensors.c b/slave/slave/lib/sensors.c --- a/slave/slave/lib/sensors.c +++ b/slave/slave/lib/sensors.c @@ -22,7 +22,11 @@ int8_t boardTemp; // Board Temperature ( int32_t ut; // Temperature from BMP085 (from i2c) int32_t up; // Pressure from BMP085 (from i2c) uint16_t humid; // Humidity (from i2c) -uint8_t light; // Lux reading (from i2c) +uint8_t lightH; // Higher byte from light sensor (from i2c) +uint8_t lightL; // Lower byte from light sensor +uint8_t exponent; // Exponent for Lux +uint8_t mantissa; // Mantissa for Lux +uint32_t lux; // Calculated Lux value int8_t batt; // Read battery voltage from ADC int16_t ac1; // The following 11 variables are the calibration values for the BMP085 @@ -194,14 +198,23 @@ void sensors_readHumid() humid = (humid / 16383) * 100; } -void sensors_readLight() +void sensors_readLux() { // FOR FIRST BYTE: - light = i2c_read(LIGHT_ADDR, 0x03); - // exponent = 8xE3 + 4xE2 + 2xE1 + E0 - // mantissa = 8xM7 + 4xM6 + 2xM5 + M4 - // light = 2^(exponent)*mantissa*0.72 - + lightH = i2c_read(LIGHT_ADDR, 0x03); + lightL = i2c_read(LIGHT_ADDR, 0x04); + + exponent = lightH; + exponent = exponent >> 4; + + lightH = lightH << 4; + mantissa = lightH | lightL; + //mantissa = mantissa << 4; + //mantissa = mantissa >> 4; + + //lux = (pow(2, exponent) * (float)(mantissa * 0.045)); + lux = (float)(pow(2,exponent) * mantissa) * 0.045; + // FOR BOTH BYTES: // light = light << 4; // light = light | (0x0F & i2c_read(LIGHT_ADDR, 0x04)); // This can be used to read in the 4 LSBs from the second register @@ -235,9 +248,9 @@ uint16_t sensors_getHumid(void) // Get return humid; } -uint8_t sensors_getLight(void) // Gets light from variable +uint32_t sensors_getLux(void) // Gets light from variable { - return light; + return lux; } //possibly uint8_t diff --git a/slave/slave/lib/sensors.h b/slave/slave/lib/sensors.h --- a/slave/slave/lib/sensors.h +++ b/slave/slave/lib/sensors.h @@ -15,14 +15,14 @@ void sensors_readSpiTemp(void); // Read void sensors_readBoardTemp(void); // Reads board temperature void sensors_readPressure(void); // Reads pressure void sensors_readHumid(void); // Reads humidity -void sensors_readLight(void); // Reads lux +void sensors_readLux(void); // Reads lux void sensors_readBatt(void); int16_t sensors_getSpiTemp(void); // Gets spi temperature from variable int8_t sensors_getBoardTemp(void); // Gets board temperature from variable int32_t sensors_getPressure(void); // Gets pressure from variable uint16_t sensors_getHumid(void); // Gets humidity from variable -uint8_t sensors_getLight(void); // Gets lux from variable +uint32_t sensors_getLux(void); // Gets lux from variable int8_t sensors_getBatt(void); // Gets battery voltage from variable uint32_t sensors_getAltitude(void); // Gets altitude from variable diff --git a/slave/slave/modules.c b/slave/slave/modules.c --- a/slave/slave/modules.c +++ b/slave/slave/modules.c @@ -125,7 +125,7 @@ sensors_readSpiTemp(); //Data Read sensors_readPressure(); //Data Read //sensors_readHumid(); //Data Read - sensors_readLight(); //Data Read + sensors_readLux(); //Data Read sensors_readBatt(); } diff --git a/slave/slave/slave.c b/slave/slave/slave.c --- a/slave/slave/slave.c +++ b/slave/slave/slave.c @@ -82,8 +82,8 @@ int main(void) io_regulateTemp(); // Gets board temperature and enables heater if below threshold - //snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Light: %u |Pressure: %lu |Altitude: %lu \r\n",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLight(),sensors_getPressure(),sensors_getAltitude()); //DEBUG - //serial0_sendString(buff); //DEBUG + snprintf(buff,128,"|ModuleID: %u |BoardTemp: %i |Millis: %lu |Lux: %lu |Pressure: %lu |Altitude: %lu \r\n",io_getModuleId(),sensors_getBoardTemp(),time_millis(),sensors_getLux(),sensors_getPressure(),sensors_getAltitude()); //DEBUG + serial0_sendString(buff); //DEBUG _delay_ms(2); // Delay to prevent the sensor loop from running again before time_millis changes led_off(0);