/* EEPROMv1 * Simple sketch that uses the Arduino built in EEPROM * * Part of the DCC Turntable Project at * http://www.digitaltown.co.uk/project7ModelRailwayTurntable.php * * EEPROM is used to store the turntable exit positions, these are adjustable * using a couple of push to contact buttons allowing positions to be turned over time. * These values are stored in EEPROM so that they are updated when the Arduino restarts */ //EEPROM //system will use built in eeprom as it should not be written to very much //it has a limited lifecycle compared to external eeprom chips. #include "EEPROM.h" const int eepromOffset = 200;//this is the position of the first byte stored in EEPROM //An UNO only has 512 bytes of EEPROM, I used 200 as a value to stop all my projects using the 1st byte //This value can be set to 0 if you have not stored data in EEPROM before //This array holds the positions for the number 1 end of the turntable //A default value has been put in to populate the EEPROM int trackPositions[4] = {247, 10747, 13047, 23547}; //default positions in steps of track exits from end 1 long eepromUpdatePeriod = 60000;//60 secs between updates...also used to turn the power off unsigned long eepromUpdateTimer; //update the eeprom with current trackpositions void updateeeprom() { int q; //writing 4 integer values to EEPROM for (q = 0; q < 4; q++) { //EEPROM.update used as it only writes a value if it is different to the existing value. //This prevents constant unrequired writes. //As the step postion is an integer it needs to br written across 2 locations //As each location can only store 1 byte EEPROM.update((q * 2) + eepromOffset, highByte(trackPositions[q])); EEPROM.update((q * 2) + eepromOffset + 1, lowByte(trackPositions[q])); } //Once we have written the values to the EEPROM we will read them back geteepromdata(); } void geteepromdata() { int q; //get the 4 bytes of data byte getbytes[8];//2 bytes per position Serial.println("getbytes[]: "); //get the data from eeprom and store in array for (q = 0; q < 8; q++) { getbytes[q] = EEPROM.read(q + eepromOffset); Serial.println( getbytes[q]); } Serial.println("trackPositions[]: "); //now assemble the data and put into trackpositions[4] for (q = 0; q < 4; q++) { trackPositions[q] = (getbytes[q * 2] * 256) + (getbytes[(q * 2) + 1]); Serial.println(trackPositions[q]); } } void setup() { Serial.begin(9600); Serial.println("EEPROMv1..."); //updateeeprom();//comment this line out once the EEPROM has been updated for the first time geteepromdata(); } void loop() { //This updates the EEPROM every 60 seconds, more than enough for turntable position updates. if (millis() > eepromUpdateTimer) { //reset the time period for the next update eepromUpdateTimer = millis() + eepromUpdatePeriod; //update the eeprom updateeeprom(); } }