Notifications
Clear all

Stepper motor with selectable speed and rotation option  

  RSS

Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-05 9:43 am  
Please Note - I have included replies from other people who gave me some advice, please also have a look at what they provided me, and please comment if it will not work and why, but below is my original question, with replies. 

I would like to be able to have 3 Momentary switches that when pressed will let me be able to select from 3 speeds or 3 settings. Here is my basic sketch without any switches.

Any advice will be appreciated?
TIA

/* Example sketch to control a stepper motor with TB6600 stepper motor driver, AccelStepper library and Arduino: acceleration and deceleration. More info: https://www.makerguides.com */

// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#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(1000);
stepper.setAcceleration(500);
}
void loop() {
// Set the target position:
stepper.moveTo(8000);
// 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);
}

---------------------------------------------------------------------------------------------------------------
Replies:
 #include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup () {

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop () {

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3= digitalRead(buttonPin3);

if (buttonState1 == 1) {
stepper.setMaxSpeed(300);
stepper.setAcceleration(500);
}
if (buttonState2 == 1) {
stepper.setMaxSpeed(600);
stepper.setAcceleration(500);
}
if (buttonState3 == 1) {
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
}
}

//in void setup () not finish
-------------------------------

How do I upload images to share with you all? I have a Nema 34 with microstep driver and powersupply. Things I would like to share if possible?

 

Make the best of today.


Quote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 493
2020-08-05 1:41 pm  

@iclazion

How do I upload images to share with you all?

Click the Attach Files option on the bottom bar where you type your posts.

To make your programs easier to read,
https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939


ReplyQuote
Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-05 3:48 pm  
Posted by: @robotbuilder

To make your programs easier to read,
https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

Thank you robotbuilder

This is the sketch I uploaded right now:

 

/* Example sketch to control a stepper motor with TB6600 stepper motor driver, AccelStepper library and Arduino: acceleration and deceleration. More info:  https://www.makerguides.com  */

// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#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(1000);
stepper.setAcceleration(500);
}
void loop() {
// Set the target position:
stepper.moveTo(8000);
// 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);
}
This post was modified 2 months ago by Iclazion

Make the best of today.


ReplyQuote
Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-05 4:04 pm  
Posted by: @robotbuilder

To make your programs easier to read,
https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

Thank you robotbuilder

This is the sketch I uploaded right now:

 

/* Example sketch to control a stepper motor with TB6600 stepper motor driver, AccelStepper library and Arduino: acceleration and deceleration. More info:  https://www.makerguides.com  */

// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#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(1000);
stepper.setAcceleration(500);
}
void loop() {
// Set the target position:
stepper.moveTo(8000);
// 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);
}

Make the best of today.


ReplyQuote
Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-05 4:05 pm  

Suggested Sketch, does it look like it will work?

 

#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup () {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop () {
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3= digitalRead(buttonPin3);
if (buttonState1 == 1) {
stepper.setMaxSpeed(300);
stepper.setAcceleration(500);
}
if (buttonState2 == 1) {
stepper.setMaxSpeed(600);
stepper.setAcceleration(500);
}
if (buttonState3 == 1) {
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
}
}
//in void setup () not finish

 

Make the best of today.


ReplyQuote
Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-05 4:08 pm  
Posted by: @iclazion

stepper.setMaxSpeed(300);

Reason I ask is "setMaxSpeed" is not a defined variable. It's assuming there is a MaxSpeed. 

Make the best of today.


ReplyQuote
triform
(@triform)
Reputable Member
Joined: 1 year ago
Posts: 330
2020-08-05 5:03 pm  

@iclazion

I think that some of the methods in that library are 'float' types.

https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html

You may want to adjust based on that.


ReplyQuote
Iclazion
(@iclazion)
Active Member
Joined: 2 months ago
Posts: 8
2020-08-06 7:37 am  

This video by "DroneBot Workshop"

 

He has the ability to speed it up and reverse it. In the simplest form I would like to be able to select 3 speeds X-revolutions per second. Is that an easy thing I can do with a sketch. I am asking because me and a friend from Malaysia is trying to figure it out without any success thus far.

If there is a person I can ask please let me know, or a course I can join that will help me?

Below is the sketch used, that was found on ( https://dronebotworkshop.com/big-stepper-motors/)

 

 

/*
  Stepper Motor Test
  stepper-test01.ino
  Uses MA860H or similar Stepper Driver Unit
  Has speed control & reverse switch
  
  DroneBot Workshop 2019
   https://dronebotworkshop.com 
*/

// Defin pins

int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer

// Variables

int pd = 500;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

  setdir = !setdir;
  
}


void setup() {

  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}

void loop() {
  
    pd = map((analogRead(spd)),0,1023,2000,50);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

This post was modified 1 month ago by Iclazion

Make the best of today.


ReplyQuote
JoeLyddon
(@joelyddon)
Reputable Member
Joined: 1 year ago
Posts: 320
2020-08-06 7:27 pm  
Posted by: @iclazion

This video by "DroneBot Workshop"

 

He has the ability to speed it up and reverse it. In the simplest form I would like to be able to select 3 speeds X-revolutions per second. Is that an easy thing I can do with a sketch. I am asking because me and a friend from Malaysia is trying to figure it out without any success thus far.

If there is a person I can ask please let me know, or a course I can join that will help me?

Below is the sketch used, that was found on ( https://dronebotworkshop.com/big-stepper-motors/)

 

 

/*
  Stepper Motor Test
  stepper-test01.ino
  Uses MA860H or similar Stepper Driver Unit
  Has speed control & reverse switch
  
  DroneBot Workshop 2019
   https://dronebotworkshop.com 
*/

// Defin pins

int reverseSwitch = 2;  // Push button for reverse
int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer

// Variables

int pd = 500;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

  setdir = !setdir;
  
}


void setup() {

  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);
  
}

void loop() {
  
    pd = map((analogRead(spd)),0,1023,2000,50);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
 
}

My Guess...  pd looks like is the KEY to the SPEED.

Setup the Serial display so you can get the pd values as it is running...  Display pd to screen after each map(analogRead(spd)...   Once you have the SPEED you like, make a note of the pd displayed value...

Do this for the three speeds you are after...  Then, add coding to control the stepper using the Prescribed PD values for desired speeds...   Adding the logic to control which one to use...   The key is the PD Value, as I see it...  Once you have it nailed with the selected speeds, remove the Serial displays...   Hope this gets you on the right path.  Let us know how it works out for you, OK?

Have Fun,
Joe Lyddon

www.woodworkstuff.net


ReplyQuote