the counter restart when the address reach 256

Hello,

My code will saves data even there is no power so i used EEPROM.. but my porblem is.. when the counter reach the 256.. the counter reset to zero.. here is my codes.. i need a counter that can store up to 600.. thank you..

#include <EEPROM.h>


int addr;
int address;
const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
int counter=0;
byte value;

void setup()
{
  pinMode(button, INPUT);
  pinMode(clearcount, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
    addr=EEPROM.read(address);
  
  
}


void loop()
{
delay(1000);
state= digitalRead(button);
if(state==HIGH)
{
delay(100);
  
  counter= EEPROM.read(addr)+1;
  
  EEPROM.write(addr, counter);
  Serial.println("WRITE");
Serial.print(addr);
  Serial.print("\t");
  Serial.print(counter, DEC);
  Serial.println("WRITE END ");
  Serial.println();
  
    // the EEPROM, so go back to 0 when we hit 512.
 
}
else if (state==LOW)
{
    
}
clearcount= digitalRead(clearcounter);
if(clearcount==HIGH)
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
    
  // turn the LED on when we're done
  digitalWrite(13, HIGH);
    Serial.println("Clear");
}

  
}

  
  // advance to the next address.  there are 512 bytes in

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

  counter= EEPROM.read(addr)+1;
 
  EEPROM.write(addr, counter);

Have you looked at the documentation for EEPROM.read() and EEPROM.write()? If not, you really should.

If you have, you should look again. Pay particular attention to the TYPE of data you can store in EEPROM. Hint: it isn't int.

If you need to store a value that can exceed 255, you MUST use more than one address.

Thank you for your reply..

is the type of my variable counter should be byte?

how can have two address??how to store more than 256?

You could use the highByte() and lowByte() functions to split an int into two bytes, then store those in eeprom.

http://arduino.cc/en/Reference/HighByte
http://arduino.cc/en/Reference/LowByte

do you have any link or tutorial about high byte and low byte...and how to split those int into two bytes..I'm sorry i'm a newbie in arduino programming..thank you

is the type of my variable counter should be byte?

Not if you want to store numbers larger than 255 in it.

how can have two address?

Use the links that dxw00d provided to split the value into two bytes, and call EEPROM.read() and EEPROM.write() twice.

To recreate the int from the two bytes, you need to know which is the high byte and which is the low byte. Store the returned values in ints, even though they are byte sized. Multiply the high byte by 256, and add the low byte to get the original int.

what should be the variable type of my counter?

i have updated my codes and am i doing right?

still when i reach the counter from 0-255 my count return to 0 again...thank you

#include <EEPROM.h>


int addr;
int address;
const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
byte counter;
byte value;

void EEPROMWriteInt(int address, int counter)
	{
	byte lowByte = ((counter >> 0) & 0xFF);
	byte highByte = ((counter >> 8) & 0xFF);

	EEPROM.write(address, lowByte);
	EEPROM.write(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 address)
	{
	byte lowByte = EEPROM.read(address);
	byte highByte = EEPROM.read(address + 1);

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



void setup()
{
  pinMode(button, INPUT);
  pinMode(clearcount, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
   // addr=EEPROM.read(address);
 
  
}


void loop()
{
delay(1);
state= digitalRead(button);
if(state==HIGH)
{
delay(1);
  
   counter= EEPROMReadInt(address)+1;
  
  
  EEPROM.write(address, counter);
  Serial.println("WRITE");
Serial.print(address);
  Serial.print("\t");
  Serial.print(counter, DEC);
  Serial.println("WRITE END ");
  Serial.println();
  
    // the EEPROM, so go back to 0 when we hit 512.
 
}
else if (state==LOW)
{
    
}
clearcount= digitalRead(clearcounter);
if(clearcount==HIGH)
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
    
  // turn the LED on when we're done
  digitalWrite(13, HIGH);
    Serial.println("Clear");
}


  
}




 

  
  // advance to the next address.  there are 512 bytes in

No, that's not right.

Something like...

void EEPROMWriteInt(int address, int counter)
{
  byte counterLow = lowByte(counter);
  byte counterHigh = highByte(counter);

  EEPROM.write(address, counterLow);
  EEPROM.write(address + 1, counterHigh);
}

unsigned int EEPROMReadInt(int address)
{
  int lowByte = EEPROM.read(address);
  int highByte = EEPROM.read(address + 1);

  int w = highByte * 256 + lowByte;
  return w;
}

@dxw00d

i followed your codes but still it gives me this output

WRITE
0 253WRITE END

WRITE
0 254WRITE END

WRITE
0 255WRITE END

WRITE
0 0WRITE END

WRITE
0 1WRITE END

after 255 the counter backs to 0...

is there any problem with my codes????

thank you for your reply

#include <EEPROM.h>



const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
byte counter;
byte address;


void EEPROMWriteInt(int address, int counter)
	{
	 byte counterLow = lowByte(counter);
  byte counterHigh = highByte(counter);
	 EEPROM.write(address, counterLow);
  EEPROM.write(address + 1, counterHigh);
	}

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

  int w = highByte * 256 + lowByte;
  return w;
}



void setup()
{
  pinMode(button, INPUT);
  pinMode(clearcount, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
   // addr=EEPROM.read(address);
 
  
}


void loop()
{
delay(1);
state= digitalRead(button);
if(state==HIGH)
{
delay(1);
  
   counter= EEPROMReadInt(address)+1;
  
  EEPROMWriteInt(address,counter);
  //EEPROM.write(address, counter);
  
  Serial.println("WRITE");
Serial.print(address);
  Serial.print("\t");
  Serial.print(counter, DEC);
  Serial.println("WRITE END ");
  Serial.println();
  
    // the EEPROM, so go back to 0 when we hit 512.er
 
}
else if (state==LOW)
{
    
}
clearcount= digitalRead(clearcounter);
if(clearcount==HIGH)
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
    
  // turn the LED on when we're done
  digitalWrite(13, HIGH);
    Serial.println("Clear");
}


  
}




 

  
  // advance to the next address.  there are 512 bytes in

counter needs to be an int, not a byte.

@ dxW00d

yes,, i changed that variable type already and do the low and high byte fuction..but still the counter backs to 0 when i reach 256...

Post your whole sketch again.

Here is my updated sketch..please let me know why my counter returns to 0 ..thank you

#include <EEPROM.h>



const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
int counter;
byte address;


void EEPROMWriteInt(int address, int counter)
	{
	 byte counterLow = lowByte(counter);
  byte counterHigh = highByte(counter);
  
  
 
	 EEPROM.write(address, counterLow);
  EEPROM.write(address + 1, counterHigh);
  

	}

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

  int w = highByte * 256 + lowByte;
  return w;
  
  
  
  
}



void setup()
{
  pinMode(button, INPUT);
  pinMode(clearcount, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  
 // EEPROMReadInt(address);
//  EEPROMWriteInt(address,counter);
   // addr=EEPROM.read(address);
   
 
  
}


void loop()
{
delay(1);
state= digitalRead(button);
if(state==HIGH)
{
delay(1);
  
   counter= EEPROMReadInt(address)+1;
  
  //EEPROMWriteInt(address,counter);
 EEPROM.write(address, counter);
  
  Serial.println("WRITE");
Serial.print(address);
  Serial.print("\t");
  Serial.print(counter, DEC);
  Serial.println("WRITE END ");
  Serial.println();
  
  
  
 
  
  
   }
   
 


clearcount= digitalRead(clearcounter);
if(clearcount==HIGH)
{
  for (int i = 0; i < 512; i++)
    EEPROM.write(i, 0);
    
  // turn the LED on when we're done
  digitalWrite(13, HIGH);
    Serial.println("Clear");
}


  
}




 

  
  // advance to the next address.  there are 512 bytes in

As a courtesy to other users, will you pleaase auto-format your code before posting, and remove excess whitespace?

I don't know. Here is my version, stripped of all the unnecessary bits. See what you get from it.

#include <EEPROM.h>

int counter=250;

void EEPROMWriteInt(int address, int counter)
{
  byte counterLow = lowByte(counter);
  byte counterHigh = highByte(counter);
  EEPROM.write(address, counterLow);
  EEPROM.write(address + 1, counterHigh);
}

int EEPROMReadInt(int address)
{
  int mylowByte = EEPROM.read(address);
  int myhighByte = EEPROM.read(address + 1);
  int w = myhighByte * 256 + mylowByte;
  return w;
}

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

  for (int l=0; l<20; l++)
  {
    EEPROMWriteInt(10, counter);
    counter = EEPROMReadInt(10) + 1;
    Serial.println(counter);
  }
}

void loop()
{
}

I get

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

dxw00d:
I don't know. Here is my version, stripped of all the unnecessary bits. See what you get from it.

#include <EEPROM.h>

int counter=250;

void EEPROMWriteInt(int address, int counter)
{
  byte counterLow = lowByte(counter);
  byte counterHigh = highByte(counter);
  EEPROM.write(address, counterLow);
  EEPROM.write(address + 1, counterHigh);
}

int EEPROMReadInt(int address)
{
  int mylowByte = EEPROM.read(address);
  int myhighByte = EEPROM.read(address + 1);
  int w = myhighByte * 256 + mylowByte;
  return w;
}

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

for (int l=0; l<20; l++)
  {
    EEPROMWriteInt(10, counter);
    counter = EEPROMReadInt(10) + 1;
    Serial.println(counter);
  }
}

void loop()
{
}




I get



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

So 20 times in a row you write a value to address 10 and 11 of the EEPROM then read from EEPROM address 10 and 11 the same value you just wrote to it, add 1 and that becomes your new value that you print. 20 times in a row to the same two places on the EEPROM when all the EEPROM cells have limited writes.

Your second attempt (below) looks pretty good to me. The EEPROMWriteInt() and EEPROMReadInt() functions are fine. For some reason you have declared counter as a byte though, and a byte is only an 8-bit value (0-255). Also I'm not sure why you're writing 0 to all of the EEPROM addresses when your clearcount input is HIGH; what's the purpose there?.

If you fix the counter declaration, does your code do what you want it to do?

chadect:
what should be the variable type of my counter?

i have updated my codes and am i doing right?

still when i reach the counter from 0-255 my count return to 0 again...thank you

#include <EEPROM.h>

int addr;
int address;
const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
byte counter;
byte value;

void EEPROMWriteInt(int address, int counter)
{
 byte lowByte = ((counter >> 0) & 0xFF);
 byte highByte = ((counter >> 8) & 0xFF);

EEPROM.write(address, lowByte);
 EEPROM.write(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 address)
{
 byte lowByte = EEPROM.read(address);
 byte highByte = EEPROM.read(address + 1);

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

void setup()
{
 pinMode(button, INPUT);
 pinMode(clearcount, INPUT);
 pinMode(led, OUTPUT);
 Serial.begin(9600);
 // addr=EEPROM.read(address);

}

void loop()
{
 delay(1);
 state= digitalRead(button);
 if(state==HIGH)
 {
   delay(1);

counter= EEPROMReadInt(address)+1;

EEPROM.write(address, counter);
   Serial.println("WRITE");
   Serial.print(address);
   Serial.print("\t");
   Serial.print(counter, DEC);
   Serial.println("WRITE END ");
   Serial.println();

// the EEPROM, so go back to 0 when we hit 512.

}
 else if (state==LOW)
 {

}
 clearcount= digitalRead(clearcounter);
 if(clearcount==HIGH)
 {
   for (int i = 0; i < 512; i++)
     EEPROM.write(i, 0);

// turn the LED on when we're done
   digitalWrite(13, HIGH);
   Serial.println("Clear");
 }
}

#include <EEPROM.h>


int addr;
int address;
const int button =2;
const int clearcounter= 3;
const int led = 12;
int state =0;
int clearcount=0;
byte counter;
byte value;

void EEPROMWriteInt(int address, int counter)
{
  byte lowByte = ((counter >> 0) & 0xFF);
  byte highByte = ((counter >> 8) & 0xFF);

  EEPROM.write(address, lowByte);
  EEPROM.write(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 address)
{
  byte lowByte = EEPROM.read(address);
  byte highByte = EEPROM.read(address + 1);

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



void setup()
{
  pinMode(button, INPUT);
  pinMode(clearcount, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  // addr=EEPROM.read(address);


}


void loop()
{
  delay(1);
  state= digitalRead(button);
  if(state==HIGH)
  {
    delay(1);

    counter= EEPROMReadInt(address)+1;


    EEPROM.write(address, counter);
    Serial.println("WRITE");
    Serial.print(address);
    Serial.print("\t");
    Serial.print(counter, DEC);
    Serial.println("WRITE END ");
    Serial.println();

    // the EEPROM, so go back to 0 when we hit 512.

  }
  else if (state==LOW)
  {

  }
  clearcount= digitalRead(clearcounter);
  if(clearcount==HIGH)
  {
    for (int i = 0; i < 512; i++)
      EEPROM.write(i, 0);

    // turn the LED on when we're done
    digitalWrite(13, HIGH);
    Serial.println("Clear");
  }
}

Just this part at the end here:

    for (int i = 0; i < 512; i++)
      EEPROM.write(i, 0);

    // turn the LED on when we're done
    digitalWrite(13, HIGH);
    Serial.println("Clear");

It writes a 2 byte value and then indexes 1 byte to write the next.

Save yourself a bit of trouble:

http://arduino.cc/en/Reference/LowByte
http://arduino.cc/en/Reference/HighByte

Nick he already has working functions to write and read ints to the EEPROM using bit shifts and mask - fundamentally a better level of understanding than those hi/lo functions, and he should be able to easily extend that to 32bit data types if he wants. Why not support his approach?