how can i transfer this to become a function?

hello, I'm Ash from Malaysia. I need help in making this stepper step into a function. the part that I am showing is only the part for the stepper to step. I am using the Step/Dir Stepper controller type

void loop()
{
  switch (state1)
  {
    case 1:
    digitalWrite (pinLED[0],HIGH);
    Timer[0]=millis();
    state1=2;
    break;
    case 2:
    if ( millis() - Timer[0] >= TimerON[0])
    {
      state1=3;
    }
    break;
    case 3:
    digitalWrite (pinLED[0],LOW);
    Timer[0]=millis();
    state1=4;
    break;
    case 4:
    if ( millis() - Timer[0] >= TimerOFF[0])
    {
      Count1--;
      if (Count1>0){state1=1;}
      else {state1=5;}
    }
    
    break;
    case 5:
    digitalWrite (pinLED[0],LOW);
    break;
    default:
    state1=1;
    break;
  }

any help would be great and i thank you in advance

That code is already a function called loop() to make it into a function called step change the word loop to step.

If you rewrite it to a function please take care of some indenting makes it more readable

void step()
{
    switch (state1)
    {
    case 1:
        digitalWrite(pinLED[0], HIGH);
        Timer[0] = millis();
        state1 = 2;
        break;
    case 2:
        if ( millis() - Timer[0] > =  TimerON[0])
        {
            state1 = 3;
        }
        break;
    case 3:
        digitalWrite(pinLED[0], LOW);
        Timer[0] = millis();
        state1 = 4;
        break;
    case 4:
        if ( millis() - Timer[0] >=  TimerOFF[0])
        {
            Count1--;
            if (Count1>0){state1 = 1;}
            else {state1 = 5;}
        }
        break;
    case 5:
        digitalWrite(pinLED[0], LOW);
        break;
    default:
        state1 = 1;
        break;
    }
}

I noticed now that after state1== 5 the system has reached its end state... is that correct?

hello robtillaart yes when State1==5 it means the end of the step sequence. however i have think that it is better that i make the break earlier by using if and making the whole step inside the if.

ash901226: yes when State1==5 it means the end of the step sequence.

In which case once it reaches that state it will never repeat. If you want it to repeat then have a state1=1 in case 5

rite now i have this sketch

void loop()
{
  switch (state1)
  {
    case 1:
    digitalWrite (pinLED[0],HIGH);
    Timer[0]=millis();
    state1=2;
    break;
    case 2:
    if ( millis() - Timer[0] >= TimerON[0])
    {
      state1=3;
    }
    break;
    case 3:
    digitalWrite (pinLED[0],LOW);
    Timer[0]=millis();
    state1=4;
    break;
    case 4:
    if ( millis() - Timer[0] >= TimerOFF[0])
    {
      Count1--;
      if (Count1>0){state1=1;}
      else {state1=5;}
    }
    
    break;
    case 5:
    digitalWrite (pinLED[0],LOW);
    break;
    default:
    state1=1;
    break;
  }
  switch (state2)
  {
    case 1:
    digitalWrite (pinLED[1],HIGH);
    Timer[1]=millis();
    state2=2;
    break;
    case 2:
    if ( millis() - Timer[1] >= TimerON[1])
    {
      state2=3;
    }
    break;
    case 3:
    digitalWrite (pinLED[1],LOW);
    Timer[1]=millis();
    state2=4;
    break;
    case 4:
    if ( millis() - Timer[1] >= TimerOFF[1])
    {
      Count2--;
      if (Count2>0){state2=1;}
      else {state2=5;}
    }
    break;
    case 5:
    digitalWrite (pinLED[1],LOW);
    break;
    default:
    state2=1;
    break;
  }
}

what it does is it actually a blink without delay using Finite State machine. I know maybe useless but it does make me understand the 2 concept better. anyway i have come to a point where i want to use this code to control a stepper controller that need only step/dir logic only. the problem is how could i make this into a function that could be easily call up like maybe step(pin number,number of step) whereby the function is only

  switch (state1)
  {
    case 1:
    digitalWrite (pinLED[0],HIGH);
    Timer[0]=millis();
    state1=2;
    break;
    case 2:
    if ( millis() - Timer[0] >= TimerON[0])
    {
      state1=3;
    }
    break;
    case 3:
    digitalWrite (pinLED[0],LOW);
    Timer[0]=millis();
    state1=4;
    break;
    case 4:
    if ( millis() - Timer[0] >= TimerOFF[0])
    {
      Count1--;
      if (Count1>0){state1=1;}
      else {state1=5;}
    }
    
    break;
    case 5:
    digitalWrite (pinLED[0],LOW);
    break;
    default:
    state1=1;
    break;
  }

how could i call this function and use it again for another pin like step(2,200); step(3,400);

Implement it not as a function but as a C++ class. Each pin will need its own state variable. Look at the stepper libary to see how this is done, or read a book on C++