0

I'm using this library for my DS3231 which have a Atmel 24C32 on it. i have a float value which increases by 0.5, like:

27.50

28

28.50 ...

the code is like this:

float lowTemp= 10.5

float highTemp= 10.5

void setup(){
  lowTemp = i2c_eeprom.read(100);
  highTemp = i2c_eeprom.read(101);
}
void loop(){
  if ((menuswtichStatus == HIGH) && (lcdWelcome == HIGH)) {
    previousMillis = millis();
    lcd.clear();
    menuScroll++;
    lcdClear = 1;
    lcdClear2 = 1;
  }
  if (menuScroll == 1) {
    lcd.setCursor(0, 0);
    lcd.print("Temperature set:");
    lcd.setCursor(0, 1);
    lcd.print("Minimum  ");
    lcd.print(lowTemp);
    lcd.print((char)223);
    lcd.print("C ");
    if (plusSwitchstatus == HIGH) {
      lowTemp += 0.5;
    }
    if (minusSwitchstatus == HIGH) {
      lowTemp -= 0.5;
    }
  }
  if (menuScroll == 2) {
    lcd.setCursor(0, 0);
    lcd.print("Temperature set:");
    lcd.setCursor(0, 1);
    lcd.print("Maximum  ");
    lcd.print(highTemp);
    lcd.print((char)223);
    lcd.print("C ");
    if (plusSwitchstatus == HIGH) {
      highTemp += 0.5;
    }
    if (minusSwitchstatus == HIGH) {
      highTemp -= 0.5;
    }
  }
  if (menuScroll == 3) {
    lcd.setCursor(0, 0);
    lcd.print("   Saving the");
    lcd.setCursor(0, 1);
    lcd.print("   Settings...");
    delay(1000);
    i2c_eeprom.write(100, lowTemp);
    i2c_eeprom.write(101, highTemp);
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SETTINGS");
    lcd.setCursor(0, 1);
    lcd.print("     SAVED!");
    delay(2000);
    menuScroll = 0;
    lcdClear = 0;
    lcdClear2 = 0;
  }
}

i have the library and stuff up there, it's just an example of my code...

The float value is not saving(written), what's the solution?

  • 2
    multiply by two before writing as an integer or a byte – jsotola Jun 21 at 15:19
  • @jsotola i don't know how...! – newbie Jun 21 at 15:23
  • perhaps you need to be asking more basic questions about bytes, integers, floats, strings, etc. – jsotola Jun 21 at 16:45
3

You have multiple options:

  1. According to jsotola's remark: the easiest is to multiply your value by 2, store it, and after reading, divide it by 2. In one byte you can store a value from 0 to 127.5 (255 / 2), or if you use signed from -64.0 to +63.5. Of course you also can use an offset in case you know the value cannot be less than e.g. -20.
  2. If you need a higher range, you can still multiply it by 2, but store it in two bytes. Split the value in a 'high value' (value % 256) / 256, and a 'low value' (value % 256).
  3. If you want to store the value completely, than use sizeof(float) to find the length, and a for loop to write each byte, starting from the first byte of the float until the last one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.