Stepper motor on/off - CNC Sheild

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
}

…R
//

if(endSwitch && ( direction == forward))
 stopMotor();
else
runMotor();

The stepper motor:is https://media.digikey.com/pdf/Data%20Sheets/NMB-MAT/PM42L-048-HHC9.pdf

Using a A4988 stepper driver. 1 AMP 24V.

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 like you suggestion on one stepping.

if(endSwitch && ( direction == forward))
{
   direction = backward;
   stopMotor();
}
 
else
   runMotor();

foamnone:
Using a A4988 stepper driver. 1 AMP 24V.

Your motor datasheet says the allowed current is 600mA. If you are feeding with with 1000mA it is not surprising if it gets hot.

...R

#include <AccelStepper.h>
int limitswitch = 9;
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{ 
  limitswitch = digitalRead(9);
  pinMode(limitswitch, INPUT_PULLUP);
  Serial.begin(9600);
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(500); 
  Serial.println(limitswitch);
}

void loop() {  
   if(digitalRead(limitswitch) == HIGH){
    
   
   stepper.runSpeed();
    

   delay(5);}

   
   else if (digitalRead(limitswitch) == LOW){
   stepper.setMaxSpeed(0);

   delay(5);}
   
}

Where is the change of direction when reaching the end stop?

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:
I do not want a change in direction. I want the motor to stop.

Sorry. Right answer for another topic....

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
}

...R

deleted

#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.

What is the purpose of Reply #12 ?

...R

The comment was placed at the end of the code.

The motor turns slow but Im scared to change anything because it works.

Im trying to understand why the motor only turns like 1 step at a time. This sketch causes my computer to freeze up when looking at Serial.

I must have some kind of loop that the system does not like

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.

Much better to describe the problem in the text so we can see them before we look at the code.

This delay(50) is almost certainly the problem - remove it

   stepper.move(300);
   stepper.run();
   delay(50);

Also remove this because it will be trying to print much too often. It will choke the Serial Output buffer and cause the Arduino to hang.

  Serial.println(previousbuttonState);

And having HIGH twice in this line makes no sense. One of them should be LOW

  if (buttonState == HIGH and previousbuttonState == HIGH){

...R