Question on stepper motor functionality

At initial when i uploaded the code, the stepper motor able to step 6 times and when i unplugged my usb power and plugged in again the motor itself step backwards once and move forward 5 times. can someone explain to me what's going on. Below is my coding.

#include <Stepper.h>

const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2,3,4,5);            

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
  
   for(int i = 0; i <= 12; i++){
    
    myStepper.step(1);
    Serial.print("steps:" );
    Serial.println(stepCount);
    stepCount++;
    delay(500);
  }  
}
#include <Stepper.h>

const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2,3,4,5);            

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);

  
}
void loop()
{
  for(int i = 0; i <= 12; i++)
  {

    myStepper.step(1);
    Serial.print("steps:" );
    Serial.println(stepCount);
    stepCount++;
    delay(500);
  }  
}

you dont have loop function in your code. JUst upload this code

You may find some useful stuff in stepper motor basics

...R

Hi AMPS,

This is actual code that i wanted to run it. For example, at time 8.15am it moves 6 times perfectly then i unplugged the usb power and re-plug in what happened was the motor will move backward once and move forward 5 times.

#include <Time.h> 
#include <TimeAlarms.h>
#include <Stepper.h>

const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 2,3,4,5);                                       // for your motor
int stepCount = 0;   


void setup()
  {
   Serial.begin(9600);
  setTime(8,29,0,1,1,11);
  Alarm.alarmRepeat(1,13,0,MorningMotor);
  Alarm.alarmRepeat(17,45,0,EveningMotor); 
  }
  
 void loop()
{
  
  Alarm.delay(1000);
} 
  
  void MorningMotor(){
  
  for(int i = 0; i <=6; i++){
    
    myStepper.step(1);
    Serial.print("steps:" );
    Serial.println(stepCount);
    stepCount++;
    delay(500);
  }  
  }
   void EveningMotor(){
  
  for(int i = 0; i <=6; i++){
    
    myStepper.step(1);
    Serial.print("steps:" );
    Serial.println(stepCount);
    stepCount++;
    delay(500);
  } 
   }

In case of real time application you need to settime only once. Once time set it run automatically

When you calling function settime() in setup everytime when you on arduino it set time ; when you connect first time it run fw; when you remove & plug it it might find different time. SO i recommend once time is set you uncomment the line & upload the code/