Controlling stepper motor direction with buttons

Good afternoon! I am trying to make, for my first project, a remote camera lift for a friend of ours to se for cancer screening. Electronically, I am using a nema 17, arduino uno, and a TB6600. Thanks to a couple of youtube videos, I currently have it figured out how to make the stepper motor run a few steps and stop each time a button is pushed (I have one for CW and one for CCW). However, I need it to run while I have a button pushed and stop when I release it. I am hoping someone can show me what to change in the coding (and explain it to a 5yr old ;). Any help would be greatly appreciated!

// 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(1);

  }



    if (p2ButtonPress()) {



      digitalWrite(dirPin,LOW);



      delay(1);

    }



    if( isForward || isBackward ){



      for(int x = 0; x < 200; 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("Plaer 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("Plaer 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;

}

Wll done using the code tag function! Please erase the empty lines! I'll not buy a double sized monitor to read such code to get an overview. Joking!

What's Your experience in programming and electronics?

That tells nothing useful but likely will the TB6600 handle it.

Please post schematics telling how You intend to wire it all.

No, thank You.

Try "while" rather than "if"

xfpd, thank you for your response! I tried changing it, upon your suggestion, but I did not see a difference in the way it responded. If you have any other thoughts, I would love to hear them!.

Thank you for taking the time to respond! My apologies with the extra spacing, I simply copied and pasted it. I thought about editing it, but I wasn't sure if that was a good idea. To answer your questions:

  1. I have a decent amount of experience with electronics, but none with the arduino or programming of this sort (the only programming I have done is regarding Marlin for my 3d printer).
  2. I hope this makes sense, but here are the schematics: My wiring from the TB6600 to the Arduino Uno: 3=DIR+, 4=PUL+, 5=ENA+, Negatives for each of these=ground on the power side. I have the stepper wired to the A&B terminals. I then have momentary switches to control the up and down hooked into the arduino with the positives at 8 and 9 with their negatives to the ground on the digital pin side. I am powering it with 9vDC from a power supply and I have the dip switches on the TB6600 set to on, off, off, on, off, off

Thank you again for any insight you can provide!!

Can you describe the movement as the button

  • is not pressed,
  • is pressed and released,
  • is pressed and held, and
  • is finally released

I think stepping needs a high/low transition. A "press and release" would be a HIGH then a LOW. A "press and hold" would need to keep transitioning high/low/high/low... maybe...

while (p1ButtonPress()) {
      digitalWrite(dirPin,HIGH);
      delay(1);
      digitalWrite(dirPin,LOW);
      delay(1);
}

Thank you again for your time! When neither of the buttons are pressed, nothing happens. When I press a button (I am using momentary buttons), nothing happens until I release it and then it rotates a degree to two. One button makes it go clockwise, and the other makes it go counter clockwise.
So, not pressed=nothing.

· is pressed and released=small movement

· is pressed and held=nothing

· is finally released=small movement.

Additionally, I put in a regular on/off switch, and the same thing happens. When I power it up with the switch on, the motor goes a couple degrees and stops. If I turn it off and then on again, it will rotate a couple of degrees and stops.

Show your sketch with the updates from post #8.

You have made the code way too complicated for this simple task.
See if this revision does what you want.

// defines pins numbers

const int dirPin  = 3;
const int stepPin = 4;
const int enPin   = 5;

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

int switchOneState = 0;         
int switchTwoState = 0;
         
void setup()
{
  Serial.begin(9600);

  pinMode(switchOne, INPUT_PULLUP);
  pinMode(switchTwo, INPUT_PULLUP);
  switchOneState = HIGH;//default state for input pullup
  switchTwoState = HIGH;  

  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  digitalWrite(enPin, LOW);
}

void loop()
{
  //debounce switches by reading every 50 ms
  static unsigned long lastReading = 0;
  if (millis() - lastReading >= 50)
  {
    lastReading = millis();
    switchOneState = digitalRead(switchOne);
    switchTwoState = digitalRead(switchTwo);
  }

  if (switchOneState == LOW) //button is pressed
  {
    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }

  if (switchTwoState == LOW) 
  {
    digitalWrite(dirPin, LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(500);
  }
}

Cattledog, that did it!! Thank you so much!!!!!

Thank you for your help and patience, xfpd!! The code from Cattledog works. Thank you again.

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