Store Rotary Encoder Data for next Power Up

At the moment I am using a hall effect sensor to track a point on a gear. This is good for calculating RPM as well as keeping a constant track of said point.

I came across the idea of using a rotary encoder again. If there are 50 teeth on the gear, I will use the encoder to count 1-50, reseting every 50 counts. This way 25 is always my middle point (point on gear I am tracking). In this case it is similar to the hall sensor.

The problem is the calibration every time the arduino is powered on and off.

Is it possible to store the last gear tooth count, so that when the board is powered up again it will start where it left off, Like EEPROM? If so, is there some sample code I can read?

Thanks

Yes EEPROM is good look here:- http://www.arduino.cc/en/Reference/EEPROM

However how are you going to tell when the arduino is going to be switched off? You can't store every change in the EEPROM as it has a limited number of write cycles before it dies.

The Atmega 168 datasheet says that EEPROM memory has a specified life of 100000 write/erase cycles, so there is a limit to how many times you can write information to that memory space.

However how are you going to tell when the arduino is going to be switched off? You can’t store every change in the EEPROM as it has a limited number of write cycles before it dies.

I was hoping that it would constantly write and erase, but thank you for pointing out that it can’t.

I can press a button on the board signaling a “shut down”, to save the count number.

Does this mean I can only store a number up to 255 or does it refer to the size of the data?

Syntax
EEPROM.write(address, value)
Parameters
address: the location to write to, starting from 0 (int)
value: the value to write, from 0 to 255 (byte)
Returns

Also can you explain in the example code
#include <EEPROM.h>

void setup()
{
for (int i = 0; i < 512; i++)
EEPROM.write(i, i);
}

void loop()
{
}

What the for (int i=0;i<512;i++) means?

I also realize that I can continue to use my hall effect sensor with multiple magnets to keep count ::)

One byte in the EEPROM can store a value from 0 to 255, to store a bigger number just use two or more bytes, next to each other.

It's a bit fiddly but you can store your data in two circular arrays (one keeps track of the write point) in EEPROM, this will give you 512x the write capacity or about 51 million writes before you expect the EEPROM to die.

I don't know how often the value needs to be stored but that's a heck of a long time.

EDIT: Actually I just realised that if you're storing bytes then you'd only get 25 million, but you could switch after a while and get another 25 million.


Rob