Hi everyone,
I have a small homework but I dont know how to start so I really need help.
I want to control a stepper motor nema23, . And I want after pressing a button, the motor accelerates to the position then decelerates and stop. But the thing is that When I release the button, the motor will stop.
So I just want press the button one time, and the motor moves to the postion and maybe press an other button to go back to previous positon, but with acceleration and deceleration. Thank you guys
Your post was MOVED to its current location as it is more suitable.
Could you also 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 in the future.
It sounds like you need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE
Once you have detected the change of state you can call a function to start the motor
I haven’t used it, but there’s a canned library ‘AccelStepper’ which will probably sort you out.
Don’t forget you’ll need a separate driver and beefy power supply to feed the motor.
I do have a driver and this is my code and seem like i can not run full speed with this code and also it cant decelarate, when I release the button, it stops immediately :#include <AccelStepper.h>
// Defining pins
int pulse = 9;
int dir = 8;
int ENA = 7;
int button = 2;
int buttonstate;
int oldpos = 0;
long newpos = 1000000;
long maxspeed = 1000 ;
long accelaration = 1000;
AccelStepper stepper(1,8,9);
void setup() {
pinMode(pulse,OUTPUT);
pinMode(dir,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(button,INPUT_PULLUP);
digitalWrite(ENA,LOW);
digitalWrite(dir,LOW);
// put your setup code here, to run once:
}
void loop() {
buttonstate = digitalRead(2);
if(buttonstate == LOW){
//stepper.setSpeed(1000);
stepper.setMaxSpeed (maxspeed);
stepper.setAcceleration(accelaration);
stepper.move(newpos);
stepper.runSpeed();
}
if (buttonstate == HIGH){
stepper.setCurrentPosition(0);
}
The easier you make it to read and copy your code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags
Here is example code using the state change detection method that you might be able to adapt for your use.
/*
The circuit:
- pushbutton attached to pin 10 from ground
- the internal pullup on pin 10 is enabled
- LED attached from pin 13 to ground (or use the built-in LED on most
Arduino boards)
*/
// this constant won't change:
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;
const byte buttonPin = 10; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW); // enable steppers
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 20;
if (millis() - timer >= interval)
{
timer = millis();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
// put code to move the stepper here
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
}
}
Thank you sir,
You could use the MobaTools library. It contains a class to move your stepper with acceleration and deceleration, and it contains a class to manage your buttons - e.g. recognize the 'pressed' or 'released' moment.
Examples are provided in the lib.
The lib can be installed by means of the library manager ( type 'mobatools' in the search box. )
Edit:
A small example to go forth and back with 2 buttons:
#include <MobaTools.h>
// Adjust pins, steps and position as needed
const byte stepPin = 9;
const byte dirPin = 8;
const byte enaPin = 7;
const int stepsPerRev = 800; // Steps per revolution ( exammple with 1/4 microsteps )
int buttonPosition = 100000; // Steps to be executed when button is pressed
const byte buttonPins[] = { A0, A1 }; // connect the buttons between pin and gnd
MoToButtons myButtons( buttonPins, 2, 20, 500 );
MoToStepper myStepper ( stepsPerRev, STEPDIR );
void setup() {
myStepper.attach( stepPin, dirPin );
myStepper.setSpeedSteps( 10000 ); // 1000 Steps/sec
myStepper.attachEnable( enaPin, 50, LOW);
myStepper.setRampLen( 200 ); // length af acceleration/deceleration ramp
}
void loop() {
myButtons.processButtons();
if ( myButtons.pressed(0) ) {
// goto buttonPosition if 1st button pressed
myStepper.writeSteps( buttonPosition );
}
if ( myButtons.pressed(1) ) {
// back to origin when 2nd button pressed
myStepper.writeSteps( 0 );
}
}
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.