Decimal point

Hi
Why after decimal point I have 2 zeros for example 15.00, how to fix it ?

#include <LCD5110_Graph.h>
//LCD5110 lcd(PA4, PA3, PA2, PA0, PA1);
LCD5110 lcd(PA0, PA1, PA2, PA4, PA3);
extern unsigned char SmallFont[];

char MString3 [6]; //M

String str3; //M

float  UxValue = 0;
//int  UxValue = 0;

//int  M = 0;
float  M = 0;

#include <STM32ADC.h>
STM32ADC myADC(ADC1);
uint8 pins[] = {PA6, PA7};
const int maxSamples = 2;
uint16_t dataPoints[maxSamples];
long sum1;

void setup() {
  lcd.InitLCD();
  lcd.setFont(SmallFont);
  Serial.begin(115200);
  myADC.calibrate();
  rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_8);
  pinMode(PA6, INPUT_ANALOG);

  myADC.setSampleRate(ADC_SMPR_239_5);
  myADC.setScanMode();
  myADC.setPins(pins, 2);
  myADC.setContinuous();
  myADC.setDMA(dataPoints, 2, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
  myADC.startConversion();
}

void loop() {
  lcd.clrScr();
  lcd.update();

  float  M = UxValue;

  UxValue = sum1 / 10.00; sum1 = 0;// UxValue = analogRead(PA6);
  UxValue = map( UxValue, 0, 4095, 0, 65);

  str3 = String(M) + "°";
  str3.toCharArray(MString3, 6);

  lcd.print(MString3, 54, 0); // magnitude in #

  lcd.update();
  for (int j = 0; j < 10; j++) {
    sum1 = sum1 + dataPoints[0];

    delay(1);
  }
  Serial.println(UxValue);


}

Because you are printing a float and by default .print will show 2 decimal places. Look up in the Arduino Reference for .print how to specify the extra parameter to tell it how many decimals you want.

by default .print will show 2 decimal places. but with zeros ?

As you are not showing any examples of output you get (what do your get? what are you expecting?) then if the answer is zero then it will print zero for the decimals.

I can't help as I don't immediately see the problem. I suggest that you add some serial prints to see what the actual values are that you read and calculate.

And it's strongly suggested to stay away from String (capital S); this is specifically the case when your program grows and does a lot of String manipulation which can result in memory fragmentation and unexpected behaviour at run time.

I'm not familiar with the STM32, so not sure if the following applies to that particular processor, but the map function uses integer math so will always produce a whole number as the output:

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Simple enough to create your own function that uses floating point numbers.

serial monitor

Screen Shot 2021-01-30 at 5.36.35 AM.png

Screen Shot 2021-01-30 at 5.36.35 AM.png

david_2018:
I'm not familiar with the STM32, so not sure if the following applies to that particular processor, but the map function uses integer math so will always produce a whole number as the output:

long map(long x, long in_min, long in_max, long out_min, long out_max) {

return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}




Simple enough to create your own function that uses floating point numbers.

I think the problem is mapping, how to change that ?

Use mapf?

So if your project requires precise calculations (e.g. voltage accurate to 3 decimal places), please consider avoiding map() and implementing the calculations manually in your code yourself.

= ?

I getting closer,

 //UxValue = map( UxValue, 0, 4095, 0, 65);
  UxValue =  UxValue/10.0;

the last # is always 0

Screen Shot 2021-01-30 at 6.15.32 AM.png

Screen Shot 2021-01-30 at 6.15.32 AM.png

You should consider working in integers and only converting the result to a float if and when it is needed

done

  UxValue =  UxValue/10.05;

Thanks for support.

Th1s is what I have on serial monitor 100.65
on LCD 100.6, the last digit is missing ?

#include <LCD5110_Graph.h>
//LCD5110 lcd(PA4, PA3, PA2, PA0, PA1);
LCD5110 lcd(PA0, PA1, PA2, PA4, PA3);
extern unsigned char SmallFont[];

char MString3 [6]; //M

String str3; //M

float  UxValue = 0;
//int  UxValue = 0;

//int  M = 0;
float  M = 0;

#include <STM32ADC.h>
STM32ADC myADC(ADC1);
uint8 pins[] = {PA6, PA7};
const int maxSamples = 2;
uint16_t dataPoints[maxSamples];
long sum1;

void setup() {
  lcd.InitLCD();
  lcd.setFont(SmallFont);
  Serial.begin(115200);
  myADC.calibrate();
  rcc_set_prescaler(RCC_PRESCALER_ADC, RCC_ADCPRE_PCLK_DIV_8);
  pinMode(PA6, INPUT_ANALOG);

  myADC.setSampleRate(ADC_SMPR_239_5);
  myADC.setScanMode();
  myADC.setPins(pins, 2);
  myADC.setContinuous();
  myADC.setDMA(dataPoints, 2, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);
  myADC.startConversion();
}

void loop() {
  lcd.clrScr();
  lcd.update();

  float  M = UxValue;

  // UxValue = sum1 / 10.00; sum1 = 0;// UxValue = analogRead(PA6);
  //UxValue = map( UxValue, 0, 4095, 0, 65);

  UxValue = (sum1 / 10) / 100; sum1 = 0; // UxValue = analogRead(PA6);
  UxValue =  UxValue * 3.05;

  str3 = String(M) + "°";
  str3.toCharArray(MString3, 6);

  lcd.print(MString3, 40, 0); // magnitude in #

  lcd.update();
  for (int j = 0; j < 10; j++) {
    sum1 = sum1 + dataPoints[0];

    delay(1);
  }
  Serial.println(UxValue);
}

Screen Shot 2021-01-30 at 7.13.08 AM.png

Screen Shot 2021-01-30 at 7.13.08 AM.png

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