Remembering and storing last value

Hi!

I am totally new to programming and I am trying to use a stepper motor to build a controller for opening and closing a latch. I am doing this through a the motor turning a screw which pushes a nut that is attached to the latch. my idea is that I can calculate exactly how many rotations that is needed to fully close/open the latch and therefor know how many percent open the latch is by knowing how many rotations in the two directions the motor has taken.

My problem is I am having a hard time storing the number of rotation the motor has done. I have tried to Google but not been able to make it work. I have been unsuccessful creating a variable (in my case the "oldpo" variable) that updates is self with a new value.

The result of my code below is that the motor turns the inputted amount of steps in one direction, and does the same procedure the other direction. It does not save the value in "oldpo".

Thanks in advance!

#include <Stepper.h>

int in1Pin = 11;
int in2Pin = 10;
int in3Pin = 9;
int in4Pin = 8;

int newpo;
int static oldpo;
int steps;

Stepper motor(2038, in1Pin, in2Pin, in3Pin, in4Pin);

void setup() {
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);
Serial.begin(9600);
motor.setSpeed(2);
}

void loop() {
if (Serial.available()){
currentpo= Serial.parseInt();
steps=newpo-oldpo;
oldpo=newpo;
motor.step(steps);
Serial.println ("newpo:");
Serial.println (newpo);
Serial.println ("oldpo:");
Serial.println (oldpo);
}
}

Cheers Henning

See my comments

   currentpo = Serial.parseInt();  //currentpo is never used after reading
    steps = newpo - oldpo;    //oldpo and newpo are both zero so steps will now be zero
    oldpo = newpo;  //both still zero

Thanks!
Changed the currentpo to newpo. Will try it when I get back home!

still same problem..

Post the new code, using code tags (as described in "How to use this forum").

Hi, I edited the code in the first post but will use the code tag next time!

henninghafr:
Hi, I edited the code in the first post but will use the code tag next time!

no, next time, you will post the new code in code tags and leave the original code as it was.

Ok! here is the new code. The code in the first post is in its original.

#include <Stepper.h>
 
int in1Pin = 11;
int in2Pin = 10;
int in3Pin = 9;
int in4Pin = 8;

int newpo;
int static oldpo;
int steps;

Stepper motor(2038, in1Pin, in2Pin, in3Pin, in4Pin);  

void setup() {
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(in3Pin, OUTPUT);
  pinMode(in4Pin, OUTPUT);
  Serial.begin(9600);
  motor.setSpeed(2);
}

void loop() {
 if (Serial.available()){
 newpo= Serial.parseInt();
 steps=newpo-oldpo;
 oldpo=newpo;
 motor.step(steps);
 Serial.println ("newpo:"); 
 Serial.println (newpo);  
 Serial.println ("oldpo:");  
 Serial.println (oldpo);  
}
}

I'm not quite sure what you think the problem is, but the code could certainly be part of it.

Do you expect "oldpo" and "newpo" to be different, after setting them to be equal?

 oldpo=newpo;
 motor.step(steps);
 Serial.println ("newpo:");
 Serial.println (newpo); 
 Serial.println ("oldpo:"); 
 Serial.println (oldpo);

Nope I was just checking if they had the same value or not.

I want to transfer the new position (which is entered manually in serial) to the oldpo variable so that the information can be used the next time I want to enter a new position and calculate how many steps the motor has to take to get to the new position

That is what the program does. I don't understand what you think the problem is.