Hello,
I visited this page Arduino Playground - EEPROMWriteAnything to try and sort out how to save to the eeprom and need some help. First off what variable name is to be changed in the example code at the bottom of the page so I can save my variable? Second the tutorial says: ''put this in a file named "EEPROMAnything.h":''. Ok that's fine and when windows asks me where I am going to save it, then what? You'd think it could be a little clearer to us people who don't know the expected! Any help will be greatly appreciated.
Thanks
jessey
First off what variable name is to be changed in the example code at the bottom of the page so I can save my variable?
The one(s) in the struct.
Second the tutorial says: ''put this in a file named "EEPROMAnything.h":''. Ok that's fine and when windows asks me where I am going to save it, then what?
You can save it in the sketch folder.
Thanks PaulS,
I'll give it a go when I get home tomorrow night.
Very Mich Appreciated
Jessey
Hey PaulS,
When I tried to save it in arduino sketches, I got a caption down below in the sketch box that says:
''The sketch name had to be modified. Sketch names can only consist
of ASCII characters and numbers (but cannot start with a number).
They should also be less less than 64 characters long.''
What is up with that? Instead of the file name being saved as EEPROMAnything.h it is now EEPROMAnything_h So it got modified from a . to an underscore?
Thanks
jessey
The simplest thing to do is to create a new tab, called EEPROMAnything.h and then paste your code into it.
You CAN rename a file that the IDE mis-handled.
Hello,
I managed to modify my first arduino program and it works good but I'd like to see how to save the variable to the eeprom. The program I modified used two push buttons, one to increment and the other button to decrement a variable to show on a LCD. The modification to the code makes the numbers go faster both up and down while the button is held down. Does anyone have an actual working program that saves a variable to the eeprom that they could share with me? I tried including the code to my program here but I guess it went over the allocated number of charters allowed. I tried to put the code in an attachment here but don't see it here whenever I PREVIEW it. I'll see it it shows up when I POST this. Any help will be greatly appreciated.
Thanks In Advance
jessey
Have you looked at the EEPROM examples in the IDE ?
if (Pause_Counter == 11) {Variable_Pause = 118;}
if (Pause_Counter == 12) {Variable_Pause = 117;}
if (Pause_Counter == 13) {Variable_Pause = 116;}
if (Pause_Counter == 14) {Variable_Pause = 115;}
if (Pause_Counter == 15) {Variable_Pause = 114;}
if (Pause_Counter == 16) {Variable_Pause = 113;}
if (Pause_Counter == 17) {Variable_Pause = 112;}
if (Pause_Counter == 18) {Variable_Pause = 111;}
if (Pause_Counter == 19) {Variable_Pause = 110;}
if (Pause_Counter == 20) {Variable_Pause = 109;}
if (Pause_Counter == 21) {Variable_Pause = 108;}
if (Pause_Counter == 22) {Variable_Pause = 107;}
if (Pause_Counter == 23) {Variable_Pause = 106;}
if ( etc etc
Most of this could be condensed into a few lines of simple arithmetic.
UKHeliBob:
Have you looked at the EEPROM examples in the IDE ?
I need to see the complete code written that is actually saving the variable to the eeprom. I am best at learning from example on a lot of this programming. Or if you could explain how to use the example code below for my application I'd surely appreciate it.
/*
* EEPROM Write
*
* Stores values read from analog input 0 into the EEPROM.
* These values will stay in the EEPROM when the board is
* turned off and may be retrieved later by another sketch.
*/
#include <EEPROM.h>
// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;
void setup()
{
}
void loop()
{
// need to divide by 4 because analog inputs range from
// 0 to 1023 and each byte of the EEPROM can only hold a
// value from 0 to 255.
int val = analogRead(0) / 4;
// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(addr, val);
// advance to the next address. there are 512 bytes in
// the EEPROM, so go back to 0 when we hit 512.
addr = addr + 1;
if (addr == 512)
addr = 0;
delay(100);
}
Which variable do you want to write to the EEPROM ?
UKHeliBob:
Which variable do you want to write to the EEPROM ?
Hey thanks ukhelibob, the variable is counter.
counter is an int so you will need to save 2 bytes to EEPROM to store its value. Something like this
#include <EEPROM.h>
void setup()
{
Serial.begin(115200);
byte address = 0;
unsigned int counter = 29346;
EEPROM.write(address, highByte(counter));
EEPROM.write(address + 1, lowByte(counter));
Serial.println((EEPROM.read(address) * 256) + EEPROM.read(address + 1));
}
void loop()
{
}
If the maximum value of counter will be 255 and it can never go negative then make it a byte instead of an int and you can write and read it with no manipulation.
However you do it minimise the times that you write to a particular EEPROM address because they have a limited life cycle and if you do it in loop() that is easily exceeded.
UKHeliBob:
However you do it minimise the times that you write to a particular EEPROM address because they have a limited life cycle and if you do it in loop() that is easily exceeded.
Could it be done by monitoring the supply voltage of the arduino and when it drops to a certain voltage (indicating a power outage) a capacitor or small rechargeable battery is brought into the circuit to allow additional time so the variable can be saved? I think I remember reading a discussion in a pic basic pro programming forum about that years ago but never tried it. Have you heard of such a method?
I don't really understand your eeprom code other than it should be placed in the void setup of the program? How do I write to it?
Thanks
jessey
I don't really understand your eeprom code other than it should be placed in the void setup of the program? How do I write to it?
My example code writes an int to the EEPROM by spitting it into 2 bytes (a high byte and a low byte) and writing them to two adjacent EEPROM addresses. It then reads the two bytes back and turns them back into an int by combining them as part of the printing. The high byte is multiplied by 256 and added to the low byte to recreate the original int value.
Could it be done by monitoring the supply voltage of the arduino and when it drops to a certain voltage (indicating a power outage) a capacitor or small rechargeable battery is brought into the circuit to allow additional time so the variable can be saved?
Sounds possible but complicated. You might just as well write to an SD card as often as you like. Unless the power goes off during the write, and there are ways in software to avoid that being a problem, there are no worries how many times you write to it.
If you are using the IDE version 1.6.2 or above, the EEPROM library now supports reading objects/variables of more than one byte.
#include <EEPROM.h>
int counter;
void loop{
int addr = 0; //Address to store value in EEPROM.
//Save to EEPROM
EEPROM.put( addr, counter );
//Retrieve from EEPROM
EEPROM.get( addr, counter );
}
void setup(){}
Here is a post I made about the updates: Official EEPROM library: support and reference.
pYro_65:
If you are using the IDE version 1.6.2 or above, the EEPROM library now supports reading objects/variables of more than one byte.
Hey pYro_65,
I just installed the new version 1.6.2 IDE and put your code snip in it and had it up and running in no time. That's great and thanks a lot I really appreciate your help. I'm including my program so others can have a look. I'd like to be able to save more variables in my program. Do you have any code samples that would show me how to accomplish that?
Thanks
jessey
AWOL:
Most of this could be condensed into a few lines of simple arithmetic.
Thanks for the tip AWOL. Could you share that with me on how to do that? I am definitely interested in learning what ever I can.
Thanks
jessey
jessey:
Hey pYro_65,I just installed the new version 1.6.2 IDE and put your code snip in it and had it up and running in no time. That's great and thanks a lot I really appreciate your help. I'm including my program so others can have a look. I'd like to be able to save more variables in my program. Do you have any code samples that would show me how to accomplish that?
Thanks
jessey
I wrote examples for EEPROM.put() and EEPROM.get(). These are available from the IDE.
File->Examples->EEPROM->eeprom_get and eeprom_put.
They show the basic usage, but is pretty much the same as what you have already seen. The major thing to take notice of is the address (btw, you do not need to create a variable if you want to hard code the value, EEPROM.put( 0, counter ); will do).
When storing multiple objects you'll need to keep track of the addresses somehow (or at least make sure they do not overlap).
An approach like this may help keep things together (the most basic method):
#include <EEPROM.h>
int someSetting1;
float someSetting2;
bool someSetting3;
#define SETTING1_ADDR 0
#define SETTING2_ADDR SETTING1_ADDR + sizeof(int)
#define SETTING3_ADDR SETTING2_ADDR + sizeof(float)
void loop{
EEPROM.put( SETTING1_ADDR, someSetting1 );
EEPROM.put( SETTING2_ADDR, someSetting2 );
EEPROM.put( SETTING3_ADDR, someSetting3 );
}
void setup(){}
I do have more examples to post in my thread mentioned previously. Keep an eye out
pYro_65:
When storing multiple objects you'll need to keep track of the addresses somehow (or at least make sure they do not overlap).
An approach like this may help keep things together (the most basic method):
Hi pYro_65,
I just put together some code (below) with lots of help from example code that's available for arduino on the net. I needed to save more than one variable and I tried your suggested code routine without any success. Any ideas on what I did wrong? Any help with this will be much appreciated. I'd like to install this voltmeter in my truck to warn me if the alternator ever starts to under or over charge the battery.
Thanks
jessey
#include <LiquidCrystal.h>
#include <EEPROM.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int Hi_Alarm_Set_Point;
float Low_Alarm_Set_Point;
bool Select_Button;
#define HI_ALARM_SET_POINT_ADDR 0
#define LOW_ALARM_SET_POINT_ADDR HI_ALARM_SET_POINT_ADDR + sizeof(int)
#define SELECT_BUTTON_ADDR LOW_ALARM_SET_POINT_ADDR + sizeof(float)
byte incrementButton = 21;
byte decrementButton = 20;
int Buzzer = 18; // piezo buzzer indicator
int resetPin = 17; // choose the input pin for a pushbutton
int val = 0; // variable for reading pin status of the pushbutton
int a = 0;
int analogInput = A8;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 10000.0; // resistance of R2 (10K) - see text!
int value = 0;
void setup()
{
pinMode(analogInput, INPUT);
pinMode(incrementButton, INPUT); //set incrementButton as INPUT
pinMode(decrementButton, INPUT); //set decrementButton as INPUT
pinMode(resetPin, INPUT); //declare pushbutton as input
lcd.begin(16, 2); //set up the LCD's number of columns and rows:
lcd.setCursor(0, 0); // this moves the cursor to the top line and first character on left side of LCD
lcd.print("Digital Voltmetr"); //print a message on the LCD
lcd.setCursor(0, 1);
lcd.print(" Version #7 ");
delay(3000); // show Version for 5 seconds
lcd.clear();
pinMode(Buzzer, OUTPUT); // sets digital pin 18 as output
} //end of setup
//********************** ________________ **********************//
//********************** void loop() Area **********************//
//********************** ________________ **********************//
void loop(){
if (Select_Button == 0){if (digitalRead(20) == LOW) {Adj_Hi_Volt_Alarm_Set();}
if (digitalRead(21) == LOW) {Adj_Hi_Volt_Alarm_Set();}}
if (Select_Button == 1){ if (digitalRead(20) == LOW) {Adj_Low_Volt_Alarm_Set();}
if (digitalRead(21) == LOW) {Adj_Low_Volt_Alarm_Set();}}
val = digitalRead(resetPin);
if (val == LOW) {
Sound_The_Button_Pres_Indicator();
if (Select_Button == 0){Select_Button = 1;}
else
{Select_Button = 0;}
while (val == LOW){
val = digitalRead(resetPin);
lcd.setCursor(0, 0); // this tells the position of the cursor and the indicates we are printing to the top line of the Lcd
lcd.print("You Can Now Adj ");
lcd.setCursor(0, 1);
if (Select_Button == 0){lcd.print(" Hi_Volt_Alarm ");}
if (Select_Button == 1){lcd.print(" Low_Volt_Alarm ");}
delay(100);
}
lcd.clear();
if (val == HIGH) { EEPROM.put( SELECT_BUTTON_ADDR, Select_Button);}
}
value = analogRead(analogInput);
vout = (value * 5.03) / 1024.0; // see text
vin = vout / (R2/(R1+R2));
if (vin<0.09) {
vin=0.0;//statement to quash undesired reading !
}
lcd.setCursor(0, 0); // this tells the position of the cursor and the indicates we are printing to the top line of the Lcd
lcd.print("Hi = ");
lcd.print(Hi_Alarm_Set_Point);
lcd.setCursor(8, 0);
lcd.print("Low = ");
lcd.print(Low_Alarm_Set_Point);
lcd.setCursor(0, 1);
lcd.print("Voltage = ");
lcd.print(vin);
delay(100);
// Sound the alarm if needed
if (vin >= Hi_Alarm_Set_Point){digitalWrite(Buzzer, HIGH);}
if (vin <= Hi_Alarm_Set_Point){digitalWrite(Buzzer, LOW);}
if (vin <= Low_Alarm_Set_Point){digitalWrite(Buzzer, HIGH);}
} //end of void loop
//********************** ________________ **********************//
//********************** Subroutines Area **********************//
//********************** ________________ **********************//
void Sound_The_Button_Pres_Indicator()
{
digitalWrite(Buzzer, HIGH); delay(1); digitalWrite(Buzzer, LOW);
}
//==================================
void Adj_Hi_Volt_Alarm_Set(){
if (digitalRead(21) == LOW){
Sound_The_Button_Pres_Indicator();
if (Hi_Alarm_Set_Point < 255){
Hi_Alarm_Set_Point = Hi_Alarm_Set_Point + 1;
if (Hi_Alarm_Set_Point > 255){Hi_Alarm_Set_Point = 255;}}}
if (digitalRead(20) == LOW){
Sound_The_Button_Pres_Indicator();
Hi_Alarm_Set_Point = Hi_Alarm_Set_Point - 1;
if (Hi_Alarm_Set_Point < 2){Hi_Alarm_Set_Point = 1;}}
EEPROM.put( HI_ALARM_SET_POINT_ADDR, Hi_Alarm_Set_Point );
}
//================================
void Adj_Low_Volt_Alarm_Set(){
if (digitalRead(21) == LOW){
Sound_The_Button_Pres_Indicator();
if (Low_Alarm_Set_Point < 255){
Low_Alarm_Set_Point = Low_Alarm_Set_Point + 1;
if (Low_Alarm_Set_Point > 255){Low_Alarm_Set_Point = 255;}}}
if (digitalRead(20) == LOW){
Sound_The_Button_Pres_Indicator();
Low_Alarm_Set_Point = Low_Alarm_Set_Point - 1;
if (Low_Alarm_Set_Point < 2){Low_Alarm_Set_Point = 1;}}
EEPROM.put( LOW_ALARM_SET_POINT_ADDR, Low_Alarm_Set_Point );
}
In your code there does not appear to be any calls to EEPROM.read(), or EEPROM.get() which you'll need to restore the data when the Arduino is powered (or when ever you need the data).
In your setup() you can read the values using get(). I also like to reserve a cell to use as a flag for whether the EEPROM data is valid.
#define HAS_DATA_BEEN_SET 0xCD
void setup(){
if( EEPROM.read( EEPROM.length() - 1 ) == HAS_DATA_BEEN_SET ){
EEPROM.get( HI_ALARM_SET_POINT_ADDR, Hi_Alarm_Set_Point );
EEPROM.get( LOW_ALARM_SET_POINT_ADDR, Low_Alarm_Set_Point );
EEPROM.get( SELECT_BUTTON_ADDR, Select_Button);
}else{
Hi_Alarm_Set_Point = 100;
Low_Alarm_Set_Point = 10;
Select_Button = false;
}
}
void onSaveData(){
EEPROM.put( HI_ALARM_SET_POINT_ADDR, Hi_Alarm_Set_Point );
EEPROM.put( LOW_ALARM_SET_POINT_ADDR, Low_Alarm_Set_Point );
EEPROM.put( SELECT_BUTTON_ADDR, Select_Button);
//add the 'data set' flag.
EEPROM.update( EEPROM.length() - 1, HAS_DATA_BEEN_SET );
}