Need some suggestions on controlling a stepper motor with buttons.

So I am trying to control a ballscrew linear actuator with an arduino uno + motor shield.

I have several functions i need this actuator to do, i found code on these fourms but i still have one function that i need to accomplish.

my project is that i am adding an extra z axis to my cnc router for a laser.

here is the problem i am trying to solve. once it his homed by setting off a limit switch i want to be able to use buttons to make it go down to what ever distance i need. once i am at that distance i would like to be able to push a button to set that distance as zero, so then i can easily return back to that distance by pushing another button.

thanks,
Mike

here is the code i found that i plan to edit to suit my needs.

#include <AccelStepper.h>

AccelStepper stepper1(1, 6, 5);

const int homeButton = 2;
const int ledPin = 13;
byte hBval;


void setup(){
stepper1.setMaxSpeed(500); //nice and slow for testing
stepper1.moveTo(-3200);
stepper1.setAcceleration(500);
pinMode(homeButton, INPUT);
pinMode(ledPin, OUTPUT);
stepperHome(); //runs routine to home motor
}
void loop(){
stepper1.moveTo(5000); // random position to end for testing
stepper1.runToPosition();
delay(1000);
stepper1.moveTo(0);
stepper1.runToPosition();
delay(1000);
}
//contributed by Runaway Pancake 9/2/13
void stepperHome(){ //this routine should run the motor
hBval = digitalRead(homeButton);
while (hBval == HIGH)
{
 //backwards slowly till it hits the switch and stops
 stepper1.moveTo(-3200);
 stepper1.run();
 digitalWrite(ledPin, LOW); //indicates it's doing something
 hBval = digitalRead(homeButton);
}
digitalWrite(ledPin, HIGH); //indicates it's doing something
stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

You need to get all the delay()s out of your program if you want it to be responsive. Have a look at how millis() is used to manage timing in several things at a time

Also I reckon you should use the stepper.run() or the stepper.runSpeed() functions to make your motor move without blocking the rest of the Arduino code.

Take the project in small bites. Write a short program that makes the motor move when you press a buttin and which displays the position on the Serial Monitor. The you can add another button to save that position in a variable.

…R
Stepper Motor Basics
Simple Stepper Code

thanks, fixed it.

I have not not tested or opened any code yet, mostly trying to do research before my parts arrive. I am just trying to make sure my concept works first. then i will take your advice and just go step by step.

I just looked into the serial monitor, that’s awesome and good to know about.

so it is possible to code it where i can push a button and that button will read how many steps my motor has taken from the home reference?

I think i know one way it could work, just don’t fully know the limits of arduino yet.

so say my actuator can physically go 1000 steps, so i set my default limit from home to 1000 steps. so when i turn the arduino on the actuator goes up until it hits the limit switch letting it know it can go 1000 steps from there.

now if i need to set a custom height i would like to do this by using buttons to make it go up and down and then press a different button to remember the height. when that button is pressed i need it to change the default limit to the new limit that i set.

i have a lot to learn about the code, just trying to plan so i can focus my learning in the right direction.

thanks,
mike

mikedcaffey:
so it is possible to code it where i can push a button and that button will read how many steps my motor has taken from the home reference?

The button does not read the steps. It just indicates to the Arduino that steps are required. Your Arduino program counts the steps as it issues them.

From the sound of things (is there sound with reading ? :slight_smile: ) you need to get some simple programming experience under your belt so that you have a better grasp of how an Arduino is used. There are lots of examples with the Arduino IDE.

You may find something useful in Planning and Implementing a Program although it was not written for a complete beginner.

...R

Hey Robin,

just wanted to say thanks for your help. I have recruited the help of a friend who has a little more experience! Although i would love to learn to code it myself, my high school c++ skills are a little forgotten.

I will update this post when we figure it out, thanks.

mikedcaffey:
Although i would love to learn to code it myself, my high school c++ skills are a little forgotten.

Learn programming!

It is very satisfying. It can be like having a Lego set with an unlimited supply of every brick.

...R