Rotating 0 to 180 degrees fine on first input but its rotating 180 to 0 degrees on second input not 180 to 360 degrees. How to rotate object 180 degrees on every input smoothly?

i am trying to rotate object 180 degrees on every input smoothly, here in my code object rotating 0 to 180 degrees fine on first input but its rotating 180 to 0 degrees on second input not 180 to 360 degrees

    public float speed = 0.9f;
    private Quaternion to;
    private void Start()
    {
        to = transform.rotation;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {

            RotateTwo();
        }

        if (Input.GetKeyDown(KeyCode.D))
        {

            RotateOne();
        }


        transform.rotation = Quaternion.Lerp(transform.rotation, to, speed * Time.deltaTime);

    }


    void RotateOne()
    {
        to *= Quaternion.Euler(0, 0, 180);
    }

    void RotateTwo()
    {
        to *= Quaternion.Euler(0, 0, -180);
    }

Lerp will always find the shortest path to rotate to the desired rotation. Because 360 and 0 actually refer to the exact same rotation lerp will always move between 180 and 0 in the same arc. Because rotating clockwise and anti clockwise between 0 and 180 is exactly the same distance it just arbitrarily picks one arc. If you were to rotate 90 degrees, there is only the one shortest path so you won’t have the problem. You could try first rotating 90 degrees, to nudge the object in the right direction, then rotate to the 180 degree position, as you will now closer in one direction, the lerp will move in the correct direction.