Using Sensor Input to Stop Motor (w/ Accelstepper library)

Hello,
I hope this is the right place to post this, I wasn’t sure if here or the motors board would be better but I think this is a programming issue.
I’m trying to create a simple program that runs a stepper motor (using a TB6600 microstep driver) continuously until interrupted by a sensor signal. Essentially, I want the motor to start running whenever I start the program, and when the program receives a low signal from the sensor, I want the motor to stop. Once the program recieves a high signal again from the sensor, I want it to start running again. I’ve tried the code below and it’s not doing anything, the motor just makes a clicking sound. Any idea what’s going on? I’ve tried so many different variations and looked at so many Accelstepper examples to try to get this to work and I can’t figure it out. I feel like it should be really simple but it’s just not clicking in my brain.
Thanks for any help!

#include <AccelStepper.h>
#define MotorInterfaceType 1
#define dirPin 6
#define stepPin 3
AccelStepper stepper = AccelStepper(MotorInterfaceType, stepPin, dirPin);

int IRsensor = 0;
int IRsensorcall;

void setup() {

   pinMode(IRsensor, INPUT);
   stepper.setMaxSpeed(2000);
  
Serial.begin(9600);
}
 
void loop() {

stepper.setSpeed(1000);
stepper.move(1000);  //run motor at constant speed  
IRsensorcall = digitalRead(IRsensor); //read sensor
Serial.println(IRsensorcall);


//if sensor reads 0, stop the motor. Else keep motor running.
if (IRsensorcall == LOW){
  stepper.move(0); 
  delay(500);

   }
stepper.run();
   }
  //if sensor reads 0, stop the motor. Else keep motor running.
  if (IRsensorcall == LOW) {
    stepper.move(0);
    delay(500);

  }
  stepper.run();
}

Interesting. You have an 'else' in the comment but none in the code.

Read about else at the IDE reference page

My assumption was that I didn't need to have an 'else' actually in the code if I just wanted the code to keep doing the same thing it was originally doing. It would appear I don't have much of a grasp on the fundamentals of programming though :confused: .
So do I need to repeat the previous command like below?

 if (IRsensorcall == LOW) {[color=#222222][/color]
    stepper.move(0);[color=#222222][/color]
    delay(500);
}
else {
stepper.move(1000);
}

sbbs20:
So do I need to repeat the previous command like below?

Looks promising. Try it.

Which Arduino? If UNO, Nano, Mini, you can't use pin 0 for input and Serial at the same time. If all you are doing is starting and stopping the motor at a fixed speed, you don't need a library.
If you are trying to run the motor at 2000 steps per second (600 RPM with a 200 step motor), you MUST use acceleration, there is none in your code.

I'm using an UNO, thank you for the advice on the 0 pin, I had no idea! I don't know why (other than my own ineptitude), but every time I attempted to run a program without the library I couldn't get it to do anything.

I attempted the code with the added 'else' and it works but it runs very slowly with a loud grinding sound, any idea what's going on here?
I also tried an alternate method where I have it just run at a constant speed and then disables the motor instead of telling the motor to move 0, but that gave me the same problem I previously had where it just makes a clicking noise (also I'm not sure if it's harmful to the motor to be constantly turning it off and on like that).
I appreciate the help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.