A
A
Alex Franz2019-01-31 18:38:46
Arduino
Alex Franz, 2019-01-31 18:38:46

How to remove the floating point in the temperature value from the sensor in Arduino?

Good evening!
There is a code that converts the analog value from the thermistor to resistance and then to temperature.

reading_temp = analogRead(A0); // Write thermistor data to variable
    // Convert Data to Resistance Thermistor
    reading_temp = (1023 / reading_temp) - 1;
    reading_temp = SERIESRESISTOR / reading_temp;
    // Convert Resistance to Temperature
    temp_data = reading_temp / THERMISTORNOMINAL; // (R/Ro) -> Data / Nominal Thermistor (10k)
    temp_data = log(temp_data); // ln(R/Ro) Logarifm
    temp_data /= BCOEFFICIENT; // 1/B * ln(R/Ro)
    temp_data += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
    temp_data = 1.0 / temp_data; // Inverting
    temp_data -= 273.15; // Converting to Temp *C

At some point in time, I need to send the temperature to another module via the UART bus. To do this, I will use sprintf() and then Serial.println().
sprintf doesn't support floating point numbers, and I don't want to use String because of the bad rumors about it (it seems to ruin the chip).
Question: how is it possible to remove the floating point from the temperature, so that for example the temperature +26.7 is sent as +26?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
kalapanga, 2019-01-31
@kalapanga

Nonsense about both sprintf() and String.
And most likely, you just
need Serial.println(1.23456, 0) to print "1"
0 - the number of decimal places to print.

V
vanyamba-electronics, 2019-02-02
@vanyamba-electronics

float x = 4.567;
int z = (int) x;            // z == 4

#include <math.h>
z = (int) roundf(x);        // z == 5

C
CHolfield, 2019-01-31
@CHolfield

convert the floating point number to an array of bytes and pass it serially with Serial.write(). On the other side, collect and convert back. Here is the data type to convert:

union cvt {
float val;
unsigned char b[4];
} x;

На отправителе:
x.val = 22.2;

xb - there will be an array of bytes for transmission
On the receiving side, assign xb read x.val as a number

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question