Stepper Motors and Limit Switches

Hi

I have some trouble on my code for stepper motors and limit switchers…

I need to do 7 different movements in z- and x-axis, and for this, I’m using limit switchers so I can get the precise position, so it can repeat the same position over and over.

I’m doing one movement towards a limit switch. After that, I want to do another movement with another motor in a different direction towards a new limit switch.
Then I want the same motor, to go the opposite direction to another limit switch.
Then the other motor to another limit switch, and so on.

Any suggestions over how I should program it?

This is the program I’ve come up with so far, and it doesn’t work correctly:

The program will be an answer to this post since it is too many characters.

All kinds of help are appreciated, thanks in advance

Kind Regards
David

bergan:
This is the program I’ve come up with so far, a

you haven’t posted the code. (please post w/in tags, click “</>”)

Here is the code, hopefully, you can open it up

The name is: code but in Swedish

Thanks again guys

kod.pdf (273 KB)

bergan:
Here is the code

where?

look at your post

please post it as text, not a pdf

I can not post it as a text because it is too long

David

i doubt the text file of the code is bigger than the .pdf. post the code, .txt, as an attachment like you did the .pdf. if it’s really too long, post it in parts as several attachments

Like this?

Also, it would take an eternity to post the whole program in parts, since I only can make 1 post every 5 minutes.

kod.txt (16 KB)

was that so hard? :slight_smile:

gcjr:
was that so hard? :slight_smile:

It was until I did it :smiley:

Your code is way too long for something that doesn’t work. Try getting a single stepper working.

One issue I see with it is that you have a bunch of endless loops such as this one:

      while (true)
        // Movement 2 Z axis bath 1 Down
      {

That loop never ends, so nothing after it gets executed.

wildbill:
That loop never ends, so nothing after it gets executed.

Okey, my thought was to make it so that the next loop starts by the contact of the limit switch

Do you have any suggestions on how to do that in another way?

I'm doing one movement towards a limit switch. After that, I want to do another movement with another motor in a different direction towards a new limit switch.
Then I want the same motor, to go the opposite direction to another limit switch.
Then the other motor to another limit switch, and so on.

so rotate motor-1 to position-A, then in opposite direction to position-B.
then rotate motor-2 to position-C and then in opposite direction to position D

why don't you use a state machine, one state for each action. do something and then exit loop() instead of calling these while loopsj

in your case, loop() is simply a switch statement with 5 cases.

the zero case starts motor-1 toward position-A

case one reads the switch the position-A. if not, do nothing, else reverse direction and change state to 2

case two reads the switch for position B. when active, stop motor-1, start motor-2 and state = 3

and so on.

since it sounds like you want to do more, and possibly similar things, the above could be a sub-function

Getting rid of that while(true) and its siblings would be a good start. You can keep the one you're using to indicate an error condition, although I would be inclined to put that in a separate function rather than repeating the code over and over.

gcjr:
so rotate motor-1 to position-A, then in opposite direction to position-B.
then rotate motor-2 to position-C and then in opposite direction to position D

why don't you use a state machine, one state for each action. do something and then exit loop() instead of calling these while loopsj

in your case, loop() is simply a switch statement with 5 cases.

the zero case starts motor-1 toward position-A

case one reads the switch the position-A. if not, do nothing, else reverse direction and change state to 2

case two reads the switch for position B. when active, stop motor-1, start motor-2 and state = 3

and so on.

since it sounds like you want to do more, and possibly similar things, the above could be a sub-function

Okey, sounds pretty logical to me

Could you give me an example of a code for the first 2 movements including the switch?

Because then I can just copy it and try to put in delays in between?

hopefully you understand to break things into pieces and steps

#define posApin   2
#define posBpin   3
#define posCpin   4
#define posDpin   5

int  state = 0;

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

    case 1:
        if (! digitRead (posApin))  {
            // reverse motor-1 direction toward position-B
            state++;
        }
        break;

    case 2:
        if (! digitRead (posBpin))  {
            // stop motor-1
            // start motor-2 moving clockwise toward position-C
            state++;
        }
        break;
    }
}

void
loop (void)
{
    funcA ();
}

Thanks man

Just one or two more questions :smiley:

Where do I put delays in that code you just made?

why do you need delays?

Because this project is made for heat treatment, and we need delays for the different times the samples will be in the salt baths (hardening process)

So we need 2 different delays with a long time on, so we get the hardening right

since your using a library, isn't the step rate controlled by some library function?

i'm not familiar with stepper motor libraries, i just drive the motor directly