How to change integer to float?

I have a SparkFun RedBoard (Uno) that reads 4 analog inputs and scales them to output to 4 Sparkfun Qwiic HT16K33 displays.

The Arduino sketch (from Github) is displaying integers only.
I need channel A0 in the format: -40.0 to +40.0

Thanks. Marty

#include <Wire.h>
#include <SparkFun_Alphanumeric_Display.h>

// I²C addresses for Qwiic Alphanumeric Displays
const byte displayAddresses[4] = {0x70, 0x71, 0x72, 0x73};

// Create display objects for each address
HT16K33 displays[4];

// Analog pins for each display
const byte analogPins[4] = {A0, A1, A2, A3};

// Scaling parameters for each display
struct ScaleParams
{
  float inMin;     // Minimum input voltage
  float inMax;     // Maximum input voltage
  float outMin;    // Minimum output value
  float outMax;    // Maximum output value
  char format[8];  // Format for display
};

// Scaling values for each display
ScaleParams scale[4] =
  // A0	  temp  ext  0.1 ─► 0.9v  displays -40.0 -> +40.0
  // A1	  humid ext  0.7 ─► 2.3v  displays    10 ->  80
  // A2	  humid int  0.7 ─► 2.3v  displays    10 ->  80
  // A3	  BAP        1.7 ─► 2.0v  displays   900 -> 1100
{
  // Display A0
  {0.1, 0.9, -40.0, +40.0, "%+3d"},

  // Display A1
  {0.7, 2.3, 10, 80, "%3d"},

  // Display A2
  {0.7, 2.3, 10, 80, "%3d"},

  // Display A3
  {1.5, 2.0, 900, 1100, "%3d"}
};

void setup()
{
Wire.begin();
Serial.begin(115200);

// Initialize each display
for (int i = 0; i < 4; i++)
{
 displays[i].begin(displayAddresses[i]);
 displays[i].setBrightness(0);
 displays[i].clear();
 displays[i].print(i+1);
 delay(150);
}
}

float analogToVoltage(float analogVal)
{
  return analogVal * (3.3 / 1023);
}

void loop()
{
  for (int i = 0; i < 4; i++)
  {
  float analogVal = analogRead(analogPins[i]);
  float voltage = analogToVoltage(analogVal);

  // Map voltage to scaled output
  float scaled = (voltage - scale[i].inMin) * (scale[i].outMax -
  scale[i].outMin) / (scale[i].inMax - scale[i].inMin) + scale[i].outMin;

  // Clamp output to min/max
  //if (scaled < scale[i].outMin) scaled = scale[i].outMin;
  //if (scaled > scale[i].outMax) scaled = scale[i].outMax;

  char buffer[8];
  snprintf(buffer, sizeof(buffer), scale[i].format, (int)scaled);

  displays[i].print(buffer);
}


delay(1000);
}

Because of this. You convert it to integer.

Check my HT16K33 library, it has several dedicated print functions including:

  • bool displayFloat(float f, uint8_t decimals = 3)
  • bool displayFixedPoint1(float f) displays values -99.9 .. 999.9 with 1 decimal.

See this thread about sprintf() with float values.

https://forum.arduino.cc/t/sprintf-with-float-values/937562

It's not supported on the Uno and other 8 bit avr boards. Use dtostrf().

AnalogRead returns integers, so try this approach.

int rawValue = analogRead (A0);
float value = map (rawValue, 0, 1023, -400, 400) / 10;

or, shorter,

float value = map (analogRead(A0), 0, 1023, -400, 400) / 10;

1. UNO R3 is a unipolar ADC which means it cannot convert -ve DC voltage into biary bits.

2. Let us see how to convert the signal coming from ADC-Ch0 (A0) into floating form. Connect 3.3V pin with A0-pin by a jumper wire.

unsigned int y = analogRead(A0);   //y ~= 675
float voltY = (5.0/1023.0) * y;  // voltY ~= 3.3
Serial.println(voltY, 1);   //shows: approx. 3.3V

Let's not forget that the Uno R3 doesn't have an A/D that can reliably read voltages.
It's ratiometric A/D relies on the potentially unstanle USB supply voltage.

It is not clear to me what OP's project is or does.

Golam

If you red carefully (past tense of READ) , you would see there is no negative voltages.

0.1 -> 0.9v to display -40.0 ─► +40.0

Thanks for this but my displays are 14-segment, not 7.

Who is OP?

As I undersrand OP, TO, and TP are synonyms.

You get an integral if you do that - you won’t have one decimal. You need to divide by 10.0 to get the division to be performed as a float.

Preterite is spelled read (but indeed pronounced “red”)

How did you do that??

I hovered over the OP's avatar which produced the text then took a screen shot of the area of interest.

Thanks , does not work for me.

Must be a Mac Safari thing.
Handy for discovering other things:


You can just leave the number as an integer, expressed as tenths, and go through a rather convoluted process to print that as a decimal number.

void setup() {
  Serial.begin(115200);
  int valueTenths = map (analogRead(A0), 0, 1023, -400, 400);
  char buffer[8];
  snprintf(buffer, sizeof(buffer), "%c%2d.%1d", (valueTenths < 0) ? '-' : '+', abs(valueTenths) / 10, abs(valueTenths) % 10);
  Serial.println(buffer);
}

void loop() {
}

It gets a little more complicated if you want the +/- to immediately proceed the number instead of being in a fixed position, but dtostrf() presents a similar problem where it does not show the '+' character for positive numbers.

You can only get that resolution by first setting the A/D reference to 1.1volt.

void setup() {
    analogReference(INTERNAL); // set Aref to 1V1
}

Then you must find the returned A/D values at 0V1 and 0V9 for your board and use them in the map() command.

Example:
if 0.1volt returns 110 and 0.9volt returns 920...

int rawValue = analogRead (A0);
Serial.println (rawValue); // see what values you get
float value = map (rawValue, 110, 920, -400, 400) / 10.0; // convert to -40/+40 float

Correct.
Posted before the morning coffee.
Thanks for correcting me.