Pages: [1]   Go Down
Author Topic: EEPROM Problem  (Read 1369 times)
0 Members and 1 Guest are viewing this topic.
Tacoma, Washington, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

Code:
#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(){
  Serial.begin(9600);
  int Pos1 = EEPROM.read(CCW);//Get the last saved CCW position.
  Serial.print("EEPROM ");
  Serial.println(EEPROM.read(CCW));
  Serial.print("Pos1 ");
  Serial.println(Pos1);
  int Pos2 = EEPROM.read(CW);//Get the last saved CW position.
  Serial.print("EEPROM ");
  Serial.println(EEPROM.read(CW));
  Serial.print("Pos2 ");
  Serial.println(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;
         break;
        case 1:
         Pos2 = Position;//Get the new CCW position.
         EEPROM.write(CW,Pos2);//Save it.
         count = 2;
        break;
        case 2:
         count = 0;//Positions have been saved so reset count for need application.
         if(Pos1 > Pos2) Exchange();
         break;}}}
        
//Auto Mode
    while(!ManualMode()){
    Serial.print("Pos1 ");//This is the limit value to pass to automove.
    Serial.println(Pos1);
    AutoMove(Pos1);
    Idle();
    Serial.print("Pos2 ");//This is the limit value to pass to automove.
    Serial.println(Pos2);
    AutoMove(Pos2);
    Idle();}}

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.println(Limit);
  Serial.print("Direction ");
  Serial.println(Direction);
  Serial.print("Position ");
  Serial.println(Position);
  while(Position != Limit) rotate(Direction);}//Rotate disc until at the desired limit.

Bob
« Last Edit: October 06, 2012, 10:30:14 pm by rweber95 » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13024
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

#define CCW 0
#define CW  2  


You also need to store and retrieve both bytes for each value.  This provides a convenient way...
http://www.arduino.cc/playground/Code/EEPROMWriteAnything
Logged

Tacoma, Washington, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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???

Bob
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3435
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: