I'm reading a pulse through pulseIn and want to convert the value into seconds. The problem is that I don't get a result like 0.00003412, but just 0. What is it I'm doing wrong?
Floats don't have enough precision to do what you want so the result is rounded to 0.
If you really need to show the value as seconds you could write a routine that printed a decimal point followed by zeros for the appropriate number of places for the result you get, followed by the decimal value of result.
Perhaps something like this pseudocode
for placeVal equal to 1000000
if result is greater than or equals placeVal
print result and exit
else print a zero and divide placeVal by 10
repeat if placeVal is greater than zero
I don't really have to show all digits, so I think I just forget about that. However, in general, I'm not able to show anything else than full integers. This example code prints out 12 and not 12.45 as I would expect. What do I have to do to print out 12.45?
float number = 12.45;
void loop()
{
Serial.print(number, DEC);
}
You're telling Serial.print to print a decimal. You're implicitly casting 12.45 to an integer, 12, which is what's being sent to the serial port.
Arduino doesn't have a native function to print a floating point to the serial port, but the code mem just linked to (which he actually posted in reply to me -- thanks again, mem) will do the job.