I have a moisture sensor and was trying to return a percentage back until I discovered the map command.
That being said, as I was dealing with int and floats, I have not been able to see the value I am expecting to be returned if I just do simple division.
Can you please help me understand what I am doing wrong?
int val1;
float val2;
float outputVal;
val1 = analogRead(A0);
val2 = 4095;
outputVal = val1/val2;
My Particle.variable result is this: “result”: 6.152402546588351e-270,
val/val2 will give you a percentage value, but in the range of 0-1, not 0-100 if that’s what you were expecting. The code below gets you 0-100.
The main thing is that Particle.variable takes a double (8-byte floating point) not a float (4-byte floating point). I’m surprised that compiled, but I tried it and it does and results in a very, very small value because of the extra “garbage” bytes.