Go Down

Topic: EEPROMAnything() beginner question. (Read 531 times) previous topic - next topic

Debuginlove0

Hello,
I just want to start off by thanking all the people who support this informative forum and i'm sorry if this question had already been answered, but as I'm still learning ,  I have a question regarding  the EEPROMAnything library,

In these two small codes that I'm running:

Code: [Select]
#include <EEPROM.h>
#include <EEPROMAnything.h>


int address =6;
int b = 1234 ;
int output = 0;

void setup(){
 
  Serial.begin(9600);


EEPROM_writeAnything ( address, b);
output= EEPROM_readAnything(address, b);
Serial.print(address);
Serial.print("\t");
Serial.print(output);
Serial.println();

}

void loop(){}

 
my Serial output looks like this : 6   "2"
int = 2 bytes ,
Is it possible if I can get it to write my data "1234" , ( higher than 255) ?

Also, in this short code,

Code: [Select]
#include <EEPROM.h>
#include <EEPROMAnything.h>

  void setup()
{
    Serial.begin(9600);

    int n = 0;

    int by = 1234;
    //Serial.println(by, HEX);
    n = EEPROM_writeAnything(0, by);
    Serial.print("Wrote bytes: ");
    Serial.println(n);
    by = 1234;
    //Serial.println(by, HEX);
    n = EEPROM_readAnything(0, by);
    Serial.print("Read bytes: ");
    Serial.println(n);
    //Serial.println(by, HEX);

    Serial.println("-------");
}
   
    void loop(){
}


I get :   Wrong bytes: 2
              Read bytes:    2

If possible , can you please enlighten me , why its not writing my values and reading  2 bytes instead ?
Thanks

michinyon

I don't understand your first problem.   An int is 2 bytes  and can have numbers between -32000 and +32000  ( whether 2 to the 15th power is precisely ).   An unsigned int can be between 0 and 65000.    An int is not limited to 255,   you are thinking of a 1 byte number.









Magicj

#2
Nov 21, 2013, 10:28 am Last Edit: Nov 21, 2013, 10:32 am by Magicj Reason: 1
The problem is that you can only write a byte to each address, which means that the highest number you can write to a single address is 255.

I have been using this code for quite a few years (not sure of the original author, sorry) which effectively writes a 2 byte integer to a specified address plus the next one - and reads the same way.

Code: [Select]
//===========================================================================================================================================================

//This function will write a 2 byte integer to the eeprom at the specified address and address + 1
void EEPROMWriteInt(int p_address, int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);

EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}

//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);

return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}


//=======================================================================================================


There are some more extensive libraries also available - you might want to have a look at http://playground.arduino.cc//Code/EEPROMex

I'm not sure if anyone has combined the functionality of the EEPROMAnything library with the ability to read/write different data formats.

Cheers

Debuginlove0

Thank you guys very much for your suggestions, and thanks @Magicj for this code,
I'm still learning how to use the EEPROMEx lib. But I was just more curious in the EEPROMAnything library., and I wanted to test it out by running a small code. The
EEPROM.h can write a single byte =0-255 and int = 2 bytes,

excuse my lame question, but In the first code,  shouldn't  it be able to write/read  a 2 byte int ( "1234" ) using the "EEPROMAnything.h"  or is the code just incorrectly written or is it just, I  can't use the eepromAnything lib to write values higher than 255 ??
Thank you

Debuginlove0

#4
Nov 22, 2013, 06:22 am Last Edit: Nov 22, 2013, 06:27 am by Debuginlove0 Reason: 1
Hi,

I think I get it know guys, since 1234 cannot be stored in one address  because 1234  is too big to be stored in a single byte, so it is stored in address 0 and address 1.
so it would look something like this,

Code:
Code: [Select]
#include <EEPROMAnything.h>

#include <EEPROM.h>
void setup(){
 
 Serial.begin(9600);
int address =6;
int b = 1234;
int output ;

EEPROM.write ( address, highByte (b));
EEPROM.write( 1, lowByte (b));

output= (EEPROM.read(address) << 8) + EEPROM.read (1);
Serial.print(address);
Serial.print("\t");
Serial.print(output);
Serial.println();

}

void loop(){}

or

Code: [Select]
#include <EEPROMAnything.h>

#include <EEPROM.h>

void setup()
{
 Serial.begin(9600);
 int a = 3333;
 int address=0;
 int output;

 EEPROM_writeAnything(address, a);
 EEPROM_readAnything(address,output);
     
 Serial.print(output);

}
void loop()
{}

?
// =================

sorry but can I ask a different question, as I'm not sure, if its more appropriate to open a new topic or just continue here, but its related to eeprom ,

using two switches, to cycle through characters on the selected spot of lcd and change positions.
Code: [Select]
#include <LiquidCrystal.h>

const int button1 = 1;
const int button2 = 10;

//char* secretCode = "0241";

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pos = 0;
int cpos1 = 0;

 String output = "----";
 
void setup()
{
 
lcd.begin(16,2);
lcd.setCursor(3, 0);
lcd.print("Enter your");
lcd.setCursor(1,1);
lcd.print("Social security");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print (output);

//Setup some buttons
pinMode (button1, INPUT);
pinMode (button2, INPUT);
//Turn the internal resistors on
digitalWrite(button1, HIGH);
digitalWrite(button2, HIGH);

}

void loop()
{
 //Change the position
 if (digitalRead(button1) == LOW)

 {
   pos ++;
   cpos1 ++;
   delay (500);
   
   if (pos >= 5 )
   {
   
    pos = 40;
    cpos1 = 0;
   }

   if (pos >= 45 )
   {
    pos = 0;
    cpos1 = 0;
   }
 }
 
 // Cycles through the characters on the selected spot
 if (digitalRead(button2) == LOW)
 {
  lcd.clear();
  lcd.print(output);
  output [pos] = (output [pos] ++);
  lcd.clear();
  lcd.print(output);
  delay (400);
  }
lcd.setCursor (cpos1, 0);
  lcd.blink();
}


But i was wondering, if it is possible to store any 4 characters I choose and  store  it in the eeprom, in which this 4 digit can only be useful/usable once , just like a coupon; can only be used once, so say I choose 0241 (B) and store it in the eeprom so like

int B= 0241;
int address= 6;
EEPROM.write ( address, highByte (B));
 EEPROM.write( 7, lowByte (B));
say,
if i choose 0241 as my 4 digits, save that into the memory and proceed to the next step. But if i reset it again and say i entered different characters like 1299 or same characters again, 0241, , it'd say something like
if pos1=1299
lcd.print("not genuine");
and cannot proceed , return to initialize
or
if B=0241
lcd.print("already been used")
cannot proceed, return.

I'm not really sure, if i can incorporate this in my above code or if it can even be done. But mainly, i'm not sure whats the best approach to do this, whether to use a while, if or for loop ?
sorry if this looks lame to you but really any ideas are welcome, thanks  :)


tylernt

There are two ways to store numbers. Actually, there are more, but let's concentrate on two.

One is binary. EEPROM stores binary in blocks of 8 bits which allows 0-255 (or -128 to 127) to be represented in one address. To store larger binary numbers, you have to spread the bits across blocks. A 16-bit 'int' requires two EEPROM blocks, a 32-bit 'long' requires four EEPROM blocks.

The other is ASCII. This is where you store each digit in a block. So 0241 would be stored as 0 in adress 0, 2 in address 1, 4 in address 2, and 1 in address 3.

Which you want to use depends on... what you want to do. :)

Binary is usually more space-efficient. ASCII is "easier" to encode and decode (for some definitions of "easier").

AWOL

Quote
There are two ways to store numbers. Actually, there are more, but let's concentrate on two.

One is binary

No, it's all binary.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

tylernt

Internally, yes. But in your C source code you can just use a single digit numbers or a single-quoted ASCII character and let the compiler make it binary for you. ;)

EEPROM.write ( address, '1');
EEPROM.write ( address, 1);

Of course, you'll need to read it back out the same way. 1 does not equal '1'.

Debuginlove0

Thanks guys, i got it now,
single byte = 0-255 aka 8bits aka not enough, if you're dealing with "int" or"long",  so more address are required for data storage.
I was wondering if my above idea, with the coupon, whether it is do-able using the eeprom approach or if there's a better apporach or do i need to use the DB.h?
thanks

tylernt


I was wondering if my above idea, with the coupon, whether it is do-able using the eeprom approach


Yes, quite do-able. In order to store a leading 0 you will need to use the ASCII approach.

Debuginlove0

Hi,
Just got a chance to go on my PC again and I just wanted to thank everyone one for their i/ps.
thanks :).

Go Up