Pages: [1]   Go Down
Author Topic: how can i transfer this to become a function?  (Read 456 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33362
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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?
Logged

Rob Tillaart

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33362
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

rite now i have this sketch
Code:
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:
  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);
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33362
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: