Help with controlling a stepper motor

Hello Arduino users,

I’m new to Arduino as a PLC guy and try to understand the program code.
But it’s not easy if you are used to do it different like with PLC’s.
Can anybody put me in the right directions with the following program:

Let’s explain a little bit what i’m thinking of;

  • Power on
  • Input 8 will be connected to +5V
  • Motor will move (CW or CCW) untill input 10, ref switch is reached, this must be done before button 11 or 12 can work
  • When input 11 or 12, CCW or CW, will be 1, the motor will move as in the code below.

Who can help me with this so I understand the code?

Thanks in advance,

Arjan

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
int buttonCW=12; //define CW rotation of motor
int buttonCCW=11; //define CCW rotation of motor
int buttonREF=10; //Reference switch
int buttonSTART=9; // Activate programm after taking reference
int Safety=8; //Must be connected to +5V to let the programm work

void setup(){
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (buttonCW, INPUT);
pinMode (buttonCCW, INPUT);
pinMode (buttonREF, INPUT);
pinMode (buttonSTART, INPUT);
pinMode (Safety, INPUT);

}

void loop(){

for (int i=0; i<12800; i++) //Forward 4 rotations (3200 pulses per rotation)
{
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(100);
digitalWrite(PUL,LOW);
delayMicroseconds(100);
}

for (int i=0; i<12800; i++) //Backward 4 rotations (3200 pulses per rotation)
{
digitalWrite(DIR,HIGH);
digitalWrite(ENA,HIGH);
digitalWrite(PUL,HIGH);
delayMicroseconds(10);
digitalWrite(PUL,LOW);
delayMicroseconds(10);
}

}

exester:
Who can help me with this so I understand the code?

What happens when you run the program - please give as much detail as possible. What do you want it to do that is different?

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R
Stepper Motor Basics
Simple Stepper Code

Hello Robin2,

Thank you for your reply.

This is a basic code I found but this starts at power up and will go continuously moving backward and forward.
That works and I understand how it works.
So I’ve been busy the whole day finding code samples/explanations/manuals etc to find out how to build up the program as described in my first request.
What I found was a lot of samples for steppers using libraries and than with joystick control etc but not what I want to do.

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
int buttonCW=12; //define CW rotation of motor
int buttonCCW=11; //define CCW rotation of motor
int buttonREF=10; //Reference switch
int buttonSTART=9; // Activate programm after taking reference
int Safety=8; //Must be connected to +5V to let the programm work



void setup(){
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (buttonCW, INPUT);
  pinMode (buttonCCW, INPUT);
  pinMode (buttonREF, INPUT);
  pinMode (buttonSTART, INPUT);
  pinMode (Safety, INPUT);

}


void loop(){


  for (int i=0; i<12800; i++)    //Forward 4 rotations (3200 pulses per rotation)
  {
    digitalWrite(DIR,LOW);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(100);
    digitalWrite(PUL,LOW);
    delayMicroseconds(100);
  }
  
  for (int i=0; i<12800; i++)   //Backward 4 rotations (3200 pulses per rotation)
  {
    digitalWrite(DIR,HIGH);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(10);
    digitalWrite(PUL,LOW);
    delayMicroseconds(10);
  }
  
}

At the moment you have no code to read the switches so the next step is to leave the stepper code as it is and add some code to read the switches, save their states to variables and print the values on Serial Monitor.

Your description in these lines (from the Original Post) is not very clear. I suggest you think of the problem as a series of actions and write them down with one action on each line. When you have a very clear description of the requirement the programming will be much easier.

Have a look at Planning and Implementing a Program. By breaking the code into several short single-purpose functions it will be much easier to develop, test, debug and maintain. For example your code in loop() might be as simple as

void loop() {
   readSwitches();
   updateSystemState(); // depending on state of switches
   updateMotorMoves(); // depending on the system state
   operateMotor();
}

...R

exester:

  • Motor will move (CW or CCW) until input 10, ref switch is reached, this must be done before button 11 or 12 can work

So it should be done in setup(). WHICH DIRECTION?!? If it doesn't matter, PICK ONE. If it does matter, how do you determine which one?

To do the move, set the DIR and ENA outputs, then 'while' the switch is not activated, pulse the PUL output. This would typically be done with a 'while' loop.

exester:

  • When input 11 or 12, CCW or CW, will be 1, the motor will move as in the code below.

"The code below" repeatedly moves forward (CW?) four turns and backward (CCW?) four turns. What you wrote implies that either button will cause it to "repeatedly move CW four turns and CCW four turns" which doesn't sound useful. Did you mean that the CW button causes it to move CW four turns and the CCW button causes it to move CCW four turns? If that is what you want to happen, that is what you should write. If the CW button is held down, should it continue to move CW as long as the button is down? If not, you have to say that, too.

exester:

int Safety=8; //Must be connected to +5V to let the programm work

To do that you must check the input before every step. If the Safety pin reads LOW you will have to freeze the program, perhaps with a "while(true);" statement.