AccelStepper rotation from 359° to 1°

Hello,
I have to spin a wing so that it always follows the shortest path in a round corner. My code from 359 to 1 and vice versa, instead of making 2 degrees of rotation, makes 358.
How can I always make him follow the shortest path?

myStepper.run();
WingAngle = AWA*gear_ratio;
myStepper.moveTo(WingAngle/stepPerAngle);

With such limited code,I am just going to guess that your division is truncated, so you will always go to the next lower. If rounding is ok, you can always add so that you get an extra ,5 before you truncate, making your rounding more exact. otherwise you need to add a little more complexity to your algorithm.

delta_angle = finish_angle-start_angle;
if (delta_angle > 180) {
change_rotation_direction();
delta_angle = 360-delta_angle;
}

I can set with a rotary encoder (constrain 0-360) the angle (AWA) I want the wing to go to.
My problem is that when the encoder passes from 0 to 359 or vice versa, the wing rotates 358 deg instead 2 deg. I want the wing to make the shortest rotation

Did you notice reply #2?

Yes, I wrote something similar, the problem is on the change_rotation_direction() function. How can i do that? Can you also suggest me an adequate library? Now I’m using the AccelStepper library.

Thanks

if(WingAngle - WA_p <= 180) {
        myStepper.run();
        WingAngle_aux = AWA*gear_ratio;
        myStepper.moveTo(WingAngle_aux/stepPerAngle);
        
        digitalWrite(green_led, HIGH);
        digitalWrite(red_led, LOW);
        digitalWrite(yellow_led, HIGH);
      } else {
        WingAngle = AWA*gear_ratio;
        WingAngle_aux = (-360 + AWA)*gear_ratio;
        myStepper.moveTo(WingAngle_aux/stepPerAngle);
        myStepper.runToPosition();
        myStepper.run();
  
        digitalWrite(green_led, LOW);
        digitalWrite(red_led, HIGH);
        digitalWrite(yellow_led, HIGH);

        delay(5000);
      }

IMHO It's not a library problem. As I suggested you on italian forum (did you read it??), you just need to calculate the direction based on current and new angle, and don't use moveTo() function (requiring absolute angle) but just .move() with relative angle.

So, I'd start by creating an "int relativeAngle(int current, int new)" to return the positive/negative "delta" value based on current and new angle, then test it with a simple test sketch showing the result for regular and "critical" changes.
Something like this:

int curAngle;
void setup() {
  Serial.begin(9600);
  // Initial position
  curAngle = 0;
  // Test
  Serial.println( relativeAngle(358) ); // Should return -2
  Serial.println( relativeAngle(2) ); // Should return 4
  Serial.println( relativeAngle(358) ); // Should return -4
  Serial.println( relativeAngle(350) ); // Should return -8
  Serial.println( relativeAngle(100) ); // Should return -250
  Serial.println( relativeAngle(2) ); // Should return -98
}

void loop() {

}

int relativeAngle(int newAngle) {
  int delta;

  // Put your code here
  ...

  // Update current angle
  curAngle = newAngle;
  // Return relative angle
  return delta;
}

(you could even test it on TinkerCad)
When "relativeAngle()" is working, you can add it to your program and use it with a few changes (but remember it's a relative angle, so don't use .moveTo()!).

the problem is on the change_rotation_direction() function

That is just to remind you that you need to arrange for the stepper to move the other direction, that many degrees.

Consult the AccelStepper library documentation.