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

Re: How to save a float variable to the eeprom

Post by jessey » Sat Jan 13, 2018 10:52 pm

OK here is the stripped down version of my program without the volt meter code or the push button code to implement or decrement the float variable and no lcd code to see the variables changing in value.

The 3 push buttons in the code are only there to initiate a save of one the 3 variables to the eeprom. And as you can see whenever any one of the push buttons are pressed the code goes into a while loop for de-bouncing and that variable is only saved once for whatever button is pressed in the void loop.

Pretty simple, press a button and that variable is saved. Definitely not complicated...

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>

   float Volt_Set_Point=1.234;
   int a;
   int b;

   int Button_1 = 5;
   int Button_2 = 6; 
   int Button_3 = A1;

   #define Is_Pressed  0 

void setup() {     

    pinMode(Button_1, INPUT); // on pin 5 
    pinMode(Button_2, INPUT); // on pin 6
    pinMode(Button_3, INPUT); // on pin A1
 
    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              

}// end of the void setup()

void loop() {         

    if (digitalRead(Button_1)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&Volt_Set_Point, (void*)0, sizeof(float));
    }

    if (digitalRead(Button_2)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&a, (void*)4, sizeof(int));
    }

    if (digitalRead(Button_2)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&b, (void*)6, sizeof(int));
    }
    
  }// end of the void loop()

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 11:04 pm

OK here is the stripped down version of my program without the volt meter code or the push button code to implement or decrement the float variable and no lcd code to see the variables changing in value.

The 3 push buttons in the code are only there to initiate a save of one the 3 variables to the eeprom. And as you can see whenever any one of the push buttons are pressed the code goes into a while loop for de-bouncing and that variable is only saved once for whatever button is pressed in the void loop.

Pretty simple, press a button and that variable is saved. Definitely not complicated...

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>

   float Volt_Set_Point=1.234;
   int a;
   int b;

   int Button_1 = 5;
   int Button_2 = 6; 
   int Button_3 = A1;

   #define Is_Pressed  0 

void setup() {     

    pinMode(Button_1, INPUT); // on pin 5 
    pinMode(Button_2, INPUT); // on pin 6
    pinMode(Button_3, INPUT); // on pin A1
 
    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              

}// end of the void setup()

void loop() {         

    if (digitalRead(Button_1)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&Volt_Set_Point, (void*)0, sizeof(float));
    }

    if (digitalRead(Button_2)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&a, (void*)4, sizeof(int));
    }

    if (digitalRead(Button_2)==Is_Pressed)
    {
     while (digitalRead(Button_1)==Is_Pressed)  
      {delay(100);}// debounce
      eeprom_write_block((const void*)&b, (void*)6, sizeof(int));
    }
    
  }// end of the void loop()

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

Re: How to save a float variable to the eeprom

Post by jremington » Sun Jan 14, 2018 1:56 pm

That looks OK, but there is no code to test whether it works. Does it actually work?

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 Jan 15, 2018 3:36 am

jessey wrote:
Thu Dec 21, 2017 2:02 am
I managed to get it working and it seems to be functioning good. Unless you can see any gotchas.
jremington wrote:
Fri Jan 12, 2018 2:34 pm
If the program works, fine.
Your playing with me, right?
this is almost laughable

I posted that it was working good and my complicated code proved it to me.

I was only asking if you could see any potential gotchas?

I give up now, you win...

good job



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.

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

Re: How to save a float variable to the eeprom

Post by DanV » Mon Jan 15, 2018 9:10 am

You got some balls coming here for advice / help and then disrespecting the one person offering advice.
You do know that this is a forum and not Sparkfun tech support, right?
Seriously, @jremington is a support volunteer because he achieved enough posts for the moderators to assign that monker - you don't petition for it.

And, I read through your code. Not formatted well enough to just casually glance through it and determine if all is well.
So I spent a half hour with it and it looks like it should work and no glaring issues (you're welcome).

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 Jan 15, 2018 10:43 pm

Ok I do apologize for sounding off a little and it won't happen again but it really did seem to me like I was being played.
What can I say, one thing I'll say with certainty is thank you Dan for taking the time to look at my code...

Thanks
jessey

Post Reply