3

This is my first post here and I'm hoping there are some Arduino hobbyists here to help me.

Does anyone here know how to save a float variable directly to the EEPROM? If so, could you have a look at the program I wrote and hopefully explain how it would be set up to work with the other 2 variables I'm saving to the EEPROM?

The program I wrote displays a voltmeter to the LCD. The program has an adjustable alarm set point as to when to sound an alarm when the voltage drops below a user-set alarm set point. I want to be able to declare the alarm set point as a float variable and save it directly to the EEPROM. I can't seem to find any programs that I could use as an example to try and figure out how to do that.

I did manage to get my program working to save my Volt_Alarm_Set_Point by declaring it as an int var and then comparing it to my Volt_Set_Point (declared as a float var) to 165 if statements to save the right Volt_Alarm_Set_Point value. And it works a treat but uses up 4956 bytes of program space. That's ok if I'm using an Arduino Mega but I'd like to be able to have my program run on an Arduino Nano which could cause problems with some bigger programs.

If anyone here could show me how to do this it would be greatly appreciated and I would be in your dept. Here is my code below. I don't understand how the code is suppose to be put here? Other forums are dead easy to post code but I can't figure this one out. Sorry about that... Thanks jessey

 #include <EEPROM.h> 
 #include <LiquidCrystal_I2C.h>
 LiquidCrystal_I2C lcd(0x27, 20, 4);

//----------Volt Meter Code----------//
    int analogInput = A6;                                         
    float vout = 0.0;         
    float vin = 0.0;
    float R1 = 99300.0;
    float R2 = 9970.0; 
    int value = 0;
//----------Volt Meter Code----------//

  int Volt_Alarm_Set_Point; // this works
  float Volt_Set_Point;
//  float Volt_Alarm_Set_Point; // this dosen't work
  int s = 0;
  int a = 0;
  int b = 0;

 int Button_1 = 5;
 int Button_2 = 6; 
 int Button_3 = A1;
 int Button_4 = A0;
 int Alarm_Relay_1 = A3;
 int Relay_2 = A2; 
 int Relay_3 = 8;
 int Blinking_Led = 3;// this one is ready to be used anytime  

 #define VOLT_ALARM_SET_POINT_ADDR 0
 #define A_ADDR VOLT_ALARM_SET_POINT_ADDR + sizeof(int) 
 #define B_ADDR A_ADDR + sizeof(int) 

 #define Is_Pressed  0  // used for pushbuttons
 #define Is_Turned_On 1  // used for the relays
 #define Is_Turned_Off 0 // used for the relays

 //|---------void setup---------void setup---------|//
void setup() {

    pinMode(Alarm_Relay_1, OUTPUT);
    pinMode(Relay_2, OUTPUT);
    pinMode(Relay_3, OUTPUT);
    pinMode(Blinking_Led, OUTPUT); 
    pinMode(Button_1, INPUT); // on pin 5 
    pinMode(Button_2, INPUT); // on pin 6
    pinMode(Button_3, INPUT); // on pin A1
    pinMode(Button_4, INPUT); // on pin A0
//-----------Volt Meter Code--------------//    
    pinMode(analogInput, INPUT);
//-----------Volt Meter Code--------------//

    lcd.begin();    

    lcd.setCursor(0, 0);
    lcd.print(F("  This Program Is   ")); 
    lcd.setCursor(0, 1);                 
    lcd.print(F("to try and save a   "));
    lcd.setCursor(0, 2);                
    lcd.print(F("float variable      "));
    lcd.setCursor(0, 3);                                    
    lcd.print(F("to the eeprom       "));       
    delay(500);

    lcd.clear();

    EEPROM.get(VOLT_ALARM_SET_POINT_ADDR, Volt_Alarm_Set_Point);
    EEPROM.get(A_ADDR, a); 
    EEPROM.get(B_ADDR, b); 
//  EEPROM.put( VOLT_ALARM_SET_POINT_ADDR, Volt_Alarm_Set_Point);
//  EEPROM.put(A, a); 
//  EEPROM.put(B, b);

}// end of the void setup()

//|---------void setup---------void setup---------|//
//|---------void loop---------void loop---------|//

void loop() {

    delay(50);
  //increment & decrement Volt_Alarm_Set_Point
    if (digitalRead(Button_1)==Is_Pressed 
      ||digitalRead(Button_2)==Is_Pressed)  
      {Adjust_The_Volt_Alarm_Set_Point();}  
        
    Read_Volt_Meter();
    //Multiply_By_0_Point_1;
    Display_A_Float_Voltage_Reference();
    
    lcd.setCursor(0, 0); 
    lcd.print(F("This is the Volt adj")); 
    lcd.setCursor(0, 1); 
    lcd.print(F("Volt Set Pnt = ")); 

    if (Volt_Alarm_Set_Point<=9){
    lcd.setCursor(16, 1); 
    lcd.print(" ");}
    lcd.setCursor(15, 1); 
    lcd.print(Volt_Set_Point);  

    lcd.setCursor(0,2); 
    lcd.print(F("VoltageX= "));   
    lcd.setCursor(10, 2); 
    lcd.print(vin); 

    if (Volt_Alarm_Set_Point<=9){
    lcd.setCursor(17, 2); 
    lcd.print(" ");}
    lcd.setCursor(16, 2); 
    lcd.print(Volt_Alarm_Set_Point);           

    if (vin < Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_On);}   
    if (vin > Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_Off);} 

    lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4, 3);lcd.print(a); 
    lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11, 3);lcd.print(b);         

    if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
    if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}

    if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
    if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}

    if (digitalRead(Button_4)==Is_Pressed){
      if (a==0){a=1;}else{a=0;}
      while (digitalRead(Button_4)==Is_Pressed){
      if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
      if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}        
      lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a); 
      lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11,3);lcd.print(b);            
      }
      EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
    }    

    if (digitalRead(Button_3)==Is_Pressed){
     if (b==0){b=1;}else{b=0;}
     while (digitalRead(Button_3)==Is_Pressed){
     if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
     if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}        
     lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a); 
     lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(11,3);lcd.print(b);            
     }
     EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
    }     
  }// end of the void loop()
  
//|---------void loop---------void loop---------|//
//======= Subroutine Area ======= Subroutine Area =======//
///*
void Display_A_Float_Voltage_Reference()
{
if (Volt_Alarm_Set_Point==0){Volt_Set_Point=0.00;}// this code uses 165 if statements for 4956 bytes of program space and 4 bytes of dynamic memory.
if (Volt_Alarm_Set_Point==1){Volt_Set_Point=0.10;} 
if (Volt_Alarm_Set_Point==2){Volt_Set_Point=0.20;} 
if (Volt_Alarm_Set_Point==3){Volt_Set_Point=0.30;} 
if (Volt_Alarm_Set_Point==4){Volt_Set_Point=0.40;} 
if (Volt_Alarm_Set_Point==5){Volt_Set_Point=0.50;} 
if (Volt_Alarm_Set_Point==6){Volt_Set_Point=0.60;} 
if (Volt_Alarm_Set_Point==7){Volt_Set_Point=0.70;} 
if (Volt_Alarm_Set_Point==8){Volt_Set_Point=0.80;} 
if (Volt_Alarm_Set_Point==9){Volt_Set_Point=0.90;} 
if (Volt_Alarm_Set_Point==10){Volt_Set_Point=1.00;} 

if (Volt_Alarm_Set_Point==11){Volt_Set_Point=1.10;} 
if (Volt_Alarm_Set_Point==12){Volt_Set_Point=1.20;} 
if (Volt_Alarm_Set_Point==13){Volt_Set_Point=1.30;} 
if (Volt_Alarm_Set_Point==14){Volt_Set_Point=1.40;} 
if (Volt_Alarm_Set_Point==15){Volt_Set_Point=1.50;} 
if (Volt_Alarm_Set_Point==16){Volt_Set_Point=1.60;} 
if (Volt_Alarm_Set_Point==17){Volt_Set_Point=1.70;} 
if (Volt_Alarm_Set_Point==18){Volt_Set_Point=1.80;} 
if (Volt_Alarm_Set_Point==19){Volt_Set_Point=1.90;} 
if (Volt_Alarm_Set_Point==20){Volt_Set_Point=2.00;}

if (Volt_Alarm_Set_Point==21){Volt_Set_Point=2.10;} 
if (Volt_Alarm_Set_Point==22){Volt_Set_Point=2.20;} 
if (Volt_Alarm_Set_Point==23){Volt_Set_Point=2.30;} 
if (Volt_Alarm_Set_Point==24){Volt_Set_Point=2.40;} 
if (Volt_Alarm_Set_Point==25){Volt_Set_Point=2.50;} 
if (Volt_Alarm_Set_Point==26){Volt_Set_Point=2.60;} 
if (Volt_Alarm_Set_Point==27){Volt_Set_Point=2.70;} 
if (Volt_Alarm_Set_Point==28){Volt_Set_Point=2.80;} 
if (Volt_Alarm_Set_Point==29){Volt_Set_Point=2.90;} 
if (Volt_Alarm_Set_Point==30){Volt_Set_Point=3.00;}

if (Volt_Alarm_Set_Point==31){Volt_Set_Point=3.1;} 
if (Volt_Alarm_Set_Point==32){Volt_Set_Point=3.2;} 
if (Volt_Alarm_Set_Point==33){Volt_Set_Point=3.3;} 
if (Volt_Alarm_Set_Point==34){Volt_Set_Point=3.4;} 
if (Volt_Alarm_Set_Point==35){Volt_Set_Point=3.5;} 
if (Volt_Alarm_Set_Point==36){Volt_Set_Point=3.6;} 
if (Volt_Alarm_Set_Point==37){Volt_Set_Point=3.7;} 
if (Volt_Alarm_Set_Point==38){Volt_Set_Point=3.8;} 
if (Volt_Alarm_Set_Point==39){Volt_Set_Point=3.9;} 
if (Volt_Alarm_Set_Point==40){Volt_Set_Point=4.0;}

if (Volt_Alarm_Set_Point==41){Volt_Set_Point=4.1;} 
if (Volt_Alarm_Set_Point==42){Volt_Set_Point=4.2;} 
if (Volt_Alarm_Set_Point==43){Volt_Set_Point=4.3;} 
if (Volt_Alarm_Set_Point==44){Volt_Set_Point=4.4;} 
if (Volt_Alarm_Set_Point==45){Volt_Set_Point=4.5;} 
if (Volt_Alarm_Set_Point==46){Volt_Set_Point=4.6;} 
if (Volt_Alarm_Set_Point==47){Volt_Set_Point=4.7;} 
if (Volt_Alarm_Set_Point==48){Volt_Set_Point=4.8;} 
if (Volt_Alarm_Set_Point==49){Volt_Set_Point=4.9;} 
if (Volt_Alarm_Set_Point==50){Volt_Set_Point=5.0;}

if (Volt_Alarm_Set_Point==51){Volt_Set_Point=5.1;} 
if (Volt_Alarm_Set_Point==52){Volt_Set_Point=5.2;} 
if (Volt_Alarm_Set_Point==53){Volt_Set_Point=5.3;} 
if (Volt_Alarm_Set_Point==54){Volt_Set_Point=5.4;} 
if (Volt_Alarm_Set_Point==55){Volt_Set_Point=5.5;} 
if (Volt_Alarm_Set_Point==56){Volt_Set_Point=5.6;} 
if (Volt_Alarm_Set_Point==57){Volt_Set_Point=5.7;} 
if (Volt_Alarm_Set_Point==58){Volt_Set_Point=5.8;} 
if (Volt_Alarm_Set_Point==59){Volt_Set_Point=5.9;} 
if (Volt_Alarm_Set_Point==60){Volt_Set_Point=6.0;}

if (Volt_Alarm_Set_Point==61){Volt_Set_Point=6.1;} 
if (Volt_Alarm_Set_Point==62){Volt_Set_Point=6.2;} 
if (Volt_Alarm_Set_Point==63){Volt_Set_Point=6.3;} 
if (Volt_Alarm_Set_Point==64){Volt_Set_Point=6.4;} 
if (Volt_Alarm_Set_Point==65){Volt_Set_Point=6.5;} 
if (Volt_Alarm_Set_Point==66){Volt_Set_Point=6.6;} 
if (Volt_Alarm_Set_Point==67){Volt_Set_Point=6.7;} 
if (Volt_Alarm_Set_Point==68){Volt_Set_Point=6.8;} 
if (Volt_Alarm_Set_Point==69){Volt_Set_Point=6.9;} 
if (Volt_Alarm_Set_Point==70){Volt_Set_Point=7.0;}

if (Volt_Alarm_Set_Point==71){Volt_Set_Point=7.1;} 
if (Volt_Alarm_Set_Point==72){Volt_Set_Point=7.2;} 
if (Volt_Alarm_Set_Point==73){Volt_Set_Point=7.3;} 
if (Volt_Alarm_Set_Point==74){Volt_Set_Point=7.4;} 
if (Volt_Alarm_Set_Point==75){Volt_Set_Point=7.5;} 
if (Volt_Alarm_Set_Point==76){Volt_Set_Point=7.6;} 
if (Volt_Alarm_Set_Point==77){Volt_Set_Point=7.7;} 
if (Volt_Alarm_Set_Point==78){Volt_Set_Point=7.8;} 
if (Volt_Alarm_Set_Point==79){Volt_Set_Point=7.9;} 
if (Volt_Alarm_Set_Point==80){Volt_Set_Point=8.0;}

if (Volt_Alarm_Set_Point==81){Volt_Set_Point=8.1;} 
if (Volt_Alarm_Set_Point==82){Volt_Set_Point=8.2;} 
if (Volt_Alarm_Set_Point==83){Volt_Set_Point=8.3;} 
if (Volt_Alarm_Set_Point==84){Volt_Set_Point=8.4;} 
if (Volt_Alarm_Set_Point==85){Volt_Set_Point=8.5;} 
if (Volt_Alarm_Set_Point==86){Volt_Set_Point=8.6;} 
if (Volt_Alarm_Set_Point==87){Volt_Set_Point=8.7;} 
if (Volt_Alarm_Set_Point==88){Volt_Set_Point=8.8;} 
if (Volt_Alarm_Set_Point==89){Volt_Set_Point=8.9;} 
if (Volt_Alarm_Set_Point==90){Volt_Set_Point=9.0;}

if (Volt_Alarm_Set_Point==91){Volt_Set_Point=9.1;} 
if (Volt_Alarm_Set_Point==92){Volt_Set_Point=9.2;} 
if (Volt_Alarm_Set_Point==93){Volt_Set_Point=9.3;} 
if (Volt_Alarm_Set_Point==94){Volt_Set_Point=9.4;} 
if (Volt_Alarm_Set_Point==95){Volt_Set_Point=9.5;} 
if (Volt_Alarm_Set_Point==96){Volt_Set_Point=9.6;} 
if (Volt_Alarm_Set_Point==97){Volt_Set_Point=9.7;} 
if (Volt_Alarm_Set_Point==98){Volt_Set_Point=9.8;} 
if (Volt_Alarm_Set_Point==99){Volt_Set_Point=9.9;} 
if (Volt_Alarm_Set_Point==100){Volt_Set_Point=10.0;}

if (Volt_Alarm_Set_Point==101){Volt_Set_Point=10.1;} 
if (Volt_Alarm_Set_Point==102){Volt_Set_Point=10.2;} 
if (Volt_Alarm_Set_Point==103){Volt_Set_Point=10.3;} 
if (Volt_Alarm_Set_Point==104){Volt_Set_Point=10.4;} 
if (Volt_Alarm_Set_Point==105){Volt_Set_Point=10.5;} 
if (Volt_Alarm_Set_Point==106){Volt_Set_Point=10.6;} 
if (Volt_Alarm_Set_Point==107){Volt_Set_Point=10.7;} 
if (Volt_Alarm_Set_Point==108){Volt_Set_Point=10.8;} 
if (Volt_Alarm_Set_Point==109){Volt_Set_Point=10.9;} 
if (Volt_Alarm_Set_Point==110){Volt_Set_Point=11.0;}

if (Volt_Alarm_Set_Point==111){Volt_Set_Point=11.1;} 
if (Volt_Alarm_Set_Point==112){Volt_Set_Point=11.2;} 
if (Volt_Alarm_Set_Point==113){Volt_Set_Point=11.3;} 
if (Volt_Alarm_Set_Point==114){Volt_Set_Point=11.4;} 
if (Volt_Alarm_Set_Point==115){Volt_Set_Point=11.5;} 
if (Volt_Alarm_Set_Point==116){Volt_Set_Point=11.6;} 
if (Volt_Alarm_Set_Point==117){Volt_Set_Point=11.7;} 
if (Volt_Alarm_Set_Point==118){Volt_Set_Point=11.8;} 
if (Volt_Alarm_Set_Point==119){Volt_Set_Point=11.9;} 
if (Volt_Alarm_Set_Point==120){Volt_Set_Point=12.0;}

if (Volt_Alarm_Set_Point==121){Volt_Set_Point=12.1;} 
if (Volt_Alarm_Set_Point==122){Volt_Set_Point=12.2;} 
if (Volt_Alarm_Set_Point==123){Volt_Set_Point=12.3;} 
if (Volt_Alarm_Set_Point==124){Volt_Set_Point=12.4;} 
if (Volt_Alarm_Set_Point==125){Volt_Set_Point=12.5;} 
if (Volt_Alarm_Set_Point==126){Volt_Set_Point=12.6;} 
if (Volt_Alarm_Set_Point==127){Volt_Set_Point=12.7;} 
if (Volt_Alarm_Set_Point==128){Volt_Set_Point=12.8;} 
if (Volt_Alarm_Set_Point==129){Volt_Set_Point=12.9;} 
if (Volt_Alarm_Set_Point==130){Volt_Set_Point=13.0;}

if (Volt_Alarm_Set_Point==131){Volt_Set_Point=13.1;} 
if (Volt_Alarm_Set_Point==132){Volt_Set_Point=13.2;} 
if (Volt_Alarm_Set_Point==133){Volt_Set_Point=13.3;} 
if (Volt_Alarm_Set_Point==134){Volt_Set_Point=13.4;} 
if (Volt_Alarm_Set_Point==135){Volt_Set_Point=13.5;} 
if (Volt_Alarm_Set_Point==136){Volt_Set_Point=13.6;} 
if (Volt_Alarm_Set_Point==137){Volt_Set_Point=13.7;} 
if (Volt_Alarm_Set_Point==138){Volt_Set_Point=13.8;} 
if (Volt_Alarm_Set_Point==139){Volt_Set_Point=13.9;} 
if (Volt_Alarm_Set_Point==140){Volt_Set_Point=14.0;}


if (Volt_Alarm_Set_Point==141){Volt_Set_Point=14.1;} 
if (Volt_Alarm_Set_Point==142){Volt_Set_Point=14.2;} 
if (Volt_Alarm_Set_Point==143){Volt_Set_Point=14.3;} 
if (Volt_Alarm_Set_Point==144){Volt_Set_Point=14.4;} 
if (Volt_Alarm_Set_Point==145){Volt_Set_Point=14.5;} 
if (Volt_Alarm_Set_Point==146){Volt_Set_Point=14.6;} 
if (Volt_Alarm_Set_Point==147){Volt_Set_Point=14.7;} 
if (Volt_Alarm_Set_Point==148){Volt_Set_Point=14.8;} 
if (Volt_Alarm_Set_Point==149){Volt_Set_Point=14.9;} 
if (Volt_Alarm_Set_Point==150){Volt_Set_Point=15.0;}

} // end of void Display_A_Float_Voltage_Reference()
//*/

  void Adjust_The_Volt_Alarm_Set_Point() 
 { s=0;
  // push button to increase this Alarm Set Point 
  if (digitalRead(Button_2) == Is_Pressed) 
     { s=1;   
      if (Volt_Alarm_Set_Point <= 150)
      { 
       Volt_Alarm_Set_Point = Volt_Alarm_Set_Point  + 1;// this line is using a int variable 
       if (Volt_Alarm_Set_Point > 150)
          {Volt_Alarm_Set_Point = 150;}
      }
     }
 // push button to decrease this Alarm Set Point
  if (digitalRead(Button_1) == Is_Pressed) 
    { s=1; 
     Volt_Alarm_Set_Point = Volt_Alarm_Set_Point  - 1;// this line is using a int variable 
     if (Volt_Alarm_Set_Point <= 0)
     {Volt_Alarm_Set_Point = 0;}
    }
    if (s==1){EEPROM.put( VOLT_ALARM_SET_POINT_ADDR,Volt_Alarm_Set_Point);}
 }


void Read_Volt_Meter() 
{
   //-------------------------------Volt Meter Code-------------------------------// 
   //-----------------------------------// This code reads the value at analog 
   value = analogRead(analogInput);//---// input A6 and this is where we can change 
   vout = (value * 4.72) / 1024.0;//----// the volts value to what the actual supply 
   vin = vout / (R2/(R1+R2));//---------// voltage is equal to by checking it with an  
   if (vin<0.09) {//--------------------// accurate Digital Votl Meter then entering 
   vin=0.0;//---------------------------// it here-->(value * 4.79) changing the 4.79 
   }//----------------------------------// to whatever your meter reads...     
   //-------------------------------Volt Meter Code-------------------------------//    
}
//======= Subroutine Area ======= Subroutine Area =======//
// end the Subroutine Area
// end of the program...
3
  • 3
    Looks like Volt_Set_Point = Volt_Alarm_Set_Point/10.0; :) Dec 14, 2017 at 9:51
  • 1
    Why don't you just EEPROM.put() and EEPROM.get() your float? Dec 14, 2017 at 10:50
  • @EdgarBonet - That's a new one to me. Thanks! Dec 15, 2017 at 16:12

3 Answers 3

7

Since your floating point value always uses only 1 digit after the comma, why not multiply it by 10 and put it in an integer? For a value up to 15.0 (resulting in 150 int), you can use a simple byte and store it in EEPROM directly.

Then you can get rid of all if statements. To make a better formula, you can even add rounding (so adding + 0.5 before converting to an int).

Thus:

EEPROM.put(VOLT_ALARM_SET_POINT_ADDR, (byte) (value * 10.0 + 0.5);

And to retrieve the value:

EEPROM.get(VOLT_ALARM_SET_POINT_ADDR, f);
f = (float) (f / 10.0);
0
4

@Michel Keijzers

I tried to use the code suggestion you sent but I couldn't get it set up properly to work with-in my code. I do thank you for your answer though. I did manage to figure out a way to save the value of my float variable to an integer and it works good. I'm including the working program below so if anyone ever googles the question I was asking they will find this and hopefully it'll be helpful to them because this works...

    {
        // this program uses 6986 bytes of program space
    // the old program with all the if statements used 11872 bytes
    // This program uses 4,886 less bytes of program space to accomplish the same thing
     
     #include <EEPROM.h> 
     #include <LiquidCrystal_I2C.h>
     LiquidCrystal_I2C lcd(0x27, 20, 4);
    
    //----------Volt Meter Code----------//
        int analogInput = A6;                                         
        float vout = 0.0;         
        float vin = 0.0;
        float R1 = 99300.0;
        float R2 = 9970.0; 
        int value = 0;
    //----------Volt Meter Code----------//
    
      int Volt_Alarm_Set_Point; // this works
      float Volt_Set_Point;
      int s = 0;
      int a = 0;
      int b = 0;
    
     int Button_1 = 5;
     int Button_2 = 6; 
     int Button_3 = A1;
     int Button_4 = A0;
     int Alarm_Relay_1 = A3;
     int Relay_2 = A2; 
     int Relay_3 = 8;
     int Blinking_Led = 3;// this one is ready to be used anytime  
    
     #define VOLT_ALARM_SET_POINT_ADDR 0
     #define A_ADDR VOLT_ALARM_SET_POINT_ADDR + sizeof(int) 
     #define B_ADDR A_ADDR + sizeof(int) 
    
     #define Is_Pressed  0  // used for pushbuttons
     #define Is_Not_Pressed  1  // used for pushbuttons
     #define Is_Turned_On 1  // used for the relays
     #define Is_Turned_Off 0 // used for the relays
    
     //|---------void setup---------void setup---------|//
    void setup() {
    
        pinMode(Alarm_Relay_1, OUTPUT);
        pinMode(Relay_2, OUTPUT);
        pinMode(Relay_3, OUTPUT);
        pinMode(Blinking_Led, OUTPUT); 
        pinMode(Button_1, INPUT); // on pin 5 
        pinMode(Button_2, INPUT); // on pin 6
        pinMode(Button_3, INPUT); // on pin A1
        pinMode(Button_4, INPUT); // on pin A0
    //-----------Volt Meter Code--------------//    
        pinMode(analogInput, INPUT);
    //-----------Volt Meter Code--------------//
    
        lcd.begin();    
    
        lcd.setCursor(0, 0);
        lcd.print(F(" This Program Is an ")); 
        lcd.setCursor(0, 1);                 
        lcd.print(F("example on how to   "));
        lcd.setCursor(0, 2);                
        lcd.print(F("save a float. Named "));
        lcd.setCursor(0, 3);                                    
        lcd.print(F("Save_Float_Using_Int"));       
        delay(3500);
    
        lcd.clear();
    
        EEPROM.get(VOLT_ALARM_SET_POINT_ADDR, Volt_Alarm_Set_Point);
        EEPROM.get(A_ADDR, a); 
        EEPROM.get(B_ADDR, b); 
    
    }// end of the void setup()
    
    //|---------void setup---------void setup---------|//
    //|---------void loop---------void loop---------|//
    
    void loop() {
    
        delay(50);
      //increment & decrement Volt_Alarm_Set_Point
        if (digitalRead(Button_1)==Is_Pressed 
          ||digitalRead(Button_2)==Is_Pressed)  
          {Adjust_The_Volt_Alarm_Set_Point();}
           
        Volt_Set_Point = Volt_Alarm_Set_Point*0.1;// this math displays the correct float value to the Lcd      
    
        Read_Volt_Meter();
    
        lcd.setCursor(0, 0); 
        lcd.print(F("This is the Volt adj")); 
    
        if (Volt_Set_Point<10.00)// this if statement gets rid of leftover unwanted #'s
        {lcd.setCursor(19, 1);   // while decrementing the Volt_Set_Point variable 
        lcd.print(" ");}
        lcd.setCursor(0, 1); 
        lcd.print(F("Volt Set Pnt = "));
        lcd.setCursor(15, 1); 
        lcd.print(Volt_Set_Point); // 2nd line of Lcd (displays float variable)         
    
        lcd.setCursor(0,2); 
        lcd.print(F("Voltage = "));   
        lcd.setCursor(10, 2); 
        lcd.print(vin);
        if (Volt_Alarm_Set_Point<10)//this if statement gets rid of leftover unwanted #'s
        {lcd.setCursor(17, 2);      // while decrementing the Volt_Alarm_Set_Point variable  
        lcd.print(" ");}
        if (Volt_Alarm_Set_Point<100)// this if statement gets rid of leftover unwanted #'s
        {lcd.setCursor(18, 2);       // while decrementing the Volt_Alarm_Set_Point variable 
        lcd.print("  ");}        
        lcd.setCursor(16, 2); 
        lcd.print(Volt_Alarm_Set_Point);  // 3rd line of Lcd (displays saved integer variable)   
                  
        if (vin <= Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_On);}   
        if (vin > Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_Off);} 
    
        lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4, 3);lcd.print(a); 
        lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(10, 3);lcd.print(b);         
    
        if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
        if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}
    
        if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
        if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}
    
        if (digitalRead(Button_4)==Is_Pressed){
          if (a==0){a=1;}else{a=0;}
          while (digitalRead(Button_4)==Is_Pressed){
          if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}
          if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}        
          lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a);            
          }
          EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
        }    
    
        if (digitalRead(Button_3)==Is_Pressed){
         if (b==0){b=1;}else{b=0;}
         while (digitalRead(Button_3)==Is_Pressed){
         if (b==0) {digitalWrite(Alarm_Relay_1, Is_Turned_Off);}
         if (b==1) {digitalWrite(Alarm_Relay_1, Is_Turned_On);}         
         lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(10,3);lcd.print(b);            
         }
         EEPROM.put(A_ADDR, a); EEPROM.put(B_ADDR, b);
        }     
      }// end of the void loop()
    
    //|---------void loop---------void loop---------|//
    //======= Subroutine Area ======= Subroutine Area =======//
    
      void Adjust_The_Volt_Alarm_Set_Point() 
     { s=0;
      // push button to increase this Alarm Set Point 
      if (digitalRead(Button_2) == Is_Pressed) 
         { s=1;   
          if (Volt_Alarm_Set_Point <= 150)
          { 
           Volt_Alarm_Set_Point = Volt_Alarm_Set_Point + 1;// this line is using a int variable 
           if (Volt_Alarm_Set_Point > 150)
              {Volt_Alarm_Set_Point = 150;}
          }
         }
     // push button to decrease this Alarm Set Point
      if (digitalRead(Button_1) == Is_Pressed) 
        { s=1;
         Volt_Alarm_Set_Point = Volt_Alarm_Set_Point - 1;// this line is using the int variable 
         if (Volt_Alarm_Set_Point <= 0)
         {Volt_Alarm_Set_Point = 0;}     
        }// the s variable here insures we save once & only when a button is pressed
        if (s==1){EEPROM.put( VOLT_ALARM_SET_POINT_ADDR,Volt_Alarm_Set_Point);}
     }
    
    
    void Read_Volt_Meter() 
    {
       //-------------------------------Volt Meter Code-------------------------------// 
       //-----------------------------------// This code reads the value at analog 
       value = analogRead(analogInput);//---// input A6 and this is where we can change 
       vout = (value * 4.72) / 1024.0;//----// the volts value to what the actual supply 
       vin = vout / (R2/(R1+R2));//---------// voltage is equal to by checking it with an  
       if (vin<0.09) {//--------------------// accurate Digital Votl Meter then entering 
       vin=0.0;//---------------------------// it here-->(value * 4.79) changing the 4.79 
       }//----------------------------------// to whatever your meter reads...     
       //-------------------------------Volt Meter Code-------------------------------//    
    }
    //======= Subroutine Area ======= Subroutine Area =======//
    // end the Subroutine Area
    // end of the program...
    }
-1

Here, these are universal functions you should be able to use anywhere.

Call this function by using SaveFloat(your float); eg SaveFloat (12.34); or call float yourfloat = LoadFloat();

void SaveFloat (float input) { 

  String valstring = String(input);

  int digitcount = 0;

  for (int index = 0; index < valstring.length(); index++) {

    if (String(valstring.charAt(index)) != ".") {

      String temp = String(valstring.charAt(index));
      int digit = temp.toInt();

      EEPROM.update(index + 10, digit); // Change + 10 to whatever address you want to be the starting address. eg + 11 will be EEPROM.read(11);

    } else {

      EEPROM.update(index + 10, 254); // Change + 10 to whatever address you want to be the starting address. eg + 11 will be EEPROM.read(11);

    }

    digitcount++;

  }

  EEPROM.update(9, digitcount); //This address is important, so set it to an address you arent using.

}

float LoadFloat () {            

  String output = "";

  for (int index = 0; index < EEPROM.read(//where you stored "digitcount"); index++) {

    if (EEPROM.read (index + 10) < 10) { // Change + 10 to whatever address you want to be the starting address. eg + 11 will be EEPROM.read(11);

      output += EEPROM.read (index + 10); // Change + 10 to whatever address you want to be the starting address. eg + 11 will be EEPROM.read(11);

    } else {

      output += ".";

    }

  }

  return output.toFloat();

}
1
  • Why are you doing all these useless and complex binary ↔ text ↔ BCD conversions just for storing the value? Why not store it as-is, in binary? E.g. void SaveFloat(float x){EEPROM.put(10,x);} and float LoadFloat(){float x;return EEPROM.get(10,x);} Aug 22, 2023 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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