Speed and direction of a stepper motor with push buttons

Hi I am working on a project and I'm not familiar with arduino at all. I have two push buttons and was looking to control the direction of the motor with each push button. In my code i want to control the speed of the direct. I would like p2buttonState to go faster then p1buttonState. I had it working before but now for some reason it wont work. Heres the code im using.

// defines pins numbers
const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;

const int switchOne = 8;
const int switchTwo = 9;

int p1buttonState = 0; // current state of the button
int lastp1buttonState = 0; // previous state of the button

int p2buttonState = 0; // current state of the button
int lastp2buttonState = 0; // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;

void setup() {

Serial.begin(9600);
pinMode( switchOne, INPUT_PULLUP);
pinMode( switchTwo, INPUT_PULLUP);

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

pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);

}
void loop() {
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
//if (p1buttonState != lastp1buttonState) {
// forward

if (p1buttonState == LOW) {
  digitalWrite(dirPin,HIGH);
  // printSerialMessage("Setting Forward Command");
  digitalWrite(stepPin, HIGH);
  // printSerialMessage("RUN");
}
if (p1buttonState == HIGH) {
  digitalWrite(stepPin, LOW);
  // printSerialMessage("STOP");
}
delayMicroseconds(5000);
// lastp1buttonState = p1buttonState;

//}
//if (p2buttonState != lastp2buttonState) {
// reverse

if (p2buttonState == LOW) {
  digitalWrite(dirPin,LOW);
  // printSerialMessage("Setting Reverse Command");
  digitalWrite(stepPin, HIGH);
  // printSerialMessage("RUN");
}
if (p2buttonState == HIGH){
  digitalWrite(stepPin, LOW);
  // printSerialMessage("STOP");
}
delayMicroseconds(500);
 //lastp2buttonState = p2buttonState;

//}
}

  • Please follow the Forum’s posting guidelines.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

1 Like
// defines pins numbers
const int dirPin  = 3;
const int stepPin = 4;
const int enPin   = 5;

const int switchOne     = 8;
const int switchTwo     = 9;

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;

void setup() {

  Serial.begin(9600);
  pinMode( switchOne, INPUT_PULLUP);
  pinMode( switchTwo, INPUT_PULLUP);

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

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);

}
void loop() {
  p1buttonState = digitalRead(switchOne);
  p2buttonState = digitalRead(switchTwo);
  //if (p1buttonState != lastp1buttonState) {
    // forward
    
    if (p1buttonState == LOW) {
      digitalWrite(dirPin,HIGH);
      // printSerialMessage("Setting Forward Command");
      digitalWrite(stepPin, HIGH);
      // printSerialMessage("RUN");
    }
    if (p1buttonState == HIGH) {
      digitalWrite(stepPin, LOW);
      // printSerialMessage("STOP");
    }
    delayMicroseconds(5000);
    // lastp1buttonState = p1buttonState;
  //}
  //if (p2buttonState != lastp2buttonState) {
    // reverse
    
    if (p2buttonState == LOW) {
      digitalWrite(dirPin,LOW);
      // printSerialMessage("Setting Reverse Command");
      digitalWrite(stepPin, HIGH);
      // printSerialMessage("RUN");
    }
    if (p2buttonState == HIGH){
      digitalWrite(stepPin, LOW);
      // printSerialMessage("STOP");
    }
    delayMicroseconds(5000);
     //lastp2buttonState = p2buttonState;
  //}
}

1 Like
  • Please supply a schematic.

  • We are sure you realize the delay(5000) lines are a big problem when it comes to program response time.

1 Like

Ehh... delayMicroseconds(5000);....

1 Like

Don't ask for 2 different actions in one reply, #2! Utterly few OPs manage to respond to that.

2 Likes
  • Yes, I noticed that too :sunglasses:

Too many times here.... The same happens in Email to many people too. The last missing institution vas the Police.....

  • I always put a new thought or a new request on a separate bulleted line thinking it might turn on a light in one’s head; it does not. :grimacing:

Haha. We have seen the same. There's no way but being "social"...... keep on posting.

I saw both bullet points dont worry. I just didnt have any schematics. Im doing this as a project for my job so sorry for the delay. I will get some schematics shortly.

  • Is there a common between the Arduino GND and the 24v GND ?

  • At first glance, 24v seems high :thinking:

  • Switch wiring seems contorted ? :woozy_face:

  • The 24v is to power the stepper motor it is not connected to the Arduino at all.
  • I have a 24 to 12v step down to power the Arduino.
  • the switch wiring share a ground that's it. They are connected to 8 and 9 on the Arduino.
  • It am trying to control the direction the motor turns with while pushing and holding the button down. This set up and code does that I just need to be able to change the speed for both directions.

The code looks like it does only one step per button:

autoformatted Code
// defines pins numbers
const int dirPin  = 3;
const int stepPin = 4;
const int enPin   = 5;

const int switchOne     = 8;
const int switchTwo     = 9;

int p1buttonState = 0;         // current state of the button
int lastp1buttonState = 0;     // previous state of the button

int p2buttonState = 0;         // current state of the button
int lastp2buttonState = 0;     // previous state of the button
bool bPress = false;

bool isForward = false;
bool isBackward = false;

void setup() {

  Serial.begin(9600);
  pinMode( switchOne, INPUT_PULLUP);
  pinMode( switchTwo, INPUT_PULLUP);

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

  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);

}
void loop() {
  p1buttonState = digitalRead(switchOne);
  p2buttonState = digitalRead(switchTwo);
  //if (p1buttonState != lastp1buttonState) {
  // forward

  if (p1buttonState == LOW) {
    digitalWrite(dirPin, HIGH);
    // printSerialMessage("Setting Forward Command");
    digitalWrite(stepPin, HIGH);
    // printSerialMessage("RUN");
  }
  if (p1buttonState == HIGH) {
    digitalWrite(stepPin, LOW);
    // printSerialMessage("STOP");
  }
  delayMicroseconds(5000);
  // lastp1buttonState = p1buttonState;
  //}
  //if (p2buttonState != lastp2buttonState) {
  // reverse

  if (p2buttonState == LOW) {
    digitalWrite(dirPin, LOW);
    // printSerialMessage("Setting Reverse Command");
    digitalWrite(stepPin, HIGH);
    // printSerialMessage("RUN");
  }
  if (p2buttonState == HIGH) {
    digitalWrite(stepPin, LOW);
    // printSerialMessage("STOP");
  }
  delayMicroseconds(5000);
  //lastp2buttonState = p2buttonState;
  //}
}

...and with two unconditional delays of 5000us per button check it is limited to a top pulsing speed of 1/(2*0.005)/2=50Hz, provided you can press the buttons that fast.

It is unclear what you are trying to do.

Maybe look into the Mobatools Example #3 or one of the other stepper motor library examples:

It does not do one step per button push. if you hold the button the motor moves continuously. the delay is what controls the speed of the the motor.

Ah. I see. The step pin is brought low each cycle if one of the buttons is not pressed. One cycle per loop is 1/(2*0.005)=100Hz

If you want one delay to be different than the other, put the delays inside of the conditional, right next to where you set the stepPin high. You don't really need to test the other button isn't pressed in order to bring the step pin LOW. You could put that in the conditional statement as well.

For instance, here's a completely untested snippet:

  if (p1buttonState == LOW) {
    digitalWrite(dirPin, HIGH);
    // printSerialMessage("Setting Forward Command");
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);  // DM556 needs only 2.5us vs digitalWrite's 3.4us.
    delayMicroseconds(5000);
    // printSerialMessage("RUN");
  }

That will get you to 1/0.005=200Hz

Thanks! that worked I was able to use that for both directions and control the speeds on each direction.

Regarding PM: Yes a homing switch is possible, but it requires more complicated logic since you have to keep track of position.

Consider these MobaTools examples from above: