# Stepper Motors and Limit Switches

Might be, but it's difficult for me to understand all the libraries and stuff like that

Might be easier for us to drive the motor directly, but since we want some kind of delay on some of the positions, I don't know what would be the easiest way.

We would also like to have acceleration and deacceleration before it hits the limit switches and be able to control the speed of the motors

bergan:
but it's difficult for me to understand all the libraries and stuff like that

sometimes it's more difficult to use a library that has many features that you don't need

what does this do

``````stepperX.setSpeedInMillimetersPerSecond(10.0);
``````

bergan:
We would also like to have acceleration and deacceleration before it hits the limit switches and be able to control the speed of the motors

how would you know that you're approaching a limit switch to begin decelerating?

wouldn't you want a constant speed and just stop when you reach a limit switch?

It would be possible with a constant speed, but I think it can damage the motors and the limit switches do to the moving parts being quite heavy?

Is it really good for the motors to go from 0 to "high" speed and then back to 0 directly?

And is it really good for the limit switches to get hit with "high" speed?

how do know when the motor is approaching the limit switch?

can the motor move past the limit switch?

The motor can not go past the limit switch

And we want to use the limit switch so we can see when the motor is in the right position.
So I'm thinking about making a code where I define the number of revolutions/ steps including acceleration and having the limit switch to make the program less sensitive to overstepping. So when it hits the limit switch the motor has around 10 percents of speed left to be more kind to all the components.

i don't see how you know when the motor is approaching the limit to start slowing down before detecting it.

there is no problem detecting the switch and issuing a command to stop the motor or simply stop stepping the motor. If inertia is an issue, then the motor speed, the step rate (time between steps) must be limited

I updated the previous answer with my thoughts how to be able to decrees the speed it's hitting the switches with.

But an example of how I mean in words,

700mm of travel to switch
then tell the motor to travel 730mm wit acceleration of 100mm and deacceleration of 100mm
but the switch stops the motor after 700mm Witch makes it stop at the same position without the risk of the motor not reaching the correct physical position do to overstepping.

Did I explain it better this time?

I'm also sorry for my lack of knowledge.

ok. that make sense.

so you have some idea of the # of steps to the limit switch and you slow down when you're close.

presumably after the first cycle, you know the # steps to each limit switch. It's only the first cycle that is unknown because you don't know where the motor was stopped.

some questions need more answers (little big league)

Here is some photos of the construction were we need to use the Arduino to control the movements

So we have some certain lengths and we know the maximum travel in every direction

I would also like to start the program with one button, stop the program with another one, and I would also like a third button that will start another "homing" program in this program.

How would that be coded and were in the program should I put it?

Thanks again for all the help

not sure you need 3 buttons and is there a difference between your "homing" procedure and something else.

presumably a single button can be used to start and stop (press the button again and it stops)

It could, but I need one button for start and one to stop because of the company's safety rules.

The homing procedure is the same as my regular program, just other directions. I need this in case someone starts the program and forgets to put the right thing somewhere. So that button would essentially make the whole sample that needs to be moved, be moved into the starting position

i posted some code showing the use of a state to sequence the motors that is captured in a sub-function, funcA(), called from loop(). i did it that way thinking this is one mode of operation and that it could conditionally be called from loop() depending what mode you want to be in (may need a way to reset state).

so you can create other sub-functions for other modes of operation. They can also use state variable to sequence the motors.

And you have yet additional state variables to determine what mode you're in that are used in loop() to determine which sub-function to call.

loop() would also have code to read buttons, a sub-function that monitors for button state changes and returns a button press which loop() would use to determine which mode of operate to exercise (which sub-function to call), including one to stop everything.

just break everything into testable pieces and put the piece together to create a system.

gcjr:
i posted some code showing the use of a state to sequence the motors that is captured in a sub-function, funcA(), called from loop(). i did it that way thinking this is one mode of operation and that it could conditionally be called from loop() depending what mode you want to be in (may need a way to reset state).

Cool, could you put a link here so I can see the code?

And how do you create sub-functions?

And how do you create sub-functions?

here's the structure i suggested

``````enum { But1, But2, But3 };
enum { ModeA, ModeB, ModeStop };

int  mode = ModeStop;

// ---------------------------------------------------------
void
modeA (void)
{
static int  state = 0;

switch (state)  {
case 0:
// start motor-1 moving clockwise toward position-A
state++;
break;

case 1:
// reverse motor-1 direction toward position-B
state++;
}
break;

case 2:
// stop motor-1
// start motor-2 moving clockwise toward position-C
state++;
}
break;

default:
state = 0;
mode  = ModeB;
break;
}
}

// ---------------------------------------------------------
void
modeB (void)
{
static int  state = 0;

switch (state)  {
case 0:
break;

default:
state = 0;
break;
}
}

// ---------------------------------------------------------
int
getButton (void)
{
int but = 0;
return but;
}

// ---------------------------------------------------------
void
stop (void)
{
}

// ---------------------------------------------------------
void
loop (void)
{
switch (getButton ())  {
case But1:
mode = ModeA;
break;

case But2:
mode = ModeB;
break;

default:
mode = ModeStop;
break;
}

// -------------------------------------
switch (mode) {
case ModeA:
modeA ();
break;

case ModeB:
modeB ();
break;

dfault:
stop ();
break;
}
}
``````

Thanks for all help

I will try these 2 suggestions

I will probably ask about something more later on, but thanks again

David