BYJ48 STEPPER MOTOR Embedded C help?

Hey guys newbie here!

So I’ve got this project I’m doing that involves the BYJ48 stepper motor and using an Arduino… but without its libraries and only embedded C code.

So far I’ve been trying to get the motor to rotate 360 in one direction and then in the other, which I sort of got working. Here’s the code.

#include <avr/io.h>


int Steps = 0;      // Start at no steps in sequence
int Direction = 0; //Defaults at clockwise
int stepsleft = 4095; //<----Calculate the total number of steps per revolution

void setup() {
    DDRD |= (1 << DDD1) | (1 << DDD2) | (1 << DDD3) | (1 << DDD4); //<-----Choose which pins to set as output for the motor, avoid using the digital pins the LCD uses.
  TCCR0A = 0b0000000;
  TCCR0B &= (0 << CS02);
  TCCR0B |= (1 << CS01);
  TCCR0B |= (1 << CS00);
  //<-------- Here, set control register A for the normal mode of operation.
  //<-------- Here, set control register B for the prescaler values choose wisely to get 1ms.
  TCNT0 = 0; //Initial counter at 0

}

void loop() {
  while (stepsleft > 0) {
    if (TCNT0 >= 250)      //<--- Decide Timer counter value to step at, goal is 1ms
    {
        stepper(1);         //<--- Step a certain amount
        stepsleft--;        //<--- Decrease Step count
        TCNT0 = 0;
    }
  }
        Steps = 0;

  Direction = !Direction; // Reverse Direction
  stepsleft = 4095;   //<---- Reset to initial value
}

/* This function handles the digital pin toggles to drive the motor */
void stepper(int i) {
  for (int x = 0; x < i; x++)
  {
    switch (Steps)    // This switch case will handle the digital pins to toggle for each step, refer to the step sequence table for pin values.
    { // Make sure you are toggling the correct pins
      case 0:
        PORTD &=~ (1 << PORTD0);
        PORTD &=~ (1 << PORTD1);
        PORTD &=~ (1 << PORTD2);
        PORTD |=  (1 << PORTD3);          //<----- Insert Digital Pin allocations for steps 1-7 Remember do not set the whole byte because the LCD uses certain digital pins.
        break;
      case 1:
       PORTD &=~ (1 << PORTD0);
        PORTD &=~ (1 << PORTD1);
        PORTD |= (1 << PORTD2);
        PORTD |=  (1 << PORTD3);
        break;
      case 2:
       PORTD &=~ (1 << PORTD0);
        PORTD &=~ (1 << PORTD1);
        PORTD |= (1 << PORTD2);
        PORTD &=~  (1 << PORTD3);

        break;
      case 3:
       PORTD &=~ (1 << PORTD0);
        PORTD |= (1 << PORTD1);
        PORTD |= (1 << PORTD2);
        PORTD &=~  (1 << PORTD3);
        break;
      case 4:
          PORTD &=~ (1 << PORTD0);
        PORTD |=(1 << PORTD1);
        PORTD &=~ (1 << PORTD2);
        PORTD &=~ (1 << PORTD3);
        break;
      case 5:
        PORTD |= (1 << PORTD0);
        PORTD |= (1 << PORTD1);
        PORTD &=~ (1 << PORTD2);
        PORTD &=~  (1 << PORTD3);
        break;
      case 6:
       PORTD |= (1 << PORTD0);
        PORTD &=~ (1 << PORTD1);
        PORTD &=~ (1 << PORTD2);
        PORTD &=~  (1 << PORTD3);
        break;
      case 7:
        PORTD |= (1 << PORTD0);
        PORTD &=~ (1 << PORTD1);
        PORTD &=~ (1 << PORTD2);
        PORTD |=  (1 << PORTD3);
        break;
      default:
        PORTD &= ~(1 << PORTD0);
        PORTD &= ~(1 << PORTD1);
        PORTD &= ~(1 << PORTD2);
        PORTD &= ~(1 << PORTD3); //<----Default pins select are off
        Serial.print("check");
        break;
        TCNT0=0;

    }
        SetDirection();
  }
}

/* This Function handles the resetting of direction, when reaching the end of the rotation */
void SetDirection(){
      if(Direction==1)
      { 
        Steps++;
      }
      else
      { 
        Steps--; 
            }
      if(Steps>7){
        Steps=0;
      }
      if(Steps<0)
      {
        Steps=7; 
      }
}

The problem I’m having is if I take the “Steps = 0” at the beginning of the void loop the motor will complete the cycle once and then do another rotation, freeze, then continue rotating in the SAME direction rather then changing direction.

I have no idea why that line fixes the issue, or what is causing that issue in the first place. Also another weird thing is halving the step amount (stepsleft variable) stops it from changing direction altogether. I feel like the timers have something to do with it, which is an area I barely understand.

Thanks for any help guys!