Servo position storage for power reset

Hello all,

having a issue getting the servo to store and read it position after start up from power loss or reset. When power is applied after loss or reset it returns to a different postion then last. I only want it to read it’s last position on start up. What am I doing wrong?

#include <EEPROM.h>
#include <VarSpeedServo.h>



VarSpeedServo myservo;
int potpin1 = 0;
int potpin2 = 1;
int val1;
int val2;
int Buttonpin = 2;
boolean on=false;
int buttonState = 0; 
int address = 0;
byte value;
int startup;

void setup()
{
  pinMode(Buttonpin,INPUT);
  myservo.attach(9);
  startup = EEPROM.read(address);
  myservo.write(startup);
}
void loop()
{
  {
    val1 = analogRead(A0);
    val2 = analogRead(A1);
    val1 = map(val1, 0, 1023, 0, 179);
    val2 = map(val2, 0, 1023, 0, 179);
    buttonState = digitalRead(Buttonpin);
    if (buttonState == HIGH) {    
      if(on==true){
        on=false;
      }  
      else{
        on=true;
      }
    }
    if (on == true) {   
      myservo.write(val1, 15, true);
      EEPROM.write(0, val1);
    }
    else {   
      myservo.write(val2, 15, true);
      EEPROM.write(0,val2);
    }

    delay(200); 
  } 
}

What am I doing wrong?

You tell us.

The code you posted has useless curly braces and curly braces that are NOT properly aligned.

The code does something. You forgot to tell us what it does.

You expect the code to do something. You forgot to tell us what you expect it to do.

The servo will go to the position it was last commanded to go to when it is attached. So, tell it where to go BEFORE you attach it.

Writing to EEPROM every time you move the servo, even if there is actually no movement, is NOT a good idea.

PaulS:
The code does something. You forgot to tell us what it does. - The code moves a servo between two points set with the pots when the button is pressed.

The servo will go to the position it was last commanded to go to when it is attached. So, tell it where to go BEFORE you attach it. Ok.

Writing to EEPROM every time you move the servo, even if there is actually no movement, is NOT a good idea. So only write to EEPROM when it changes to the second point?

nkpchris2009:
So only write to EEPROM when it changes to the second point?

Check out EEPROM.update():
https://www.arduino.cc/en/Reference/EEPROMUpdate
That will save your EEPROM from unnecessary wear. Keep in mind that the EEPROM is only rated for 100,000 writes per cell. It seems like a lot but a small bug in your code can cause that to be exceeded very quickly.

Cleaned up and rewrote the code a little.

#include <VarSpeedServo.h>

VarSpeedServo myservo;
const int potpin1 = 0;
const int potpin2 = 1;
const int buttonPin = 2;
// Variables will change:
int val1;
int val2;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{
  pinMode(buttonPin, INPUT); // initialize the button pin as a input:
  Serial.begin(9600); // initialize serial communication:
  myservo.attach(9); // attaches servo to pin 9
}
void loop()
{

  val1 = analogRead(A0);
  val2 = analogRead(A1);
  val1 = map(val1, 0, 1023, 0, 179);
  val2 = map(val2, 0, 1023, 0, 179);

  buttonState = digitalRead(buttonPin); // read the pushbutton input pin:

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState){
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;


  // moves the servo every other button pushes by
  // checking the modulo of the button push counter.
  if (buttonPushCounter % 2 == 0) {
    myservo.write(val1, 15, true);
  } 
  else {
    myservo.write(val2, 15, true);
  }
}

The servo moves like it should between the two points set by the 2 10k pots. Now would i save the counter value to EEPROM so that value can be read and added too after power is reapplied and the servo knows where to be uppon power up?

Now would i save the counter value to EEPROM so that value can be read and added too after power is reapplied and the servo knows where to be uppon power up?

I wouldn’t. I’d save the servo position. If the servo was at 38 degrees, and the Arduino is shutdown, the servo should be moved to 38 degrees, shouldn’t it? That would be regardless of whether the potentiometers had been diddled with while the Arduino was powered off.

I really don’t understand what you are doing, though.