Start a new topic

Printing only 1 digit

Okay with you, I'm doing a current sampling, but the display only shows the whole digits without the dots. Example: 10.50 appears on the terminal, more on the display shows 10 instead of 10.50.

 

Follows arduino code. If someone can help me.

 

#include "EmonLib.h"

#include "Nextion.h"

EnergyMonitor emon1;

 

// Electrical network voltage

Int network = 220;

Int current sensor = A1;

 

Char buffer [100] = {0};

NexNumber numonNextion = NexNumber (0,4, "n1");

 

// NexNumber n0 = NexNumber (0, 4, "n1");

NexText t0 = NexText (0, 6, "t3");

 

 

Void setup ()

{NexInit ();

  Serial.begin (115200);

  Emon1.current (current sensor, 60);

 

}

 

 

 

Void loop ()

{

  Double Irms = emon1.calcIrms (1480);

  Serial.print (Irms * 230.0); // Apparent power

  Serial.print ("");

  Serial.println (Irms); // Irms

  //n0.setValue(Irms);

// numonNextion.getValue (& mynumber);

 // Irms = mynumber;

  Memset (buffer, 0, sizeof (buffer));

 // nexLoop (nex_listen_list);

// t0.setText (buffer, Irms);

   Uint16_t valFahr;

  Memset (buffer, 0, sizeof (buffer));

  ValFahr = Irms;

  Itoa (valFahr, buffer, 10);

  T0.setText (buffer);

}



First Nextion uses integer math.


Second, this topic covered so many times in Free Chat

I am certain there is a few examples in past 30 topics.

The examples already did, but even does not appear the house of the zero point in the numerical part.

Only the complete number is left and the digit before the point does not appear!

And rightfully so ... as expected behaviour of ignoring types.


Here is an point of interest,  programming wise.

The result will be from doing exactly as it was told to do.


Integers hold integers, not floating points

  10.50 is not an integer.


So your code converts a double into an integer

  (dropping the fractional as told to do)

and then this integer value is what you pushed to t0


... doing EXACTLY as it was told to do.

But is there a solution?

programming wise?

 - there should always be a solution

 - be smarter than the machine


change your approach

 - same approach will of course result in same results

 - learn about available resources:

    - for your chosen programming language

    - for your chosen compiler

    - for your chosen MCU

- read how others have accomplished similar feats

    - adapt parts of their approach to fit your requirements


Many dependencies - you haven't disclosed

 - which you have not even identified which MCU you use

 - I assume Arduino C++ for your programming language

 - you have not identified which IDE, environment

 - you haven't identified your target goal, yeah its needed

 - you haven't presented your available options

   - senseless to give solution which requires unavailable items


But I am not going to read your MCU manual for you (don't know which one)

 - doing so increases my knowledge and does nothing for yours


I like mikroe's microPascal Pro works great for many MCUs

I also like a variety of STM32 MCUs

Yes Arduino mega. But in the 16x2 lcd display it appears normal, only in the nextion it has this fault.

Ide arduino 1.8, nextion library, nextion edit V0.43.

Specifications are not faults - they are specifications.

Nextion uses integer math - no floating point support.


So if you want to send 10.50 to Number Component

 - it would be truncated to 10 as this is the

    expected behaviour from converting a float to an integer.


You are using a Nextion side Text component (NexText)

 - as such, such truncation does not occur

 - this is at least on the right path vs NexNumber scratched out


But to get 10.50 over to the Nextion you must first send 10.50

- this has to be done from your MCU code

- this your code telling your MCU what to do has yet to be done.


Right after your line with your itoa() command try basic debugging

put the lines

  Serial.print(valFahr,DEC);

  Serial.print(", ");

  Serial.println(buffer);

See for yourself that you are not even passing 10.50 to setText()


This therefore is not Nextion with a problem, but your code

- and your code is doing ... EXACTLY as it is being told to do.

I think that if you were to also remove all Nextion from your code

and use debugging the as suggested above after your itoa() ...

  Serial.print(valFahr,DEC);

  Serial.print(", ");

  Serial.println(buffer);

You will see Nextion has absolutely nothing to do with your issue


You are simply not sending 10.50 across the TX wire



1 person likes this
Login or Signup to post a comment