EEPROM Put & Read - Servo Motor Issues

Would somebody be kind enough to help me with using the EEPROM as memory storage.

I know it's very tricky to to remember a servos position upon reboot so I am trying to store this position in the eeprom and write to it upon program setup.

to write to my servo, I'm using a variable called posA. I'm using a push button that activates this code so I can then restart my arduino or reload the code.

  if (RESET == LOW) {
    lastresetpress = millis(); // Button not pressed set so reset clock
  }


  if ((millis() - lastresetpress >= timer1500) && (RESET == HIGH))
  {
    int readposA = 0;
    int readposB = 1;
    posA = servoA.read();
    posB = servoB.read();
    EEPROM.put(readposA, posA);
    delay(5);
    EEPROM.put(readposB, posB);
    delay(5);
    Serial.print('\n');
    Serial.print("servoA Angle Saved At:");
    Serial.println(posA);
    Serial.print("servoB Angle Saved At:");
    Serial.println(posB);
    Serial.print("Positions Saved, System Ready for Restart");
    delay(2000);
  }

Pretty much I am using readposA and readposB as storage locations, and posA & posB as values.

Upon starting my program in void setup, how can I use the EEPROM.read to write this value back to my motor before attaching it?

Thanks, Mike

Edit:

I feel like I am using these instructions completely wrong tbh. My end goal is to store my position in the EEPROM right before I decide to physically power it down and then upon startup in the setup, read this value in the EEPROM and write it to the servo.

If you have any other way of approaching this please post!!

Thanks again.

Assuming that the servo position is in a variable I don't understand the problem. In setup() read the previously stored value into a variable and use it as the servo position. What is more difficult is to know when to save the position before powering off or resetting the Arduino.

Can you please explain your problem more clearly ?

I think I finally figured it out. Very simple to be honest. What’s hard is getting these motors to co-operate. Here’s the start of my code for anyone wondering.

int readposA = 99;
int readposB = 100;

void setup() {
  Serial.begin(9600); // initialize serial
  delay(50);
 
  pinMode(ButtonForward, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonReverse, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonMotorToggle, INPUT);
  pinMode (ButtonReset, INPUT);
  pinMode (servoPinA, OUTPUT);
  pinMode (servoPinB, OUTPUT);

  posA = EEPROM.read(readposA);  // Variable to store servoA's position
  posB = EEPROM.read(readposB); // Variable to store servoB's position
  Serial.print('\n');
  Serial.println("posA from EEPROM:");
  Serial.println(posA);
  Serial.println("posB from EEPROM:");
  Serial.println(posB);
  servoA.write(posA);
  servoB.write(posB);
  servoA.attach(8);  // attaches servoA to pin 8
  servoB.attach(6); // attaches servoB to pin 6

Now… Does anybody know how to fix the jitter of a servo motor? It seems to be just one of mine and as I try to rotate it, I have jitter issues where it sometimes glitches and goes forward and reverse.

This is really what’s making the programming difficult because I don’t know if it’s my program or just my motors.

Here’s my total code

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

// Set digital pin numbers:
const int servoPinA = 8;  // The number of the ServoA pin
const int servoPinB = 6; // The number of the ServoB pin
const int ButtonForward = 9;  // The number of the fwd. Pushbutton pin
const int ButtonReverse = 7; // The number of rev. Pushbutton pin
const int ButtonMotorToggle = 10; // Motor toggle push button
const int ButtonReset = 11;

int ButtonStateFwd;  // Variable for reading the pushbutton status
int ButtonStateRev;
int ButtonStateToggle;
int RESET;
int timer1000 = 1000;
int timer1500 = 1500;
int lastfwdpress;
int lastrevpress;
int lastresetpress;
int posA;
int posB;
int readposA = 99;
int readposB = 100;
Servo servoA;  // Create servo object to control a servo
Servo servoB;
int servoNum = 0; // Variable to select servo to be controlled


void setup() {
  Serial.begin(9600); // initialize serial
  delay(50);
 
  pinMode(ButtonForward, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonReverse, INPUT);  // initialize the pushbutton pin as an input
  pinMode (ButtonMotorToggle, INPUT);
  pinMode (ButtonReset, INPUT);
  pinMode (servoPinA, OUTPUT);
  pinMode (servoPinB, OUTPUT);

  posA = EEPROM.read(readposA);  // Variable to store servoA's position
  posB = EEPROM.read(readposB); // Variable to store servoB's position
  Serial.print('\n');
  Serial.println("posA from EEPROM:");
  Serial.println(posA);
  Serial.println("posB from EEPROM:");
  Serial.println(posB);
  servoA.write(posA);
  servoB.write(posB);
  servoA.attach(8);  // attaches servoA to pin 8
  servoB.attach(6); // attaches servoB to pin 6

  Serial.print("Servo Selected:");
  Serial.print(servoNum);
  Serial.print('\n');
  Serial.println("servoA Starting Angle:");
  Serial.println(posA);
  Serial.println("servoB Starting Angle:");
  Serial.println(posB);
}

void loop() {
  // read the state of the pushbutton value:
  ButtonStateFwd = digitalRead(ButtonForward);
  ButtonStateRev = digitalRead(ButtonReverse);
  ButtonStateToggle = digitalRead(ButtonMotorToggle);
  RESET = digitalRead(ButtonReset);

  if (servoNum == 3) {
    servoNum = 1;
    Serial.print('\n');
    Serial.print("Servo Selected:");
    Serial.print(servoNum);
    delay(50);
  }

  if (ButtonStateToggle == HIGH) {
    servoNum = servoNum + 1;
    Serial.print('\n');
    Serial.print("Servo Selected:");
    Serial.print(servoNum);
    if (servoNum == 3) {
      servoNum = 1;
      Serial.print('\n');
      Serial.print("Servo Selected:");
      Serial.print(servoNum);
    }
    delay(3000);
  }

  if (ButtonStateFwd == LOW) {
    lastfwdpress = millis(); // Button not pressed set so reset clock
  }

    if (ButtonStateRev == LOW) {
    lastrevpress = millis(); // Button not pressed set so reset clock
  }

  if ((millis() - lastfwdpress >= timer1000) && (servoNum == 1) && (ButtonStateFwd == HIGH))
  {
    posA += 1;
    delay(5);
    servoA.write(posA);
    Serial.print('\n');
    Serial.print("servoA Angle:");
    Serial.print(posA);
    delay(15);

    if (posA >= 176) {
      posA = 175;
    }
  }

  if (ButtonStateRev == LOW) {
    lastrevpress = millis(); // Button not pressed set so reset clock
  }

  if ((millis() - lastrevpress >= timer1000) && (servoNum == 1) && (ButtonStateRev == HIGH))
  {
    posA -= 1;
    delay(5);
    servoA.write(posA);
    Serial.print('\n');
    Serial.print("servoA Angle:");
    Serial.print(posA);
    delay(15);

    if (posA <= 4) {
      posA = 5;
      delay(15);
    }
  }

  if ((millis() - lastfwdpress >= timer1000) && (servoNum == 2) && (ButtonStateFwd == HIGH))
  {
    posB += 1;
    delay(5);
    servoB.write(posB);
    Serial.print('\n');
    Serial.print("servoB Angle:");
    Serial.print(posB);
    delay(15);

    if (posB >= 176) {
      posB = 175;
      delay(5);
    }
  }

  if ((millis() - lastrevpress >= timer1000) && (servoNum == 2) && (ButtonStateRev == HIGH))
  {
    posB -= 1;
    delay(5);
    servoB.write(posB);
    Serial.print('\n');
    Serial.print("servoB Angle:");
    Serial.print(posB);
    delay(5);

    if (posB <= 4) {
      posB = 5;
      delay(5);
    }
  }

   if (posA <= 4) {
      posA = 5;
      delay(5);
    }

  if (posA >= 176) {
      posA = 175;
      delay(5);
    }
    
   if (posB <= 4) {
      posB = 5;
      delay(5);
    }

  if (posB >= 176) {
      posB = 175;
      delay(5);
    }

  if (RESET == LOW) {
    lastresetpress = millis(); // Button not pressed set so reset clock
  }


  if ((millis() - lastresetpress >= timer1500) && (RESET == HIGH))
  {
    readposA = 99;
    readposB = 100;
    posA = servoA.read();
    posB = servoB.read();
    EEPROM.put(readposA, posA);
    delay(5);
    EEPROM.put(readposB, posB);
    delay(5);
    Serial.print('\n');
    Serial.print("servoA Angle Saved At:");
    Serial.println(posA);
    Serial.print("servoB Angle Saved At:");
    Serial.println(posB);
    Serial.print("Positions Saved, System Ready for Restart");
    delay(2000);
  }
}

Also, sometimes when I control servoB, servoA randomly rotates.

when I control servoB, servoA randomly rotates.

Have you got any pullup resistors on your switches ?