Stepper motor basic control.

Not having ever written code for Arduino before - and I am a little overwhelmed. I have managed to use a bit of code from one place, put another in from another. Managed to get a Nema 23 running like I almost want too. But for some reason I am unable to just do one little thing.

The aim of the project for now, is to press a button, the stepper rotates in a set number of turns, pauses, and rotates back the same. I would like to add in a few limit switches and some safety bits and pieces but for now just that.

It works. But, the “stop” only stops the cycle when I have 5vdc applied to pin 9. I have been trying to have this the other way. Apply 5vdc to pin 9, the stepper cycles once in each direction and then stops, waiting for the next cycle. Know what I mean? Much safer that way. Can someone help me out with this I have tried quite a lot to sort and am missing something huh.

// defines pins numbers
const int stepPin = 5; 
const int dirPin = 2; 
const int enPin = 8;
const int buttonPin = 9;     // the number of the pushbutton pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
void setup() {
  
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}
  

void loop() {

    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // rotate stepper one cycle forward one cycle back:
    digitalWrite(enPin, HIGH);
  } else {
    // turn motor cycle off:
    digitalWrite(enPin, LOW);
  
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 400 pulses for making one full cycle rotation
  for(int x = 0; x < 16000; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }
  delay(1000); // One second delay

  digitalWrite(dirPin,LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for(int x = 0; x < 16000; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(1000);

  }}

Hi,
Welcome to the forum,
Thanks for putting your code in .

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got your button wired, does it have a pullup or pulldown resistor?
Also tell us what hardware and power supply you are using?
Spec/data on stepper and driver.
What model Arduino are you using?

Thanks… Tom… :slight_smile:

Have a look at "state machine" for encoding sequences of actions.

It is more usual (better practice) to wire a button pin so that pressing the button shorts the circuit to GND. If you use

pinMode(pin, INPUT_PULLUP);

this is easy to achieve. It also means that the pin is not floating in an un-defined state when there is no external connection. If you connect a button this way then digitalRead(buttonPin); will show LOW when the button is pressed and HIGH otherwise.

If you want your motor to go through a cycle following single brief press of the button then you need a variable to record the fact that the button was pressed and the motor should move. Something like this

buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
   motorShouldRun = true;
}

and then the code for running the motor would include a test such as this pseudo code

if (motorShouldRun == true) {
    // code to run the motor
    if the motor cycle is complete
      motorShouldRun = false;
}

...R

Ok thanks for that. I will assemble it again in the weekend and take some pictures. I agree with the pin to ground, I just couldnt seem to get the code to accept that input. And basically, in the sense of this machine, I want it to not operate in event of power failure or fault. Its kind of a dangerous exercise, which is why I am using a stepper to make the moves instead of my hands.

The code you wrote Robin2, thanks for that. I am such a new coder I will have to study just in order to get that to work.

The main thing I guess was getting the stepper to run - that part was easy enough.

Rgds B

Please draw schematic circuit diagrams by hand, photos are not very helpful.

The setup is using a Nema 23 stepper with a DM556T driver. I have a 300W 36v power supply. The Arduino board is a Ëleven"

I have tried to change the code, and added in a pullup resistor so that a ground to pin 9 will rotate the motor an amount of times in one direction, back in the other direction. Then should stop and wait for a button press again.

But, what I get is '"“expected unqualified-id before” So I am stumped.

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
const int buttonPin = 9;     // the number of the pushbutton pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
void setup() {

  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}


void loop() {

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) 
  // rotate stepper one cycle forward one cycle back:
  digitalWrite(enPin, HIGH);
   } else {
  // turn motor cycle off:
  digitalWrite(enPin, LOW);

  digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction
  // Makes 400 pulses for making one full cycle rotation
  for (int x = 0; x < 400; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000); // One second delay

  digitalWrite(dirPin, LOW); //Changes the rotations direction
  // Makes 400 pulses for making two full cycle rotation
  for (int x = 0; x < 400; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
  delay(1000);

}

Hi,
With your code you need to count your { and } pairs.
You have some missing.
Select TOOLS/AUTO-FORMAT to get your code to indent.

Hint: Your first if statement should have a { bracket in it.

Tom... :slight_smile: