-1

I have A code I got from a site. I've been trying to add EEPROM in that code but I can't get it right. I tried adding EEPROM.write and EEPROm.read I also add another servo

Can someone help me with it, Adding EEPROM in the code? Thank you in advance :)

here's the code (Simple Robotic arm with play and record function):

#include <Servo.h>
#include <EEPROM.h>

const int PinButton1 = 2;  // pin 2   
const int PinButton2 = 3;  // pin 3
int mode = 1;     // case 1 program robot arm. case 2 replay positions 
int bounce = 0;
volatile int buttonPress = 0;
unsigned long lastButtonPressTime = 0;
volatile unsigned long bounceTime = 0;
Servo Arm0Servo;      //servo objects
Servo Arm1Servo;      // Ihave renumbered servos 26 1 2017
Servo Arm2Servo;
Servo Arm3Servo;
Servo Arm4Servo;
int pos1,pos2,pos3,pos4,pos0;
int PosArm[5] ;       // array 
int ArmPos[100][5];   // array to hold arm positions up to 100
int PosCount = 0;     //  to count number of positions increased when button     pressed
int PosCountMax = 0;  //  number of positions recorded when double button push initiates replay mode
int PosReached = 0;   // used to check that arm has moved from one recorded position to next position
int addr = 0;
boolean recorded = false;

void setup() {  
     for(int i = 0; i <100 ; i++ ){
     for(int p = 0; p <5 ; p++ ){
         ArmPos[i][p] = -1;
     }
 }
pinMode(PinButton1 , OUTPUT);
digitalWrite(PinButton1 ,LOW);
pinMode(PinButton2, INPUT_PULLUP);
//  I have made a small change here due to problem using some Arduinos
//attachInterrupt( digitalPinToInterrupt(PinButton2),ButtonPress , LOW );
// digitalPinToInterrupt(PinButton2) may not be defined!
attachInterrupt( 1,ButtonPress , LOW );   // interupt to capture button presses

// attach servos to relervent pins on arduino nano
Arm0Servo.attach(12); // grip    90 to 180 open    limits of servo movement
Arm1Servo.attach(11); // elbow      to 130 up
Arm2Servo.attach(10); // shoulder   10 to 50 down
Arm3Servo.attach(9);  // turn    0  to 180 right 
Arm4Servo.attach(8);  // turn    0  to 180 right
}

void loop() {
  switch(mode){
    case 1 :  //  program robot arm. 1 press to remember position. 2 presses to progress next case 2 replay mode
              // analogRead(pin) that reads poteniometers on training arm
    PosArm[0] = map(analogRead(0),480,1024,180,10); // map (480,1024 value from potentiometer to 180,90 value sent to servo)
    EEPROM.write(addr, PosArm[0]);
    addr++;
    PosArm[1] = map(analogRead(1),480,1024,180,10);
    EEPROM.write(addr, PosArm[1]);
    addr++;
    PosArm[2] = map(analogRead(2),480,1024,180,10);
    EEPROM.write(addr, PosArm[2]);
    addr++;
    PosArm[3] = map(analogRead(3),480,1024,180,10);
    EEPROM.write(addr, PosArm[3]);
    addr++;
    PosArm[4] = map(analogRead(4),480,1024,180,10);
    EEPROM.write(addr, PosArm[4]);
             addr++;
    MoveArm();                          // call method  
    if(buttonPress == 1){               // flag set by interupt when button is pressed          
         buttonPress = 0;               // reset flag 
         if( millis() > (lastButtonPressTime + 1000)){    // only one button press in one secound
             // record  position of arm PosArm to array[100][] of armpositions        
             ArmPos[PosCount][0] = PosArm[0];

             ArmPos[PosCount][1] = PosArm[1];

             ArmPos[PosCount][2] = PosArm[2];

             ArmPos[PosCount][3] = PosArm[3];

             ArmPos[PosCount][4] = PosArm[4];


             if (addr == 512) {
               EEPROM.write(addr, 255);
               break;
             }

             if( PosCount < 100) {      // stop recording if over 100 positions recorded (memory limitations)
                 PosCount++;         
             }
         }else{                         //  more than one button press
             mode = 2;                  // go to next phase
             PosCountMax  = PosCount;   // set number of arm positions recorded
             PosCount = 0;              // reset count ready to read  arm position array from begining 
         }
    lastButtonPressTime = millis();              
    }
  break;
case 2 :        // read arm position array
    PosReached = 0;
    for(int i = 0; i <5 ; i++ ){   //adjust servo positions   
        // we move the servos in small steps and the delay(20)  makes arm motion smooth and slow
        if( PosArm[i] > ArmPos[PosCount][i]) PosArm[i] = PosArm[i] - 1;  // if actual position is greater than requird position reduce position by 1
        if( PosArm[i] < ArmPos[PosCount][i]) PosArm[i] = PosArm[i] + 1;  // if actual position is less than required position value increase position valuue by 1
        if( PosArm[i] == ArmPos[PosCount][i]) PosReached++;              // check if servo  has reached required position/angle
    }

    if(PosReached == 5) PosCount++;        // if all 4 servos have reached position  then increase array index (PosCount)to next position in array
    if(PosCount == PosCountMax) PosCount = 0;   //  if end of array reached reset index to 0 and repeat.
    Play();   // physically move arm to updated position, this is broken into small steps
    delay(5);   // pause between moves so over all motion is slowed down
  break;
default :
  break;
  }    
}

void MoveArm() {   // write arm position data to servos
  for (int i = 0 ; i < 5 ; i++) {
  if (PosArm[i] < 5) PosArm[i] = 5;  // limit servo movement to prevent hitting end stops
  if (PosArm[i] > 175) PosArm[i] = 175;  // servo.write limited 5 - 175  
  }
  Arm0Servo.write(PosArm[0]);
  Arm1Servo.write(PosArm[1]);
  Arm2Servo.write(PosArm[2]);
  Arm3Servo.write(PosArm[3]);
  Arm4Servo.write(PosArm[4]);
  Play();
  delay(5);
}
void Play() {   // write arm position data to servos
  for (int i = 0 ; i < 5 ; i++) {
  if (PosArm[i] < 5) PosArm[i] = 5;  // limit servo movement to prevent hitting end stops
  if (PosArm[i] > 175) PosArm[i] = 175;  // servo.write limited 5 - 175  
  }

  if (addr == 0) {
  PosArm[0] = EEPROM.read(addr);
  pos0 = EEPROM.read(addr+5);
  addr++;

  PosArm[1] = EEPROM.read(addr);
  pos1 = EEPROM.read(addr+5);
  addr++;

  PosArm[2] = EEPROM.read(addr);
  pos2 = EEPROM.read(addr+5);
  addr++;

  PosArm[3] = EEPROM.read(addr);
  pos3 = EEPROM.read(addr+5);
  addr++;

  PosArm[4] = EEPROM.read(addr);
  pos4 = EEPROM.read(addr+5);
  addr++;

  }

  }
   void ButtonPress(){   //  interupt to capture button press
   if(micros() > (bounceTime + 3000)){ // debounce timer 
    bounceTime = micros();          // ingnore interupts due to button bounce
    buttonPress = 1;     // flag for button pressed
}

}

Coddie
  • 9
  • 2
  • 1
    What do you mean, "add EEPROM" ? Your program can write to it and/or read from it, what do you want to do and what did you try so far ? – Sid S Jan 26 '18 at 05:02
  • The program works well, it write and read, but I want to save the write data in the EEPROM permanently. I tried EEPROM. write and EEPROM.read, it still works but it can't read the write data anymore. Thank You :) – Coddie Jan 26 '18 at 05:55
  • So where is your code that does this ? – Sid S Jan 26 '18 at 06:03
  • I will edit the posted code above. – Coddie Jan 26 '18 at 06:27
  • done editing it. – Coddie Jan 26 '18 at 06:39
  • 1
    Please reduce your code sample! This is not the review center for your work! If you have a problem like writing to eeprom, please reduce your code exactly to that! – Klaus Jan 26 '18 at 07:30

1 Answers1

1

The code that you posted will never attempt to read data from the EEPROM due to the following problem:

When your microcontroller is started, the variable mode is set to 1. If you look carefully at the loop function you can see that your code will enter the case 1 branch at least once where it basically writes data to the EEPROM and - more importantly - will set addr to a value other than 0, due to the addr++ instructions (unless you run the code so long that you encounter an integer overflow).

The only place where you attempt to read from the EEPROM is in the Play function. Due to the if (addr == 0) condition in this function you will never execute the EEPROM.read instructions as addr will not be 0 as explained above.

  • 1
    Johannes is right. Furthermore, this sketch will write to the EEPROM 1000+ times a second unless it enters mode 2 or 3. That will give your EEPROM a lifetime of a little under 2 hours to reach a cell cycle of 100000 as per ATMEL is the lifespan. So you would really want to redo your handling of EEPROM. And your Arduino might already have a dead EEPROM. – XerXeX Jan 27 '18 at 22:33