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
blo1958:
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()
pinMode(startButtonPin, INPUT_PULLUP);
while (digitalRead(startButtonPin) == HIGH) {}
The WHILE will repeat indefinitely until the button is pressed.
...R
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?
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?
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);
}
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
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
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.