Stepper Motor turns once when powering up Arduino Mega

I have wired up an Arduino Mega to have two push buttons to rotate a stepper motor one revolution clockwise or counter clockwise depending on which button is pressed. The problem I am having is that the stepPin is sending a signal on startup of the Mega that is causing an initial revolution of the motor. When I unplug pin 4 and start it up, the motor will not turn. Do the digital pins on the Mega energize on startup or is it the program. I got the program off of Youtube. Thanks for looking at this.

// 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() {

isForward = false;

isBackward = false;

p1buttonState = digitalRead(switchOne);

p2buttonState = digitalRead(switchTwo);

if (p1ButtonPress()) {

digitalWrite(dirPin,HIGH);



delay(5);

}

if (p2ButtonPress()) {



  digitalWrite(dirPin,LOW);



  delay(5);

}



if( isForward || isBackward ){



  for(int x = 0; x < 800; x++) {

    digitalWrite(stepPin,HIGH);

    delayMicroseconds(500);

    digitalWrite(stepPin,LOW);

    delayMicroseconds(500);

  }

}

}

bool p1ButtonPress()

{

bool isPress = false;

// compare the p1buttonState to its previous state

if (p1buttonState != lastp1buttonState) {

// if the state has changed, increment the counter

if (p1buttonState == LOW) {

  // if the current state is HIGH then the button went from off to on:

  bPress = true;

  isPress = true;

  Serial.println("Player One score");



} else {

  // if the current state is LOW then the button went from on to off:

  Serial.println("off");

  isForward = true;

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state, for next time through the loop

lastp1buttonState = p1buttonState;

return isPress;

}

bool p2ButtonPress()

{

bool isPress = false;

// compare the p1buttonState to its previous state

if (p2buttonState != lastp2buttonState) {

// if the state has changed, increment the counter

if (p2buttonState == LOW) {

  // if the current state is HIGH then the button went from off to on:

  bPress = true;

  isPress = true;

  Serial.println("Player Two score");



} else {

  // if the current state is LOW then the button went from on to off:

  Serial.println("off");

  isBackward = true;

}

// Delay a little bit to avoid bouncing

delay(50);

}

// save the current state as the last state, for next time through the loop

lastp2buttonState = p2buttonState;

return isPress;

}

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

Post a schematic, not a frizzy thing it appears you have a hardware problem. Also post links to technical information on each of the hardware devices. What precautions did you take to control the system during reset? During reset the unused ports are set to input.

It is the program. At worst a stray pulse on the step pin would cause one step. For a revolution you need 800 steps. I thing the cause is that you initialize the lastp1buttonState to 0 (LOW/pressed). When you code sees that the button is now NOT pressed it turns on the 'isForward' flag.

I think you are keeping too many copies of the button state varables. You have:

int p1buttonState = 0; // current state of the button\
int lastp1buttonState = 0; // previous state of the button
bool bPress = false;
bool isPress = false;
  isForward = true;

The only state you need to keep is the previous state.

Like this:

// defines pins numbers

const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;
const int switchOne = 8;
const int switchTwo = 9;

bool ForwardButtonWasPressed = false;
bool BackwardButtonWasPressed = 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()
{
  isForward = false;
  isBackward = false;

  p1ButtonPress();
  p2ButtonPress();


  if ( isForward || isBackward )
  {
    for (int x = 0; x < 800; x++)
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
  }
}

void p1ButtonPress()
{
  bool isPressed = digitalRead(switchOne) == LOW;

  if (isPressed != ForwardButtonWasPressed)
  {
    // The state has changed
    ForwardButtonWasPressed = isPressed;

    if (isPressed)
    {
      Serial.println("Player One score");
      isForward = true;
      digitalWrite(dirPin, LOW);
    }
    else
    {
      Serial.println("off");
    }

    delay(20);
  }
}

void p2ButtonPress()
{
  bool isPressed = digitalRead(switchTwo) == LOW;

  if (isPressed != BackwardButtonWasPressed)
  {
    // The state has changed
    BackwardButtonWasPressed = isPressed;

    if (isPressed)
    {
      Serial.println("Player Two score");
      isBackward = true;
      digitalWrite(dirPin, HIGH);
    }
    else
    {
      Serial.println("off");
    }

    delay(20);
  }
}

That was it. Thank you for the help.

If the stepper driver chip you are using doesn't have an internal pull-down resistor
on the step pin, you'll need to add a physical pull-down resistor to it.

This is true of pretty much and "clocked" control pin to other hardware - while the
microcontroller is resetting all its pins may float around at random.

If your stepper controller has an opto-coupled interface this precaution is not needed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.