Go Down

Topic: stepper motor and switch (Read 272 times) previous topic - next topic

blo1958

Hi
just getting in to Arduino, I have a code to run a stepper motor forward and reverse so many counts in each direction, this starts when code is downloaded. I would like to have program initiated by pressing a momentary switch but can't figure out/find code to make that happen-help please

Robin2

I would like to have program initiated by pressing a momentary switch but can't figure out/find code to make that happen-help please
If you just want the program to do nothing until you press a button you can put code like this in setup()

Code: [Select]
pinMode(startButtonPin, INPUT_PULLUP);
while (digitalRead(startButtonPin) == HIGH) {}


The WHILE will repeat indefinitely until the button is pressed.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

blo1958

Hi
I have copied some code to run a nema 17 with a TB6600, this works well, start the code and motor goes back and forward predetermined number of steps. i would like the motor to do this when a momentary switch is pressed once and the program to work once can someone tell me the code to have this happen please?

JCA34F

Post the code you have. You want the program to run only once and never again until reset or powered down and back up, is that correct? Which pin will the switch be connected?

blo1958

Hi, below is the code running the motor, what I'd like is to have the momentary switch commit the program
every time it is pressed-appreciate your help!!
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#while (! digitalRead(2) ) { }
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
  // Set the maximum speed and acceleration:
  stepper.setMaxSpeed(1500);
  stepper.setAcceleration(1000);
}
void loop() {
  // Set the target position:
  stepper.moveTo(4000);
  // Run to target position with set speed and acceleration/deceleration:
  stepper.runToPosition();
  delay(1000);
  // Move back to zero:
  stepper.moveTo(0);
  stepper.runToPosition();
  delay(1000);
}

Robin2

#5
Feb 21, 2020, 09:46 am Last Edit: Feb 21, 2020, 09:47 am by Robin2
You need code that sets a variable when the button is pressed and which "unsets" the variable when the move is finished. Something like this pseudo code
Code: [Select]
previousButtonState = buttonState;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW and previousButtonState == HIGH) { // assumes LOW when pressed
   runMotor = true;
}

if (runMotor == true) {
   // code to make motor move
   runMotor = false;
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ballscrewbob

@blo1958

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

Go Up