Spirik / GEM

Good Enough Menu for Arduino
GNU Lesser General Public License v3.0
239 stars 36 forks source link

Problem with floats #51

Closed baettigp closed 4 months ago

baettigp commented 2 years ago

Hi, thank you very much for your library. I am trying to implement it in a small project.

-I have problems setting a float in the menu. I would like to set the boiler temperature between 85 and 96 degrees C in 0.1 degree steps So I call it as follows:

float BoilerTemp =92.0;

void validateBoilerTemp();
GEMItem menuItemBoilerTemp("Temp degC:", BoilerTemp, validateBoilerTemp);

  menuPageSettings.addMenuItem(menuItemBoilerTemp);

void validateBoilerTemp() {

  // Check if mtime variable is within allowable range (i.e. 10..60)
  if (BoilerTemp < 85.0) {
    BoilerTemp = 85.0;
  }
  if (BoilerTemp > 96.0) {
    BoilerTemp = 96.0;
  }

When I go into the settings menu, the line reads "Temp degC 882.7" or after I enter the second time "Temp degC -242.0", far outside of the validated range from 85.0 to 96.0.

If I change it, sometimes it sets to 85, but as soon as I leave and reenter the menu, it's back at 882.7.

-Btw, How can I call the method setPrecision for floats? At the moment I manually set it to 1 digit in config.h, but I don't need all the floatsI want to represent only with 1 digits

Thank you very much for your help, if you need any more info, code, let me know.

Spirik commented 2 years ago

Hello! Thank you for using GEM!

What platform do you use? Just tried the following on Arduino UNO (with Adafruit GFX library) and it works:

float numberFloat = 92.0;
void validateFloat();
GEMItem menuItemFloat("Float:", numberFloat, validateFloat);

...

menuPageMain.addMenuItem(menuItemFloat);
menuItemFloat.setPrecision(1); // Set precision

...

void validateFloat() {
  // Check if numberFloat variable is within allowable range (i.e. >= 85.0 and <= 96.0)
  if (numberFloat < 85.0) {
    numberFloat = 85.0;
  }
  if (numberFloat > 96.0) {
    numberFloat = 96.0;
  }
  // Print numberFloat variable to Serial
  Serial.print("numberFloat set: ");
  Serial.println(numberFloat);
}

Also note call to menuItemFloat.setPrecision(1); for setting precision.

baettigp commented 2 years ago

Hi AlexanderThank you very much! I am using an Arduino due. Might the implementation of sprtof be different? I remember reading something a long time ago.Best regards, Pio--Sent from my Android phone with GMX Mail. Please excuse my brevity.On 6/6/22, 9:36 AM Alexander Spiridonov @.***> wrote:

Hello! Thank you for using GEM! What platform do you use? Just tried the following on Arduino UNO (with Adafruit GFX library) and it works:

float numberFloat = 92.0; void validateFloat(); GEMItem menuItemFloat("Float:", numberFloat, validateFloat);

...

menuPageMain.addMenuItem(menuItemFloat); menuItemFloat.setPrecision(1); // Set precision

...

void validateFloat() { // Check if numberFloat variable is within allowable range (i.e. >= 85.0 and <= 96.0) if (numberFloat < 85.0) { numberFloat = 85.0; } if (numberFloat > 96.0) { numberFloat = 96.0; } // Print numberFloat variable to Serial Serial.print("numberFloat set: "); Serial.println(numberFloat); }

Also note call to menuItemFloat.setPrecision(1); for setting precision. —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

Spirik commented 2 years ago

Arduino Due uses Cortex-M3 CPU and so indeed doesn't support dtostrf by default. However, in GEM it is included explicitly for SAM (like Arduino Due) and SAMD (like Arduino Zero) boards, see here for example. And since you don't get any error warnings regarding dtostrf not being declared, presumably it is included successfully.

Unfortunately I don't have Arduino Due at hand to test it myself, however I've tested this example on Arduino Zero (Cortex-M0+ based SAMD board) and it works.

I may suggest writing simple sketch with only one float variable (for testing purposes) and probably try compiling it for different board (if you have any).