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?
asked Jun 21, 2019 at 14:49
-
2multiply by two before writing as an integer or a bytejsotola– jsotola2019年06月21日 15:19:19 +00:00Commented Jun 21, 2019 at 15:19
-
@jsotola i don't know how...!ElectronSurf– ElectronSurf2019年06月21日 15:23:26 +00:00Commented Jun 21, 2019 at 15:23
-
perhaps you need to be asking more basic questions about bytes, integers, floats, strings, etc.jsotola– jsotola2019年06月21日 16:45:54 +00:00Commented Jun 21, 2019 at 16:45
1 Answer 1
You have multiple options:
- 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.
- 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).
- 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.
answered Jun 21, 2019 at 15:29