How to save a float variable to the eeprom

For the discussion of Arduino related topics.

Moderator: phalanx

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

How to save a float variable to the eeprom

Post by jessey » Thu Dec 14, 2017 4:42 am

This is my first post here and I'm hoping there are some arduino hobbyists here that can help me out.

Does anyone here know how to save a float variable directly to the eeprom? If so could you please have a look at the program I wrote here and hopefully you can show me how I can modify my code here to work with the other 2 variables I'm saving to the eeprom. The program I wrote displays a voltmeter to the LCD. It 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. Even though I wrote this program here I have very little experience or understanding of what’s involved in setting up a float var to be saved. I've been reading a lot of posts on the official arduino forum but there aren't too many posts there that are actually resolved and even when they are resolved the complete working code is very seldom posted. I get most of my coding experience by taking apart other peoples code in a working program to combine with my programs. That’s the nice thing about open source is that a lot of it is out there but unfortunately I can’t seem to find any working programs that are saving a float var to the eeprom at least none that I can understand what's going on.

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 perfectly but it uses up 4956 bytes of program space. Way too much...

That's ok if I'm using an Arduino Mega but I'd like to be able to have some of my programs run on an Arduino Nano which could cause problems with some bigger programs. If anyone here could show me how to do this or has a link to a program that I can use as a guide it would be greatly appreciated and I would be in your dept. Here is my code below.

Thanks
jessey


Code: Select all

 #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_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();
    Display_A_Float_Voltage_Reference();
    
    if (Volt_Alarm_Set_Point<=9){// these next 4 if statements 
    lcd.setCursor(16, 1);        // below gets rid of left over
    lcd.print(" ");}             // #'s to the display while decrementing

    if (Volt_Alarm_Set_Point<=99){
    lcd.setCursor(19, 1); 
    lcd.print(" ");}  

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

    if (Volt_Alarm_Set_Point<=99){
    lcd.setCursor(18, 2); 
    lcd.print(" ");}       

    lcd.setCursor(0, 0); 
    lcd.print(F("This is the Volt adj")); 
    lcd.setCursor(0, 1); 
    lcd.print(F("Volt Set Pnt = "));
    lcd.setCursor(15, 1); 
    lcd.print(Volt_Set_Point);  
    lcd.setCursor(0,2); 
    lcd.print(F("Voltage = "));   
    lcd.setCursor(10, 2); 
    lcd.print(vin);    
    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...

User avatar
DanV
Posts: 381
Joined: Sat Apr 25, 2015 9:29 am

Re: How to save a float variable to the eeprom

Post by DanV » Thu Dec 14, 2017 8:00 am

Wouldn't you simply replace

Code: Select all

if (Volt_Alarm_Set_Point==145){Volt_Set_Point=14.5;}
with

Code: Select all

Volt_Set_Point = Volt_Alarm_Set_Point / 10
?

User avatar
DanV
Posts: 381
Joined: Sat Apr 25, 2015 9:29 am

Re: How to save a float variable to the eeprom

Post by DanV » Thu Dec 14, 2017 8:02 am

And as far as saving your float, if the resolution of one decimal point is sufficient, multiply each reading by ten and store the integer.
As far as I know, you cannot save an actual float value to the EEPROM.

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How to save a float variable to the eeprom

Post by jremington » Thu Dec 14, 2017 12:17 pm

You can easily save a float value in EEPROM. One approach is to use a library like https://playground.arduino.cc/Code/EEPROMWriteAnything

The other is to break the float value into four bytes and write those individual bytes, one at a time. That can be done with a union, as follows:

Code: Select all

union cvt { float x; byte b[4]} val;
//example of use
val.x = 1.234;
byte b0 = val.b[0];

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Mon Dec 18, 2017 9:19 pm

Hello DanV,

Thanks for getting me to looking on the right track. I tried using your equation "Volt_Set_Point = Volt_Alarm_Set_Point / 10;" but I couldn't seem to get it to work in my program. Your equation definitely makes sense mathematically when you look at all the if statements but I just couldn't get it to display properly to the float variable and as a consequence the alarm wouldn't sound when using "if (vin <= Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_On);}". I did something wrong?

Eventually I did manage to get it working by using "Volt_Set_Point = Volt_Alarm_Set_Point * 0.1;" and it works good to displaying the Volt_Set_Point float variable while incrementing and decrementing the Volt_Alarm_Set_Point integer variable and it also does the comparison well to sound the alarm. I'm posting my new code below for you (and others) to see and maybe if you get the time possibly you could suggest how I could get your equation to work with-in the confines of the program code I wrote.

Thank you also jremington but I have no idea on how to use that library that you suggested unless I could see it being used in a relatively easy program that is within my comprehension to understand (a practicable example). As I stated in my original post here: I get most of my coding experience by taking apart other peoples code in a working program to combine with my programs.

Thanks
jessey

As far as what I've read and understand about arduino is not only for professionals and students but also for hobbyists who don't have too much comprehension of the inner workings of arduino of which I am definitely one. So please excuse me if I'm ignorant of arduino as I am looking to understand.

Code: Select all

// 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...


User avatar
DanV
Posts: 381
Joined: Sat Apr 25, 2015 9:29 am

Re: How to save a float variable to the eeprom

Post by DanV » Tue Dec 19, 2017 7:38 am

I suggested that division without examining the details.
Your variable declaration of Volt_Alarm_Set_Point as an integer could be changed to a float.
The C programming language takes integer math literally in this case and returns an integer since both the operands are integer.
Volt_Alarm_Set_Point / 10 returns an integer (they are both integers).
Multiplying by 0.1 (a literal float data type) will produce a float result.

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How to save a float variable to the eeprom

Post by jremington » Tue Dec 19, 2017 8:58 am

You don't need the library. If you read further in the link, you will see that the library just uses the built in eeprom functions.

Simple program to read/save a float value to four bytes starting at eeprom location zero.

Code: Select all

#include <avr/eeprom.h>

float x=1.234;

void setup()
{
  //read value of x from eeprom address 0 upon startup
  eeprom_read_block((void*)&x, (void*)0, sizeof(float));
    // ...
}
void loop()
{

    // if user pushes the "Save" button, save the value of x in location 0
    if (digitalRead(13) == HIGH)
      eeprom_write_block((const void*)&x, (void*)0, sizeof(float));
  //...
}

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How to save a float variable to the eeprom

Post by jremington » Tue Dec 19, 2017 9:37 am

Even simpler, for just one value use the built-in eeprom read/write float functions:

Code: Select all

#include <avr/eeprom.h>

float x=1.234;

void setup()
{
  //read value of x from eeprom address 0 upon startup
  x=eeprom_read_float((const float*)0);
    // ...
}
void loop()
{

    // if user pushes the "Save" button, save the value of x in eeprom locations 0 to 3
    if (digitalRead(13) == HIGH)
      eeprom_write_float((float *)0, x);
  //...
}

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Tue Dec 19, 2017 10:35 pm

jremington wrote:
Tue Dec 19, 2017 8:58 am
You don't need the library. If you read further in the link, you will see that the library just uses the built in eeprom functions.

Simple program to read/save a float value to four bytes starting at eeprom location zero.

Code: Select all

#include <avr/eeprom.h>

float x=1.234;

void setup()
{
  //read value of x from eeprom address 0 upon startup
  eeprom_read_block((void*)&x, (void*)0, sizeof(float));
    // ...
}
void loop()
{

    // if user pushes the "Save" button, save the value of x in location 0
    if (digitalRead(13) == HIGH)
      eeprom_write_block((const void*)&x, (void*)0, sizeof(float));
  //...
}
ok jremington,
Please do clarify.

Your saying I don't need the library? (I did find <avr/eeprom.h> at github and downloaded it)

Why do I need the #include <avr/eeprom.h> if your code uses the built in eeprom function? Isn't avr/eeprom.h the library your saying I don't need?

Please bear with me so I can sort out what's going on here...

Thanks
jessey

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How to save a float variable to the eeprom

Post by jremington » Wed Dec 20, 2017 3:13 pm

You don't need the EEPROMWriteAnything library. The functions used in the later examples are built in to the Arduino package. No need to download anything.

You do need to include the eeprom.h header file as shown in the last, simplest example, because it is not automatically included by the Arduino system (as is, for example, the math.h header file).

Relax, try things and read a lot. You just have a long way to go.

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Thu Dec 21, 2017 2:02 am

jremington wrote:
Wed Dec 20, 2017 3:13 pm
Relax, try things and read a lot. You just have a long way to go.
Thanks for the encouragement jremington. I managed to get it working and it seems to be functioning good. Unless you can see any gotchas.
So this is a good thread for anyone that is googling how to save a float variable and is easy to follow. I posted the new revised code below...

Thanks
jessey

As far as what I've read and understand about arduino is it's not only for professionals and students but
also for hobbyists who don't have too much comprehension of the inner workings of arduino of which I
am definitely one. So please excuse me if I'm ignorant of arduino as I am looking to understand.

Code: Select all

 #include <EEPROM.h>
 #include <EEPROMAnything.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;// got this code is here ---> http://www.electroschematics.com/9351/arduino-digital-voltmeter/
    float R1 = 99300.0;
    float R2 = 9970.0; 
    int value = 0;
//----------Volt Meter Code----------//

  float Volt_Set_Point=1.234;
  int a;
  int b;
  int s = 0;

 int Button_1 = 5;
 int Button_2 = 6; 
 int Button_3 = A1;
 int Button_4 = A0;
 int Relay_1 = A3;
 int Relay_2 = A2; 
 int Relay_3 = 8; 

 #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----< above >----void setup---------|//
void setup() {     

    pinMode(Relay_1, OUTPUT);
    pinMode(Relay_2, OUTPUT);
    pinMode(Relay_3, 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(" Th1s Program Uses  ")); 
    lcd.setCursor(0, 1);                 
    lcd.print(F("Read & Write To Save"));
    lcd.setCursor(0, 2);                
    lcd.print(F("Both Float & Integer"));
    lcd.setCursor(0, 3);                                    
    lcd.print(F("Variables To EEprom "));     
    delay(3500);

    lcd.clear();

    eeprom_read_block((void*)&Volt_Set_Point, (void*)0, sizeof(float));
    eeprom_read_block((void*)&a, (void*)4, sizeof(int)); 
    eeprom_read_block((void*)&b, (void*)6, sizeof(int));  

}// end of the void setup()

//|---------void setup----< above >----void setup--------|//

//|---------void loop-----< below >----void loop---------|//

void loop() {         

    delay(50);
    
  //increment & decrement Volt_Set_Point
    if (digitalRead(Button_1)==Is_Pressed 
      ||digitalRead(Button_2)==Is_Pressed)  
      {Adjust_The_Volt_Set_Point();}       

    Read_Volt_Meter();

    lcd.setCursor(0, 0); 
    lcd.print(F(" Adj Volt Set Point "));     

    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 (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(Relay_1, Is_Turned_Off);}
    if (b==1) {digitalWrite(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);            
      }                                                                // float var Volt_Set_Point is using memory locations 0, 1, 2 and 3
      eeprom_write_block((const void*)&a, (void*)4, sizeof(int));  // int var is 2 bytes long
    }                                                                 // using memory locations 4 and 5   
    

    if (digitalRead(Button_3)==Is_Pressed){
     if (b==0){b=1;}else{b=0;}
     while (digitalRead(Button_3)==Is_Pressed){
     if (b==0) {digitalWrite(Relay_1, Is_Turned_Off);}  
     if (b==1) {digitalWrite(Relay_1, Is_Turned_On);}         
     lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(10,3);lcd.print(b);            
     }
     eeprom_write_block((const void*)&b, (void*)6, sizeof(int));  // int b var is 2 bytes long
    }                                                             // a var is at #4 location   
  }// end of the void loop()                                // so b is set to 6 (4 + 2 =6)
                                                                // using memory locations 6 and 7
    //|---------void loop----< above >----void loop---------|//    

//======= Subroutine Area ===< below >=== Subroutine Area =======//

  void Adjust_The_Volt_Set_Point() 
 { s=0;                             
  // push button to increase this Alarm Set Point 
  if (digitalRead(Button_2) == Is_Pressed) 
     { s=1;                                // incrementing by 0.01; increments .10, .11, .12, .13 etc    
      if (Volt_Set_Point <= 1500)// incrementing by  0.1; increments .10, .20, .30, .40 etc
      { 
       Volt_Set_Point = Volt_Set_Point + 0.01; 
       if (Volt_Set_Point > 1500)
          {Volt_Set_Point = 1500;}
      }
     }
 // push button to decrease this Alarm Set Point
  if (digitalRead(Button_1) == Is_Pressed) 
    { s=1;
     Volt_Set_Point = Volt_Set_Point - 0.01;
     if (Volt_Set_Point <= 0)            // decrementing by 0.1; decrements .40, .30, .20, .10 etc
     {Volt_Set_Point = 0;}               // decrementing by 0.01; increments .13, .12, .11, .10 etc
    }// the s variable here insures we save once & only when a button is pressed
    if (s==1){eeprom_write_block((const void*)&Volt_Set_Point, (void*)0, sizeof(float));}
 }                                                                                                                            // float var is 4 bytes long
                                                                                                                              // using memory locations 0, 1, 2 and 3

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 * 5.00) / 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 * 5.00) changing the 5.00 
   }//------------------------------------------// to whatever your meter reads...     
   //-------------------------------Volt Meter Code-------------------------------//    
}
//======= Subroutine Area ======= Subroutine Area =======//
// end the Subroutine Area
// end of the program...

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Wed Jan 03, 2018 11:13 pm

jremington wrote:
Wed Dec 20, 2017 3:13 pm
Relax, try things and read a lot. You just have a long way to go.
Hello jremington,

First off - Happy New Year. And secondly if you have the time I'd really like to know if you see any gotchas in this code I wrote (the code in my last post above) to save a float variable to the eeprom? Have I missed anything? I would definitely value any comments you may have, negative or otherwise.

Thanks
jessey



As far as what I've read and understand about arduino is it's not only for professionals and students but
also for hobbyists who don't have any or not too much comprehension of the inner workings of arduino of which I
am definitely one. So please excuse me if I'm ignorant of arduino as I'm looking to understand.

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Fri Jan 12, 2018 12:08 pm

Happy New Year everyone,

I'm still looking for assurances that my use of read and wright to save my float variable is done right or is appropriate. If anyone that is in the know is reading this and can comment then I would certainly appreciate it.

Thanks
jessey


As far as what I've read and understand about arduino is it's not only for professionals and students but
also for hobbyists who don't have any or not too much comprehension of the inner workings of arduino of which I
am definitely one. So please excuse me if I'm ignorant of arduino as I'm looking to understand.


Code: Select all

   #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;// got this code is here ---> http://www.electroschematics.com/9351/arduino-digital-voltmeter/
   float R1 = 99300.0;
   float R2 = 9970.0; 
   int value = 0;
//----------Volt Meter Code----------//

   float Volt_Set_Point=1.234;
   int a;
   int b;
   int s = 0;

   int Button_1 = 5;
   int Button_2 = 6; 
   int Button_3 = A1;
   int Button_4 = A0;
   int Relay_1 = A3;
   int Relay_2 = A2; 
   int Relay_3 = 8; 

   #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----< above >----void setup---------|//
void setup() {     

    pinMode(Relay_1, OUTPUT);
    pinMode(Relay_2, OUTPUT);
    pinMode(Relay_3, 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(" Th1s Program Uses  "));
    lcd.setCursor(0, 1);                 
    lcd.print(F("Read & Write To Save"));
    lcd.setCursor(0, 2);                
    lcd.print(F("Both Float & Integer"));
    lcd.setCursor(0, 3);                                    
    lcd.print(F("Variables To EEprom3"));       
    delay(3500);

    lcd.clear();
  //float is declared above as: float Volt_Set_Point=1.234;  
    eeprom_read_block((void*)&Volt_Set_Point, (void*)0, sizeof(float));// uses memory locations 0, 1, 2 and 3   
    eeprom_read_block((void*)&a, (void*)4, sizeof(int));// uses memory locations 4 and 5 
    eeprom_read_block((void*)&b, (void*)6, sizeof(int));// uses memory locations 6 and 7 

    if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}// the a & b variables are read above
    if (a==1) {digitalWrite(Relay_2, Is_Turned_On);} // and these statements checks their
    if (b==0) {digitalWrite(Relay_1, Is_Turned_Off);}// status and turns on or off the
    if (b==1) {digitalWrite(Relay_1, Is_Turned_On);} // appropriate relays on start-up.

    lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4, 3);lcd.print(a); // update the Lcd
    lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(10, 3);lcd.print(b);//             

}// end of the void setup()

//|---------void setup----< above >----void setup--------|//

//|---------void loop-----< below >----void loop---------|//

void loop() {         

    delay(50);
    
  //increment & decrement the float variable Volt_Set_Point
    if (digitalRead(Button_1)==Is_Pressed 
      ||digitalRead(Button_2)==Is_Pressed)  
      {Adjust_The_Volt_Set_Point();}       

    Read_Volt_Meter();

    lcd.setCursor(0, 0); 
    lcd.print(F(" Adj Volt Set Point "));     

    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 (vin < Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_On);}   
    if (vin > Volt_Set_Point){digitalWrite(Relay_3, Is_Turned_Off);}         

    if (digitalRead(Button_4)==Is_Pressed){//-------------------------------//
    if (a==0){a=1;}else{a=0;}//---------------------------------------------// Turning on the relay in this
    while (digitalRead(Button_4)==Is_Pressed){//----------------------------// loop lets me see the results
    if (a==0) {digitalWrite(Relay_2, Is_Turned_Off);}//---------------------// of the button press right away.
    if (a==1) {digitalWrite(Relay_2, Is_Turned_On);}//----------------------//        
    lcd.setCursor(0, 3);lcd.print("A = ");lcd.setCursor(4,3);lcd.print(a);  // <--- 4th line of Lcd displays integer variable            
    }                // the float variable Volt_Set_Point is using memory locations 0, 1, 2 & 3
     eeprom_write_block((const void*)&a, (void*)4, sizeof(int));// <--- this is where we save a
    }//-------------------------------------------------// this int var a above is 2 bytes long    
                                                                // using memory locations 4 & 5

    if (digitalRead(Button_3)==Is_Pressed){//-------------------------------// Turn on the relay in this while
     if (b==0){b=1;}else{b=0;}//--------------------------------------------// loop so we see the results of
     while (digitalRead(Button_3)==Is_Pressed){//---------------------------// the button press right away.
     if (b==0) {digitalWrite(Relay_1, Is_Turned_Off);}//--------------------//
     if (b==1) {digitalWrite(Relay_1, Is_Turned_On);}//---------------------//         
     lcd.setCursor(6, 3);lcd.print("B = ");lcd.setCursor(10,3);lcd.print(b);// <--- 4th line of Lcd displays integer variable            
     }                            // the integer variable a above is using memory locations 4 & 5
     eeprom_write_block((const void*)&b, (void*)6, sizeof(int));  // <--- this is where we save b
    }                                                             // this int var b is 2 bytes long  
  }// end of the void loop()                                      // using memory locations 6 & 7 

    //|---------void loop----< above >----void loop---------|//    

//======= Subroutine Area ===< below >=== Subroutine Area =======//

  void Adjust_The_Volt_Set_Point() 
 { s=0;                             
  // push button to increase this Alarm Set Point 
  if (digitalRead(Button_2) == Is_Pressed) 
     { s=1;                         
      if (Volt_Set_Point <= 1500)
      { 
       Volt_Set_Point = Volt_Set_Point + 0.01; 
       if (Volt_Set_Point > 1500) // using 0.1; above increments by 1/10th of a volt e.g. 1.40, 1.50, 1.60, 1.70. 1.80 etc
          {Volt_Set_Point = 1500;}// using 0.01; above increments by 1/100th of a volt e.g. 1.40, 1.41, 1.42, 1.43, 1.44 etc 
      }
     }
 // push button to decrease this Alarm Set Point
  if (digitalRead(Button_1) == Is_Pressed) 
    { s=1;
     Volt_Set_Point = Volt_Set_Point - 0.01;
     if (Volt_Set_Point <= 0)// using 0.1; above decrements by 1/10th of a volt e.g. 1.40, 1.30, 1.20, 1.10, 1.00 etc
     {Volt_Set_Point = 0;}// using 0.01; above decrements by 1/100th of a volt e.g. 1.40, 1.39, 1.38, 1.37, 1.36 etc
    }// the s variable below insures we save once & only when a button is pressed
    if (s==1){eeprom_write_block((const void*)&Volt_Set_Point, (void*)0, sizeof(float));}
 }                                                             // this float var above is 4 bytes long
                                                               // using memory locations 0, 1, 2 & 3

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.78) / 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 * 5.00) changing the 5.00 
   }//----------------------------------// to whatever your meter reads...     
   //-------------------------------Volt Meter Code-------------------------------//    
}
//======= Subroutine Area ===< above >=== Subroutine Area =======//
// end the Subroutine Area
// end of the program...

jremington
Support Volunteer
Posts: 2275
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How to save a float variable to the eeprom

Post by jremington » Fri Jan 12, 2018 2:34 pm

If the program works, fine.

However, you must not write to the EEPROM very often, and CERTAINLY not every pass through loop(), as the EEPROM has a very limited number of writes.

Your program is too long and complicated for me to easily see whether you break that rule.

jessey
Posts: 11
Joined: Tue Feb 12, 2013 11:31 pm
Location: Vancouver Island, Nanaimo BC Canada

Re: How to save a float variable to the eeprom

Post by jessey » Sat Jan 13, 2018 1:26 pm

jessey wrote:
Wed Jan 03, 2018 11:13 pm
I would definitely value any comments you may have, negative or otherwise.
I guess I left myself totally open for that rushed unrealistic portrayal of my code that you left me.


jremington wrote:
Fri Jan 12, 2018 2:34 pm
Your program is too long and complicated for me to easily see whether you break that rule.
If you had even taken just a couple of minutes of your time to look at my code instead of just throwing your arms in the air and rushing to that judgment you would have clearly seen that my code is NOT writing to the eeprom every pass through loop(). The integer variables a & b are only saved once upon releasing the push buttons in the void loop(). And the float variable is also only saved once with a button press.

I would have expected better from someone with over 2000 posts in this forum.

Possibly when I get the time sometime I'll write a simpler program as an example that won't require more than a flighting view to realistically comment on.

Thanks
jessey



As far as what I've read and understand about arduino is it's not only for professionals and students but
also for hobbyists who don't have any or not too much comprehension of the inner workings of arduino of which I
am definitely one. So please excuse me if I'm ignorant of arduino as I'm looking to understand.

Post Reply