Go Down

Topic: EEPROM Problem (Read 3134 times) previous topic - next topic


Oct 07, 2012, 05:15 am Last Edit: Oct 07, 2012, 05:30 am by rweber95 Reason: 1
I have a motor driven display disk that can be rotated left (CCW) or right (CW) with a manual switch.  A push button allows two positions to be stored as limits for an automatic mode which will alternately move the disk to the CCW limit, wait for a predetermined time, then move to a CW limit and again wait.  This program works but requires the operator to set up the limits everytime the device is turned on.  

To correct this, I modified the code to save the 2 positions in EEPROM so that on power up I can restore the limit variables to their last set values.  It doesn't work!  I've added some serial print lines to help toubleshoot (since there is no debug mode or single step mode with the Arduino UNO).  The code section is a partial list of the program.

Here are the serial print results.

Pos1 25
Pos2 124
Pos1 0
Limit 0
Direction 1
Position 0
Pos2 0
Limit 0
Direction 1
Position 0

Code: [Select]
#include <EEPROM.h>//Include the EEPROM library.
#define CCW 0
#define CW 1
int Position = 0;//Current position count.
int Pos1;//CCW position in auto mode.
int Pos2;//CW position in auto mode.
int count = 0;//Control used to set Pos1 and Pos2.

void setup(){
 int Pos1 = EEPROM.read(CCW);//Get the last saved CCW position.
 Serial.print("EEPROM ");
 Serial.print("Pos1 ");
 int Pos2 = EEPROM.read(CW);//Get the last saved CW position.
 Serial.print("EEPROM ");
 Serial.print("Pos2 ");

void loop(){
 if(ManualMode()){//Repeat this section while in manual mode.
   while(CWCommand()) rotate(CW);//Rotate until switch released.
   while(CCWCommand()) rotate(CCW);//Rotate until switch released.
   if(SetButtonPressed()){//Save positions for use in auto mode.
     switch (count){
       case 0:
        Pos1 = Position;//Get the new CW position.
        EEPROM.write(CCW,Pos1);//Save it.
        Serial.print("Writing ");//This message will print if the button has been pressed.
        Serial.print(Pos1);//This is the new position to be saved.
        count = 1;
       case 1:
        Pos2 = Position;//Get the new CCW position.
        EEPROM.write(CW,Pos2);//Save it.
        count = 2;
       case 2:
        count = 0;//Positions have been saved so reset count for need application.
        if(Pos1 > Pos2) Exchange();
//Auto Mode
    Serial.print("Pos1 ");//This is the limit value to pass to automove.
    Serial.print("Pos2 ");//This is the limit value to pass to automove.

void AutoMove(int Limit){//Rotate the disc to the desired position.
  int Direction = CW;//Assume direction to go to the limit position.
  if(Position > Limit) Direction = CCW;//Assumption was wrong, change it.
  Serial.print("Limit= ");//These printlines show the parameters used by automove.
  Serial.print("Direction ");
  Serial.print("Position ");
  while(Position != Limit) rotate(Direction);}//Rotate disc until at the desired limit.


Coding Badly

The EEPROM is bytewise.  int is two bytes.  You need to move the CW value to allow space for the CCW value...

[font=Courier New]#define CCW 0
#define CW  2  [/font]

You also need to store and retrieve both bytes for each value.  This provides a convenient way...


Thanks for the reply.  It's been 20 years since I did any C programming so I'm a BYTE foggy.
Could you give me some specific code to store an integer value to a pair of EEPROM addresses and retrieve them back to an integer variable?
I was thinking of read the most significate byte, shift it 8 places and concatonate it with the lower byte but???



I was thinking of read the most significate byte, shift it 8 places and concatonate it with the lower byte but???

To store it, you can extract the bytes using highByte() and lowByte().

To retrieve it, do as you said with the high byte and to "concatenate" it, you can just add the low byte to it after the shift.

Go Up