Sending a numerical value to the cloud

Hi,
I have an analogue value in my code of say 7856 which is loaded into a variable called 'int ValueL = analogueRead(LDRU) and I want to divide this value by 1000 so that it shows 7.856 and then publish this,
But when I do it looses all the decimal places and becomes 7, can anyone suggest a better way of doing this

Des

Dividing an integer (int ValueL) by an integer (1000) will give an integer (in you example: 7). One of those should be a float to get a float as a result.
Could you try (haven’t tested it myself):

int ValueL = analogueRead(LDRU);
Particle.publish("ValueL", String((float)ValueL/1000));

OR

int ValueL = analogueRead(LDRU);
Particle.publish("ValueL", String(ValueL/1000.0));

OR

int ValueL = analogueRead(LDRU);
float ValueLFloat = (float)ValueL/1000;
Particle.publish("ValueL", String(ValueLFloat));

Hi Koenn,
I tried your first example

int ValueL = analogueRead(LDRU);
Particle.publish(“ValueL”, String((float)ValueL/1000));

and it works perfectly !!!

Many Thanks
DES

1 Like