How to get the last value from previous running

I am using an Encoder motor and PID logic to control its position like a servo motor. The code attached below is working fine.

I want to save the value of the pos variable in the code, so that when I restart the program next time, I can retrieve that saved value. Currently, its value is zero when i start the program. How can I do this?

I want that the motor should be set at a certain position whenever i start the arduino UNO. So for eg if it was left at 240degrees the last time i ran the program, I restart the Arduino and the motor turns back at zero position. This I can only do If i have the last pos value saved and turn the motor accordingly.

Or is there any other method for this?

This is the motor I'm using: https://www.aliexpress.com/item/32612296812.html
I'm using it with L298N Motor Driver

#define ENCA 2 
#define ENCB 3 
#define PWM 5
#define IN2 6
#define IN1 7

int turns = 0; 
int target;
int pos;
long prevT = 0;
float eprev = 0;
float eintegral = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ENCA,INPUT);
  pinMode(ENCB,INPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA),readEncoder,RISING);
  Serial.println("target pos");
}

void loop() {
  if (Serial.available() >0) {
    int x = Serial.parseInt();
    target = x;
  }
  
  //target = 250*sin(prevT/1e6);

  // PID constants
  float kp = 1;
  float kd = 0.026;
  float ki = 0.02;

  // time difference
  long currT = micros();
  float deltaT = ((float) (currT - prevT))/( 1.0e6 );
  prevT = currT;

  // error
  int e = pos-target;

  // derivative
  float dedt = (e-eprev)/(deltaT);

  // integral
  eintegral = eintegral + e*deltaT;

  // control signal
  float u = kp*e + kd*dedt + ki*eintegral;
  Serial.print(u);
  Serial.print(" ");

  // motor power
  float pwr = fabs(u);
  if( pwr > 255 ){
    pwr = 255;
  }

  // motor direction
  int dir = 1;
  if(u<0){
    dir = -1;
  }

  // signal the motor
  setMotor(dir,pwr,PWM,IN1,IN2);

  // store previous error
  eprev = e;

  Serial.print(target);
  Serial.print(" ");
  Serial.println(pos);
}

void setMotor(int dir, int pwmVal, int pwm, int in1, int in2){
  analogWrite(pwm,pwmVal);
  if(dir == 1){
    digitalWrite(in1,HIGH);
    digitalWrite(in2,LOW);
  }
  else if(dir == -1){
    digitalWrite(in1,LOW);
    digitalWrite(in2,HIGH);
  }
  else{
    digitalWrite(in1,LOW);
    digitalWrite(in2,LOW);
  }  
}

void readEncoder(){
  int b = digitalRead(ENCB);
  if(b > 0){
    pos++;
  }
  else{
    pos--;
  }
}

you could attach an adafruit-i2c-fram memory device - save your data to it during normal operation - restore data on power up

You could save the position to Flash every time the motor stops, but depending on how often you start/stop, that could cause issues with the Flash write limits.
Alternately, you could have a pushbutton that says "I'm shutting down now, save my position."

Those are the two easy ways.

You don't say what Arduino you're using. Nano, Uno, Mega, look at saving to EEPROM.

Often the zero position is marked with a limit switch. That way if the motor is moved while the power is off it can still return to the same zero position.

Arduino Uno. I did mention it btw.
And what difference does it even make btw?

Sorry, I missed the reference. But, there are many Arduino products, only some of which have EEPROM, others have other mechanisms for non-volatile storage. Short answer is, my answer would be irrelevant and misleading if your particular Arduino didn't have EEPROM.
That said, have you now looked at the EEPROM tutorial?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.