Pages: [1]   Go Down
Author Topic: EEPROMAnything() beginner question.  (Read 499 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.








Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//===========================================================================================================================================================

//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
« Last Edit: November 21, 2013, 04:32:12 am by Magicj » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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:
#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  smiley

« Last Edit: November 22, 2013, 12:27:59 am by Debuginlove0 » Logged

Idaho, US
Offline Offline
Edison Member
*
Karma: 21
Posts: 1020
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Idaho, US
Offline Offline
Edison Member
*
Karma: 21
Posts: 1020
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-wink

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'.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Idaho, US
Offline Offline
Edison Member
*
Karma: 21
Posts: 1020
Special User
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: