I have a sensor that outputs temperature data in two 8bit wide registers. Reg1 (15:8) and Reg2(7:0).
I have to connect those registers into one and make one 16bit signed quantity! Then to get the actual temperature value I have to divide this by 340 and add 36.5
Something like 16bit_SignedNumber/340+36.5
I am reading the two 8bit registers (Reg1 = RegH and Reg2 = RegL) and using this code to get the temp:
T = (string.byte(RegL) and string.byte(RegH) * 256)/340 + 36,5
It seems that is not working properly! I am getting strange values! Any idea of how I will solve this problem?
T = ((string.byte(RegL) + string.byte(RegH) * 256)/34 + 365)
print("Temperature: "..(T/10).."."..(T%10)..")
Another example or two would really help to get my brain into gear.
And I realised that I have a need to calculate lux from the output of a TSL2561 sensor.
There are several formula in the datasheet dependant on the ratio of CH1/CH2 (both 16 bit values) like this:
Lux = 0.0304*CH0 − 0.062*CH0*((CH1/CH0)1.4) -- (that is raise to the power of 1.4)
or
Lux = 0.0128*CH0 − 0.0153*CH1
Now the datasheet (and the every-bountiful adafruit example library for arduino) embody a method of doing this without floating point, but I can't quite follow all the twists and turns, and anyway, I'm sure there are more elegant ways of doing this in lua. My attempt at the simpler formula is this:
Lux=(128 * CH0 - 153 * CH1 ) / 10000
Am I on the right lines - and how would one be able to do the more complex one? datasheet is here http://www.datasheetlib.com/datasheet/867868/tsl2561_pacer-international.html?page=22#datasheet (html doesn't show calculations properly - pdf does - http://www.adafruit.com/datasheets/TSL2561.pdf
I don't know enough maths to begin to even think about how to do powers