Trouble using multiple if statements with acceltepper library

For this project I am trying to make a stepper motor, for which a revolution is equal to 513 steps, point towards a certain point and take the shortest distance to get there. For now I am using 0 deg as my point, that is I always want the motor to end up point towards 0 deg, 360 deg, 720 deg, etc. If the motor rotates, 200 steps for example, I would like it to correct itself and move backwards towards 0 ( move -200 steps since moving backwards would be the shortest distance). If the motor has moved 600 steps move backwards 87 steps to be at 0 deg or 513 steps. If the motor stops at a position greater or equal than 180 deg, the motor would move forward as that would be the shortest distance to get to 360 deg. So if the motor moves 400 steps it would then move 113 steps forward.

The way that my code currently works is that it first moves the motor the inputted amount of steps, e.g., if 600 is inputted the motor will rotate 600 steps forward. Instead of continuing to run and correct itself, that is move to 600 then move backwards 87 steps or some variation of this, I have to input another number into the serial monitor (any number) for it to to correct itself and point towards 0/360 deg. When inputting the second time it will not move the inputted amount but will instead correct itself to point towards 0/360 deg.

I have tried moving around the position of the if statements and using while loops as well. However, what ends up happening is that the motor will not move at all when I input a number to the serial.

Quick Recap: I would like for the motor to first move to the position inputted into the serial monitor. After moving the given amount of steps the motor should correct itself to move towards 0/360 deg taking the shortest distance to get there. I am having trouble getting the program to do this in one loop.

For my project I am using the accelstepper library and the Adafruit motor shield library. Any help would be greatly appreciated thanks.

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper = AFMStop.getStepper(513, 1);



void forwardstep()
{
	myStepper->onestep(FORWARD, SINGLE);
}
void backwardstep()
{
	myStepper->onestep(BACKWARD, SINGLE);
}


AccelStepper stepper(forwardstep, backwardstep);
int cnt;

void setup()
{
	AFMStop.begin(); // Start the top shield
	Serial.begin(9600);
	stepper.setMaxSpeed(50.0);
	stepper.setAcceleration(100.0);
	//stepper.moveTo(257);
	cnt = 0;
}

void loop()
{
	if (Serial.available() > 0)
	{
		int dist = Serial.parseInt();
		int posi = stepper.currentPosition() % 513;
		int curPos = stepper.currentPosition();
		if (dist != 0 )
		{
			stepper.moveTo(curPos + dist);
			if(posi < 256)
			{
				stepper.moveTo(curPos - posi);
			}
			if(posi >= 256)
			{
				stepper.moveTo(curPos + (512 - posi));
			}
			Serial.println(dist);
			serialFlush();
		}
	}

	stepper.run();
	Serial.println(stepper.currentPosition());

}

void serialFlush()
{
	while(Serial.available() > 0)
	{
		char t = Serial.read();
	}
}

513 steps per revolution is a very weird number. How did you arrive at that value? 512 would make some sense.

      int curPos = stepper.currentPosition();
      if (dist != 0 ) {
        stepper.moveTo(stepper.currentPosition()+dist);

What was the purpose of reading and storing the current position, if you aren’t going to use the stored value?

        if(posi<256){
         stepper.moveTo(curPos-posi);
        }
        if(posi>= 256){
         stepper.moveTo(curPos + (512-posi));
        }

Are there some values that posi can contain that are not less than 256 or greater than or equal to 256?

Your indenting makes it hard to follow your code. Put every { on its own line. Use Tools + Auto Format to properly format your code.

Printing out the current position, the new position (which is NOT a distance), and the number of steps to take might provide some clues.

PaulS: 513 steps per revolution is a very weird number. How did you arrive at that value? 512 would make some sense.

513 was taken for the manufacturer who says there are 513 steps per revolution.

I went ahead and changed the first part as you are correct. I also went ahead and formatted the code to hopefully be easier to read.

Are there some values that posi can contain that are not less than 256 or greater than or equal to 256?

I am not sure I really understand this question. Posi is current position mod 513, as such the answer will always be between 0 and 512. Even if posi is a large number such as 3000 my answer will be within 0 and 512. Therefore posi will always fall into either being less than 256 or greater than or equal to 256.

I will definitely try printing out the new position and the number of steps as you suggested. The current position is already printed out.

I am not sure I really understand this question. Posi is current position mod 513, as such the answer will always be between 0 and 512.

Exactly, so

if(posi > 256)
{
   // do one thing
}
else
{
  // do the other
}

When there are only two possibilities, if the first one is true the second one can't be. When the first is false, the second must be true. So there is no need to test that second is true.

The current position is already printed out.

But only after it is no longer the current position...