Arduino Uno, CNC Shield
I have a stepper motor that runs until it hits a switch. (basic limit switch NO) The switch turns off the motor by .setSpeed.
How do get out of the if logic to allow the motor to start back when the switch is not pushed?
Code works to stop the motor but does not turn back on if switch is released.
The motor gets really hot when paused. Is there a better solution to disable the motor? Stepps do not matter in this setup. The distance is always changing between the limit switch.
#include <Stepper.h>
const int limit = 9;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2,5);
void setup() {
Serial.begin(9600);
pinMode(9, INPUT);
digitalWrite(limit,LOW);
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
if(digitalRead(9) == HIGH)
{
myStepper.setSpeed(0);
delay(5);
} // stepper.setSpeed(0);
else{
// step one revolution in one direction:
Serial.println(limit);
myStepper.step(stepsPerRevolution);
delay(5);
}
}
It is normal for stepper motors to be hot. What is the current permitted in the motor coils and have you adjusted the current limit on your stepper driver to match it? If you need more advice post a link to the datasheet for your stepper motor and tell us what stepper driver you are using.
How do get out of the if logic to allow the motor to start back when the switch is not pushed?
I wonder if that properly describes the problem? I suspect what is wanted is to get the motor to move in the other direction, away from the switch, even though the switch is pressed.
Also, I suspect that the standard Stepper library is not suitable for what you want because stepper.step() is a blocking move that runs until the number of steps has been completed. The much more capable AccelStepper library can do non-blocking moves with its run() and runSpeed() functions. That means that you can interrupt a movement - for example when a switch is triggered.
If you want a responsive program with the standard Stepper library you need to get it to move one step at a time and use your own code to create the step timing.
The best way to deal with your limit switch is to use it to set a variable (for example limitTriggered = true;) and use the value of the variable to decide whether, or in what direction the motor should move. Something like this pseudo code
if ( limitTriggered == true) {
// stop motor
// change direction
// allow motor to move in new direction
}
This application is using the stepper to feed paper to another machine. When material is removed (releasing limit switch) I want the stepper to turn on the and push the same direction. Basically act as an on off switch
I do not want a change in direction. I want the motor to stop.
This motor is pushing material to another machine. The limit switch is suppose to be an on/off button.
My goal was to use a limit switch (I burned up the optical sensor on the machine) until replacement arrives to feed material. When material is removed the "switch" turns on the motor to move again.
I would like the stepper to run one step at a time and check if the limit is reached. If limit is reached, stop. When limit switch is released the motor to start again.
I looked for the commands you suggested and need to define them in AccelStepper...
foamnone:
My goal was to use a limit switch (I burned up the optical sensor on the machine) until replacement arrives to feed material. When material is removed the "switch" turns on the motor to move again.
It was not clear from your earlier posts that material would be removed to allow the switch to open.
Your code needs to detect when the switch changes from open to closed (and stop the motor) and when it changes from closed to open (to restart the motor). Something like this
previousSwitchState = switchState; // save the current value
switchState = digitalRead(switchPin); // get the new value
if (switchState == LOW and previousSwitchState == HIGH) { // assumes low when switch is pressed
// code to stop motor
}
if (switchState == HIGH and previousSwitchState == LOW) {
// code to start motor
}
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
const int buttonPin = 9;
int buttonState = 1;
int previousbuttonState = 1;
void setup(){
pinMode(buttonPin, INPUT);
stepper.setMaxSpeed(5000);
stepper.setAcceleration(800);
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(buttonPin);
Serial.print(buttonState);
Serial.println(previousbuttonState);
previousbuttonState = buttonState; // save the current value
if (buttonState == HIGH and previousbuttonState == HIGH){
stepper.move(300);
stepper.run();
delay(50);
}
if (buttonState == LOW and previousbuttonState == HIGH) {
stepper.stop();
delay(50);
}
}The motor turns slow but Im scared to change anything because it works.
foamnone:
The motor turns slow but Im scared to change anything because it works.
If you open the code and save it as a different name , then you can work on the new code without changing the original.
Use obvious names like _version 1 etc.