Need help with limit switch and steppermotor

Hello all,

This is my first post, so sorry about my bad English grammar.

i've been searching answers couple days now and decided to ask directly from the guys in the know :slight_smile: I would appreciate if someone could explain briefly in words how this could be achieved and what "classes" i need to use.

I am using mega 1280, adafruit mshield, nema 17 motor, and 2 pcs mechanical limit switches with arduinos internal pullups.

I have tried to get this to work but no success. Idea is simple, to drive one stepper motor 3000 steps, then release motor for 5 min, loop this until motor hits the mechanical switch and changes direction from forward to backward, and from backward to forward again when 2nd limit is hit.

And my problems regarding this, i'm confused about limitswitch interrupting running motor at precise time when limitswitch has been bumbed. I guess i can't use delay() at all? Motor needs to get interrupted when the limit is pressed.

I have tried several approaches but there has been mistakes on the way. I would really appreciate some help to clear this for once and for all. I know this is so easy, but just cant get it to work. I would post code, but my code is BS, any of them doesn't work and i dont know which of my sketches "has the best chance to work".

I have succesfully got the switches to work properly, by messing with button sketches, no problems there. Biggest problem is how to drive the system, and when limit is pressed, change direction.

So, any idea what would work with most reliable way? Any replies appreciated very much!

could you show us your code so that we can help you more

I can try to write new sketch to post. My current work is so full of newb mistakes that posting it makes more questions than answers. I mean, it's not commented, lots of unusable and code not related directly to this etc, not forum-friendly for sure :slight_smile: I can post the finished code for other n00bs for possible re-use tho. It's a simple lamp hanger that moves through threaded rod with limit switches on both ends.

I have no issues with mechanical switch, driving the motor, etc. Issue is the fact that i can be sure that my motor does not drive thru the limit switch and break it, how to make proper condition for motor to drive, but same time make sure that when switch is hit, direction changes. I have also made the terrible mistake to install and solder the switches and everything without testing my code with breadboard. I know, terrible n00b mistake!!! (please feel my self induced pain, lol)

What conditionals or commands would be proper way to control my system? I need the motor to drive 3000 steps and wait 5 min, this makes troubles for me. Should i make the motor driving some kind of function? How would it help?

I'm sorry my post is kinda waste of space. I would need someone to make me understand my issue and possible solution.

I do not know if this will help but this link(PDF) shows how to test a stepper with a couple of IC's and reverse
directions with simple switches..Perhaps you can get a hint to tie it into your arduino project.,d.eWU

well 3nsio, its ok if you post a newbie code on here, we are here to help you .To help you learn and together we grow and expand our knowledge. Anyway heres some pointer for you to look at

  • Finite State Machine
  • maybe A do while loop would be great too

What you're describing is possible, but usually with stepper motors you would only need one sensor/limit switch to discover the initial position and then you would use step counting to tell you where the motor was subsequently. In those terms your original problem count be solved by moving the stepper motor to a home position (indicated by a limit switch) in setup, and then moving it the required number of steps from there to get to your other positions. You can either count the steps and control the stepping speed yourself, or use a library to do it for you.: