I am looking for a way to make a variable number equal to my max speed of my stepper motor. I have 2 buttons which I want to control the speed of the motor i.e 1 buttons increases to my max speed by 100 on each press and button 2 reduces the speed by 100 each time.
I am fairly new to Arduino coding. I was trying to find a way to write:- stepper1.setMaxSpeed = number; but it wont let me but I have attached a part of the code to hopefully show what I am trying to archive.
Thanks in advance.
#include <AccelStepper.h>
#define pul1 2
#define dir1 3
int button1 = 7;
int button2 = 8;
int number = 100;
AccelStepper stepper1(1, pul1, dir1);
void setup() {
Serial.begin(9600);
stepper1.setMaxSpeed(1000);
stepper1.setSpeed(1000);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
}
void loop() {
stepper1.setMaxSpeed = number;
if(digitalRead(button1) == LOW){
number = number + 100;
Serial.println(number);
delay(400);
}
if(digitalRead(button1) == LOW){
number = number - 100;
Serial.println(number);
delay(400);
}
}
Sets the maximum permitted speed. The run() function will accelerate up to the speed set by this function. Caution: the maximum speed achievable depends on your processor and clock speed. The default maxSpeed is 1.0 steps per second.
Parameters
[in] speed The desired maximum speed in steps per second. Must be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may Result in non-linear accelerations and decelerations.
Hi yes I realise that the stepper1.setMaxSpeed command sets the max speed and I probably should use the maxspeed or runspeed etc and the but if I change the setMaxSpeed it does the same thing by altering the speed etc. and I have only put the stepper1.setMaxSpeed(1000); there as a starting speed.
But the main problem is I cant find a way to get the stepper1.setMaxSpeed = number; in other words read the value of what ever "number" will be and transfer it to the speed of the stepper.
I know stepper1.setMaxSpeed = number; is wrong and it wont let me verify it, I was wondering if there was a way to make it happen.
I have been using the MobaTools stepper library and find it easier to learn and use than AccelStepper. Here is a demo that uses 2 push button switches to change the stepper speed. One increase or decrease step per button press. It uses the the state change detection method to monitor for button presses. The code is tested with real hard ware. The MobaTools library is available via the IDE library manager and the library documentation is here.
I changed the pins to suit my setup for testing. You will need to change them to suit your setup.
#include <MobaTools.h>
const byte stepPin = 2;
const byte dirPin = 5;
const byte button1 = 9;
const byte button2 = 10;
int number = 1000; // = 100 steps per second see MobaTools docs
const unsigned int motorStepsPerRev = 200; // native motor ppr
const unsigned int microstepMultiplier = 4; // driver setting
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;
MoToStepper stepper1( STEPS_REVOLUTION, STEPDIR );
void setup()
{
Serial.begin(9600);
stepper1.attach( stepPin, dirPin );
stepper1.setSpeedSteps(number); // 1000 steps per second
stepper1.setRampLen(50);
stepper1.setZero();
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
Serial.println("set stepper speed with buttons");
}
void loop()
{
checkButton1();
checkButton2();
stepper1.rotate(1);
}
void checkButton1()
{
static bool lastButtonState = HIGH;
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
bool buttonState = digitalRead(button1);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
number = number + 100; // add 100 steps for each press
if (number > 2500)
{
number = 2500; // limit of MobaTools speed
}
Serial.println(number);
stepper1.setMaxSpeed(number);
}
lastButtonState = buttonState;
}
}
}
void checkButton2()
{
static bool lastButtonState = HIGH;
static unsigned long timer = 0;
unsigned long interval = 50;
if (millis() - timer >= interval)
{
timer = millis();
bool buttonState = digitalRead(button2);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
number = number - 100; // subtract 100 steps for each press
if (number < 0)
{
number = 0; // no negative speed
}
Serial.println(number);
stepper1.setMaxSpeed(number);
}
lastButtonState = buttonState;
}
}
}
Thanks for that I did try the ModaTools stepper library but i dont think it has the decel and excell that the accelstepper does. I have now thanks to you managed to get the accelstepper code working I have put together and added your bit "stepper1.setMaxSpeed(number);" which was what I was looking for to enable me to add and subtract 100 to the speed with each press of the 2 button.
The only thing that would be the icing on the cake would to have an led flashing the amount of times in 100's to let me know what speed the motor is at, ie 100 = 1 flash 200 =2 flashes etc. This is my code:-
#include <AccelStepper.h>
#define enPin 10 //10Pin of Arduino--Enabled of stepper motor driver
#define pul1 3
#define dir1 4
#define bt_Sp A1 // ms1
AccelStepper stepper1(1, pul1, dir1);
#define ms1Pin 11 //10Pin of Arduino—ms1 high or low select
const int buttonPinst = 2; // the number of the pushbutton pin
const int ledPinst = 12;
int buttonstState = 0;
const int buttonPinsp = 5; // the number of the pushbutton pin
const int ledPinsp = 13;
const int buttonPinspd = 6; // the number of the pushbutton pin
const int ledPinspd = 9;
int number = 100;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
stepper1.setMaxSpeed(100);
stepper1.setAcceleration(2000);
stepper1.moveTo(4000);
pinMode(enPin, OUTPUT); // declare as output for Enabled of stepper motor driver
pinMode(bt_Sp, INPUT_PULLUP); // declare bt_S as input
// initialize the LED pin as an output:
pinMode(ledPinst, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinst, INPUT_PULLUP);
pinMode(ledPinsp, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinsp, INPUT_PULLUP);
pinMode(ledPinspd, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinspd, INPUT_PULLUP);
}
void loop() {
buttonstState = digitalRead(buttonPinst);
stepper1.setMaxSpeed(number);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonstState == LOW) {
// toggle the LED state:
digitalWrite(ledPinst, !digitalRead(ledPinst));
// debounce:
delay(400);}
if(digitalRead(buttonPinspd) == LOW) {
number = number + 100;
stepper1.setSpeed(number);
Serial.println(number);
delay(500);
Serial.println(number);
}
if(digitalRead(buttonPinsp) == LOW) {
number = number - 100;
stepper1.setSpeed(number);
Serial.println(number);
delay(500);
Serial.println(number);
}
// put your main code here, to run repeatedly:
digitalWrite(ms1Pin, HIGH);
if(digitalRead(ledPinst) == HIGH)
stepper1.run();
if (stepper1.distanceToGo()==0){
stepper1.moveTo(-stepper1.currentPosition ());
}
}
That is wrong. The MobaTools stepper library does support acceleration. The setRampLen() function sets the amount of acceleration. The argument is the number of steps over which the acceleration takes place. Higher speeds need higher number of steps to get to speed.