Limit Switches and Stopping Motor

1,872 views
Skip to first unread message

Yuri Konratev

unread,
Nov 26, 2013, 1:46:26 PM11/26/13
to accels...@googlegroups.com
Hello everyone I'm working on a project to  basically build a pick and place system. At the end of each axes I have a limit switch to stop the pick and place moving when hit. I have wired up the limit switches correctly and I know they work correctly. The code should be pretty simple but for some reason it's not working to stop the motors when the switches are pressed. I am also interfacing Arduino with Matlab so I'm not sure if that would be causing any problems.

There's too much code to post the whole thing so I'll just show what I'm working with:

AccelStepper stm1(1,11,10); // stepper motor 1 declared

void setup() {
 pinMode(6,INPUT); // limit switch 2
  pinMode(5,INPUT); // limitswitch 1
}
  
  if (stm==1) { // if stepper motor 1 is called from matlab
 
   if ((digitalRead(5) ==1) && (digitalRead(6) ==1)){ //if switches not pressed motor runs where it has to as told my Matlab
     stm1.moveTo(val2*dir*90); //Changed here
        
     if ((stm1.distanceToGo() != 0)) {
  
   
      stm1.runToNewPosition(val2*90);

      stm1.run();
     }
   }
    else if  ((digitalRead(5) ==0) || (digitalRead(6) ==0)) { //if either switch is pressed
      stm1.setSpeed(0);
    }
      }

So basically I'm telling the motors to move from Matlab which works fine but the code for the limit switches doesn't work.
Limit switches are hooked up to pins 5 and 6 so while its not pressed the motor should go where it has to go but if either one is pressed it should stop the motor.
I've tried many different things along these lines but all that happens is the motors just run and when I press the button nothing happens.
If anyone would be able to help me in any way I'd appreciate it so much thank you!

Mike McCauley

unread,
Nov 26, 2013, 4:07:33 PM11/26/13
to accels...@googlegroups.com
Hello,

I think you misunderstand how some of the library API works, and you should
carefully read the doc again.

calling setSpeed(0) is probably not appropriate for what you want. It is for
use with runSpeed().

Calling moveTo() frequently in the loop is unnecessary and inefficient. Call it
only when you have a new target position.

Calling runToNewPosition() followed by run() is strange. runToNewPosition()
blocks until it reaches the new position. From the doc: "Dont use this in
event loops, since it blocks.".

The general strategy for accelstepper is to always call run() as often as
possible (ie every time through the main loop), and (only very occasionally)
call other api functions to change/set position when external events occur.


You probably want something like this:

loop()
{
if (have new target from matlab)
moveTo(newtarget)
if (have hit negative limit)
setCurrentPosition(minimumposition)
if (have hit positive limit)
setCurrentPosition(maximumposition)
run();
}

also you probably need to consider what happens with limit switch bounces.

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Yuri Konratev

unread,
Nov 26, 2013, 8:40:17 PM11/26/13
to accels...@googlegroups.com
Thank you for the response Mike. 
I have used setSpeed(0) in another piece of code and it stops the motor immediately which is exactly what I need.
I have tried doing what you said and it still doesn't work so at this point I'm lost 
Reply all
Reply to author
Forward
0 new messages