Homing switch for stepper motor. Is this code OK ?

I am using a small stepper in a project that should “Home” before the loop starts. I read a bunch of posts that did not really seem to go about it in a simple manner. I thought I would try a “while” loop and see if that would work.

It does.

Now please understand that I am totally green on programming so I would like some folks in the know to tell me if this is a bad idea or if there is a more elegant way to go about this.

With this code the motor runs in reverse until it hits the switch, and then it passes off to the main program loop.

//Test for Stepper motor homing micro switch


#include <AFMotor.h>

AF_Stepper motor2(200, 1);     // Connect a stepper motor with 200 steps per revolution to port #1
// variables will change:
int switchState = 0;           // variable for reading the switch status
// set pin numbers:
const int switchPin = 10;     // the number of the switch pin


void setup(){
    motor2.setSpeed(100);                    // 100 rpm
    pinMode(switchPin, INPUT);               // initialize the switch pin as an input:
    digitalWrite(switchPin,HIGH);

    switchState = digitalRead(switchPin);    // read the state of the switch value:

                 // check if the switch is activated.
  
while(switchState == HIGH) {                 // if it is, the switchState is HIGH:
     
    motor2.step(1, BACKWARD, INTERLEAVE);      //reverse 1 step 
    switchState = digitalRead(switchPin);      // read the state of the switch value:
    motor2.step(1, BACKWARD, INTERLEAVE);      //reverse 1 step 
    switchState = digitalRead(switchPin);      // read the state of the switch value:  
 
  } 
 
}   


void loop(){
  
      //rest of program here
  
    delay(3000);                             //pause 3 seconds
    motor2.step(200, FORWARD, INTERLEAVE);  //test to see if setup passes to loop
}

I had the reverse and read switch lines in there only once and it did not work, I thought I remembered reading that they needed to be there at least twice so I added them a second time and it ran as expected.

Also
Is there any reason to “define” home as “zero steps” when the switch is triggered ? I think it would be implied at this point seeing as how the program will always run from the same triggered position.

Any insight or comments are very welcome to this beginner.

Many thanks

Scott

while(switchState == HIGH) {                 // if it is, the switchState is HIGH:
     
    motor2.step(1, BACKWARD, INTERLEAVE);      //reverse 1 step 
    switchState = digitalRead(switchPin);      // read the state of the switch value:
    motor2.step(1, BACKWARD, INTERLEAVE);      //reverse 1 step 
    switchState = digitalRead(switchPin);      // read the state of the switch value:  
 
  }

It doesn't make sense to read the switch state twice and step twice. One pair of lines should go.

Is there any reason to "define" home as "zero steps" when the switch is triggered ? I think it would be implied at this point seeing as how the program will always run from the same triggered position.

Steppers step. If the actual number of steps is important, it is up to you to keep track of the number of steps taken. So, assigning the value to 0 when the "home" switch becomes pressed seems reasonable.

Hi Paul

Thank you !
I just took out the duplicate pair and tried it again, and it did work this time. I wonder what I had screwed up the first time around?? But yes it works.

In this application the motor will turn a cam 1 full revolution per loop. The loop will run 40 cycles and stop.
I will then reload the gizmo and reset.
I am not sure it could get too far out of whack in that short a cycle. If it does loose steps My code wouldn't do anything about it anyway. It is just telling it to go 200 steps.
Unless I make it "poll" the switch in the loop. That sounds like a better idea.

Scott

I wonder what I had screwed up the first time around?? But yes it works.

I guess I didn't read far enough into the post. I have no idea what was wrong before, since I don't know what the code looked like before, or how the switch is wired.

I am not sure it could get too far out of whack in that short a cycle. If it does loose steps My code wouldn't do anything about it anyway. It is just telling it to go 200 steps.

Try this. Mark a start line. Mark an end line 200 steps away. Stand at the start line, and look in the direction of the stop line. Put a blindfold on, and take 200 steps. Take the blindfold off. Are you anywhere near the end line? 99 out of 100 people won't be. Without feedback, we can't tell where we are or measure progress.

The stepper won't be as bad, but feedback is still important. If 200 steps is one full revolution, then stepping until the home switch is released and pressed again WOULD be a good idea.

Thank you sir !
I agree

Scott

If you are using a stepper motor the mechancial system (i.e. motor power relative to load) and the motor control system should be designed so it never misses a step.

It is obviously essential to home the motor when the system is switched on, but after that it should not lose position unless some fault occurs. If you can't trust it to retain position then the design is flawed.

The Thread stepper motor basics may be useful.

...R

If you can’t trust it to retain position then the design is flawed.

I think “flawed” is the wrong term. For operator safety, and a number of other reasons, the system should refuse to take a step. If it does, there needs to be some mechanism to get back in sync. Calling such a system flawed is wrong, in my opinion.

PaulS:
the system should refuse to take a step

If the control system detects something and causes it to refuse a step it will still know where it is .

I don't believe it should be designed so that missing steps is an accepted occurrence.

The user may occasionally need to hit the big red switch - but IMHO that only happens for events outside the scope of the program

It may also be part of the normal control system for the user to press a "small red switch" to halt proceedings temporarily - but I see no reason why that should cause a loss of position.

...R

Every real CNC system I've ever seen uses encoders to know that the steppers moved where they are supposed to. A system that uses feedback to make sure that the motor, and whatever is moves, got to where it was supposed to can not be called flawed. That designation belongs to a system that assumes that a step can and will result in the exactly correct amount of movement.

PaulS:
Every real CNC system I've ever seen uses encoders

Arduino controlled systems ?

...R

Arduino controlled systems ?

Why is the size, memory, or speed of the controller important? The Arduino has everything it needs to actually monitor the position of the stepper, assuming some external component(s) are added (such as an encoder or limit switch).

I don't think that we fundamentally disagree that the stepper should, if powered, sized, and constructed correctly, be able to make each and every step. Where we seem to disagree is whether a system that doesn't make every step is flawed, or not. I content that it is not necessarily, depending on why the stepper failed to step. Slippage of a drive belt, for instance, should be expected, and the system designed to deal with the slippage.

PaulS:
I don't think that we fundamentally disagree that the stepper should, if powered, sized, and constructed correctly, be able to make each and every step. Where we seem to disagree is whether a system that doesn't make every step is flawed, or not. I content that it is not necessarily, depending on why the stepper failed to step. Slippage of a drive belt, for instance, should be expected, and the system designed to deal with the slippage.

I fully accept that I have oversimplified the situation.

But I think it is a useful oversimplification unless the OP is very experienced.

...R

PS. If the system must have drive belts that can slip then, of course, the system must be designed with that in mind. Or use a toothed belt.

Wow , sorry If I stirred something up :slight_smile:

For the record
The stepper chosen is oversized for the application and is direct drive so I really doubt that it will loose steps. But in the interest of doing it properly I think it would be a good idea to have some feedback.

That is unless I can't get the code to work :frowning: I am quite new to this.

Many thanks for the help, it is appreciated.

Scott

Scott_M:
But in the interest of doing it properly I think it would be a good idea to have some feedback.

That is unless I can’t get the code to work :frowning: I am quite new to this.

The encoder stuff will be much harder than the stepper stuff and if you are new to the Arduino it might be a showstopper.

That’s why I suggest you design the system so it can be expected to work without feedback. There will be nothing to prevent you adding feedback in the future if you find that it is necessary.

…R

PS

Wow , sorry If I stirred something up

Nothing to apologize for. We have just been entertaining ourselves in your absence.

If you want to prevent your Threads from drifting you need to attend to them frequently (a bit like keeping weeds out of a flower bed)

Hi Robin2

I certainly don’t mind you guys entertaining yourselves :slight_smile:

I have been having some email troubles and not receiving notifications, I think it is fixed now. So I should be able to monitor the “Flower Bed” more closely.

Thanks again for your help

Scott

Scott_M:
I have been having some email troubles and not receiving notifications, I think it is fixed now.

I have never bothered with that. And since the most recent upgrade of the Forum I'm not even sur it works properly - see the website section of the Forum.

I just check the forum directly to see what is new.

...R