Format Text Box for Floating Point Numbers

Support forum for MegunoLink
Post Reply
hugh
Posts: 14
Joined: Sat Dec 31, 2016 4:59 pm

Mon Dec 11, 2017 4:42 pm

Hi,

My application sends floating point numbers to a textbox in my Interface Panel. The number is always displayed with 2 decimal places. I would like to reduce this to a single digit after the decimal point. Can this be done?

Regards,

Hugh
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 1:18 am

Hi Hugh,

Can not help you at the moment but I would also like to learn how to do that.

¿ Would you care to share the code ?

I do not have much experiencie with coding but have you checked how to cut off those decimals in the Arduino code ?

If I am not mistaken there is a math function with ABS() maybe that cuts off the decimals in your original float value ? Just guessing :-)

regards Rainer
regards Rainer

Observatorio Real de 14, SLP, MEX
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Tue Dec 12, 2017 2:45 am

Hi Hugh, this page shows how the serial.print function can be formatted to adjust the decimals.
https://www.arduino.cc/en/Serial/Print

You probably want this one specifically (but 1 instead of 0)
Serial.println(1.23456, 0) gives "1"

The next problem is that our library doesn't have an argument for adding the "0". We can look at adding this in the future but for now I suggest you modify the library to suit your application. This is the file you will need to modify on your computer.
https://github.com/Megunolink/MLP/blob/ ... ePanel.cpp

If you look at that file you should see this
void InterfacePanel::SetText(const char * ControlName, float Value)
{
SendControlHeader(ControlName, F("Text"));
m_rDestination.print(Value);
SendDataTail();
}

Thats probably the one you are using. So you would adjust the m_rDestination.print(Value) to m_rDestination.print(Value,1). Of course, this will limit all of your floats to a single decimal place.

In the future we could look to add a function like this
void InterfacePanel::SetText(const char * ControlName, float Value, int DecimalPlaces)
{
SendControlHeader(ControlName, F("Text"));
m_rDestination.print(Value,DecimalPlaces);
SendDataTail();
}

Would also need to add it to InterfacePanel.h file too.
SetText(const char * ControlName, float Value, int DecimalPlaces);

(if you're game feel free to add it and send it to us and we can integrate the fix in the next update)

Cheers
Phil
philr
Posts: 446
Joined: Mon May 26, 2014 10:58 am

Tue Dec 12, 2017 2:48 am

Another alternative (i haven't tested this but assume it would work) would be to use a numericupdown control rather than a text box and specify the number of decimal places in the property for that object.

Phil
hugh
Posts: 14
Joined: Sat Dec 31, 2016 4:59 pm

Tue Dec 12, 2017 10:04 am

Hi Rainer and Phil,

Thanks for the replies. I have swapped to a NumberUpDown control and, as you say, this works and lets me set the number of decimal places.
That said, I would like to go back to a text box for various other reasons so I will have a go at making the code changes needed. If I can get it working, I will post the changes on here.

Thanks again,

Hugh
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 4:56 pm

Hi,

Interesting. I did take a quick look into the URL you mentioned but I did not get it quite. Will take a longer look toady in the afternoon.

I am using the numeric updown box but I do not like the arrows there as I am not choosing and sending anything from the box to the Arduino. I am just receiving vales and therefore the Little arrows there are obsolete and it does not look good.

For me a User Interface apart from being practical has to look good and be simple. If I see a UI with a numeric updown box and arrows at first look at it I think the UI expects from me an Input and in this case it is not like that.

¿ is it possible to have a numeric box without arrows ? ¿ Would this not simplify a lot the sending of values to a UI ?

regards Rainer
regards Rainer

Observatorio Real de 14, SLP, MEX
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 5:33 pm

rsfoto wrote: Tue Dec 12, 2017 4:56 pm Hi,

Interesting. I did take a quick look into the URL you mentioned but I did not get it quite. Will take a longer look toady in the afternoon.

I am using the numeric updown box but I do not like the arrows there as I am not choosing and sending anything from the box to the Arduino. I am just receiving vales and therefore the Little arrows there are obsolete and it does not look good.

For me a User Interface apart from being practical has to look good and be simple. If I see a UI with a numeric updown box and arrows at first look at it I think the UI expects from me an Input and in this case it is not like that.

¿ is it possible to have a numeric box without arrows ? ¿ Would this not simplify a lot the sending of values to a UI ?

regards Rainer
Hi,

I just looked a bit more Close into the InterfacePanel.h and will test some things in the afternoon.

Maybe my idea of the nuemericbox with no arrows is not necessary. For me as a noob in all this it is sometimes hard to understand the functions or commands.

Will invest more time with palying around seeing the results ...

regards Rainer
regards Rainer

Observatorio Real de 14, SLP, MEX
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 7:54 pm

Hi,

After reading a few times the URL I think I understood it :-)

Look at my code which writes now numbers into a text box.

First code adding additional variables and using abs()

Code: Select all

#include <MegunoLink.h>

InterfacePanel Test_UI;

float volt;
float volt1;
long voltText;
long volt1Text;

void leerFR()
{
  volt = analogRead(A0);
  volt1 = volt * 4.8875;
  
  voltText = abs(volt);
  volt1Text = abs(volt1);
}


void setup()

{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()

{
  leerFR();
  Test_UI.SetNumber("A0", volt);
  Test_UI.SetNumber("CmV", volt1);
  
  Test_UI.SetText("A0_text",voltText);
  Test_UI.SetText("CmV_text",volt1Text);
  
  delay(500);
}


Then I thought it could be done easier and here is the second code. The function abs() cuts away the decimal points. I used integer but you can use also long

Code: Select all

#include <MegunoLink.h>

InterfacePanel Test_UI;

int volt;
int volt1;

void leerFR()
{
  volt = analogRead(A0);
  volt1 = abs(volt * 4.8875);
}


void setup()

{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()

{
  leerFR();
  
  Test_UI.SetNumber("A0", volt);
  Test_UI.SetNumber("CmV", volt1);
  
  Test_UI.SetText("A0_text",volt);
  Test_UI.SetText("CmV_text",volt1);
  
  delay(500);
}
MegunoLink has the following to write number values into a text box. I guess I am starting to understand MegunoLink a bit better :-)

I did not test with " ..... float value ) "

Code: Select all

void InterfacePanel::SetText(const char * ControlName, int Value)
{
  SendControlHeader(ControlName, F("Text"));
  m_rDestination.print(Value);
  SendDataTail();
}

void InterfacePanel::SetText(const char * ControlName, long Value)
{
  SendControlHeader(ControlName, F("Text"));
  m_rDestination.print(Value);
  SendDataTail();
}

void InterfacePanel::SetText(const char * ControlName, unsigned long Value)
{
  SendControlHeader(ControlName, F("Text"));
  m_rDestination.print(Value);
  SendDataTail();
}

void InterfacePanel::SetText(const char * ControlName, float Value)
{
  SendControlHeader(ControlName, F("Text"));
  m_rDestination.print(Value);
  SendDataTail();
}
regards Rainer

Observatorio Real de 14, SLP, MEX
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 8:12 pm

Hi,

A few minutes later I tested it now with " .... float Value ) "

Code: Select all

#include "MegunoLink.h"

InterfacePanel LAS;
TimePlot AnalogTomVolt;

int LedSensor;
float mVolt;
int mVoltText;

void setup()

{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()

{

  LedSensor = analogRead(A0);
  mVolt = LedSensor * 4.8875;
  mVoltText = abs(LedSensor * 4.8875);

  AnalogTomVolt.SendData("Analog", LedSensor);
  AnalogTomVolt.SendData("mVolt", mVolt);  

  LAS.SetNumber("A0", LedSensor);
  LAS.SetNumber("CmV", mVolt);
  
  LAS.SetText("A0_text", LedSensor);
  LAS.SetText("CmV_text", mVoltText);
  LAS.SetText("A0_float", mVolt);
  
  delay(100);
}
Here a screen shot with 2 numericUpDown boxes and 3 textboxes
Numeric_Text_Box.JPG
Numeric_Text_Box.JPG (175.44 KiB) Viewed 15982 times
regards Rainer

Observatorio Real de 14, SLP, MEX
rsfoto
Posts: 36
Joined: Mon Nov 20, 2017 12:09 am

Tue Dec 12, 2017 9:15 pm

Hi,

Sorry for so many posts. UNtil now I saw that the OP just wants 1 decimal number and not two. Now I understood his question.

Sorry for making up this confusin but thank anyhow I learned something else :-)

Promise that the next time I will read slower the original posts :oops:

regards Rainer
regards Rainer

Observatorio Real de 14, SLP, MEX
Post Reply