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