diff --git a/inc/MicroBitLightSensor.h b/inc/MicroBitLightSensor.h index a149efa..ee85219 100644 --- a/inc/MicroBitLightSensor.h +++ b/inc/MicroBitLightSensor.h @@ -85,10 +85,10 @@ class MicroBitLightSensor * * Where each number represents a different section on the 5 x 5 matrix display. * - * @return returns a value in the range 0 - 100 where 0 is dark, and 100 + * @return returns a value in the range 0 - 255 where 0 is dark, and 255 * is very bright * - * @note currently returns a value in the range 0 - 100 where 0 is dark, and 100 + * @note currently returns a value in the range 0 - 255 where 0 is dark, and 255 * is very bright perhaps we should normalise the returned values into an SI unit! * TODO. */ diff --git a/source/MicroBitLightSensor.cpp b/source/MicroBitLightSensor.cpp index efe29ce..8777107 100644 --- a/source/MicroBitLightSensor.cpp +++ b/source/MicroBitLightSensor.cpp @@ -105,10 +105,10 @@ MicroBitLightSensor::MicroBitLightSensor() : analogTrigger() * * Where each number represents a different section on the 5 x 5 matrix display. * - * @return returns a value in the range 0 - 100 where 0 is dark, and 100 + * @return returns a value in the range 0 - 255 where 0 is dark, and 255 * is very bright * - * @note currently returns a value in the range 0 - 100 where 0 is dark, and 100 + * @note currently returns a value in the range 0 - 255 where 0 is dark, and 255 * is very bright perhaps we should normalise the returned values into an SI unit! * TODO. */ @@ -127,7 +127,11 @@ int MicroBitLightSensor::read() int inverted = (MICROBIT_LIGHT_SENSOR_MAX_VALUE - average) + MICROBIT_LIGHT_SENSOR_MIN_VALUE; - int normalised = ((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE) * 100) / (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE); + int a = 0; + + int b = 255; + + int normalised = a + ((((inverted - MICROBIT_LIGHT_SENSOR_MIN_VALUE)) * (b - a))/ (MICROBIT_LIGHT_SENSOR_MAX_VALUE - MICROBIT_LIGHT_SENSOR_MIN_VALUE)); return normalised; }