Go Down

Topic: how can i transfer this to become a function? (Read 527 times) previous topic - next topic

ash901226

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
Code: [Select]
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

Grumpy_Mike

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

robtillaart

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

Code: [Select]
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?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ash901226

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.

Grumpy_Mike


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

ash901226

rite now i have this sketch
Code: [Select]

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
Code: [Select]
  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);

Grumpy_Mike

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++

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy