How to display float value on LED dotmatrix display (MD Parola) ?

I am running the following sketch, I get the result perfectly on serial monitor, but LED matrix is showing only one digit not decimal values. Pls. help. Thank u.


#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 8
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10
   
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
float Kg;
const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
unsigned long t = 0;
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

void setup() {
  P.begin();
  Serial.begin(57600);
  delay(10);
  
  LoadCell.begin();
  float calibrationValue; 
  calibrationValue = 696.0; 
#if defined(ESP8266)|| defined(ESP32)
  EEPROM.begin(512); 
#endif
  EEPROM.get(calVal_eepromAdress, calibrationValue); 
 
  unsigned long stabilizingtime = 2000; 
  boolean _tare = true; 
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
   while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); 
  }
}
void loop() {
  
  static boolean newDataReady = 0;
  const int serialPrintInterval = 0;

  if (LoadCell.update()) newDataReady = true;
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
        if(i <0)
     {
        i=0.00;
     }
      newDataReady = 0;
      t = millis();
      Kg = (i/1000);
      Serial.println(Kg,2);
      P.print(Kg,2);
     }
  }
}

Please post a complete and verifiable program. There are important parts missing.

Read the forum guidelines to see how to properly post code and some good information on making a good post.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

thank u,

The following sketch is tested on UNO, and it converts a float number into ASCII and displays onto MAX7219 driven 4-8x8 LED Matrix Display Unit. You may find it useful. (The floatToASCII() function is taken from net.) The sprintf() function would be easier, but it does not work on UNO for float number.

// Including the required Arduino libraries
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 10

// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);

float number = 23.56;
char buffer[20]; // Make sure the buffer is large enough

void setup()
{
  myDisplay.begin();
  myDisplay.setIntensity(0);
  myDisplay.setTextAlignment(PA_CENTER);
  myDisplay.displayClear();
  delay(2000);
  //------------------

  // Convert the float to ASCII with 2 decimal places of precision
  floatToASCII(number, buffer, 2);

  myDisplay.print(buffer);
}

void loop() {}

void floatToASCII(float num, char *buffer, int precision)
{
  // Handle the case where the number is negative
  if (num < 0) 
  {
    *buffer++ = '-';
    num = -num;
  }

  // Extract the integer and fractional parts
  int intPart = (int)num;
  float fracPart = num - (float)intPart;

  // Convert the integer part to ASCII
  int length = snprintf(buffer, 10, "%d", intPart);
  buffer += length;

  // Add the decimal point
  *buffer++ = '.';

  // Convert the fractional part to ASCII with the specified precision
  for (int i = 0; i < precision; i++) 
  {
    fracPart *= 10;
    int digit = (int)fracPart;
    *buffer++ = '0' + digit;
    fracPart -= digit;
  }

  // Null-terminate the string
  *buffer = '\0';
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.