How to stop program by limit switch (solveed)

Is it possible stop program when limit switch is pressed?
If yes witch code we need to use?

In its simplest form, skip the rest of loop() if the limit switch is activated.

void loop()
{
  if (digitalRead(limitSwitch) == HIGH)
    return;

  // other code here
}

Ahmo:
Is it possible stop program when limit switch is pressed?
If yes witch code we need to use?

You cannot "really stop" a program, but you can finish it with an infinite do-nothing-loop.

For example:
if (limitSwitchPressed()) while(1);

The code for an infinite do-nothing-loop is "while(1);"

Just write a function named "limitSwitchPressed(), returning a 'bool' value of true when the button is turning its state from "released" to "pressed" state or false otherwise.

If you want to definitively stop your software, you can use the abort() function, e.g. :

void setup() {

  Serial.begin(250000);
}


void loop() {
  static uint8_t count = 5;
  Serial.println(" I am alive");

  if ( count-- == 1) {

    Serial.flush();
    abort();
  }

  delay(2000);
}

i tried to put in to my code but wont start program because my push button.

//////////////////////////////////////////////
int Nema42matka    = 1000;   //--- Nosturin matka (0,7 pelti alaspäin)
int Nema42matka2  = 1000;   //--- Nosturin matka (2 x 0,7 pelti ylöspäin)
int Nema42nopeus  = 3000;   //--- Nosturin nopeus
int Nema34matka    = 8000;  //--- Kuljetin matka 
int Nema34nopeus  = 500;    //--- Kuljetin nopeus
////////////////////////////////////////////





/////////////////////////////////////
int Nema42Pin    = 3;   ///
int Nema42Dir     = 4;   ///
                                  ///
int Imupumppu    = 5;   ///
                                  ///
int Nema34Pin    = 6;   ///
int Nema34Dir     = 7;   ///
int buttonPin       = 10; ///
////////////////////////////////////

int task1Status=0;
int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;
int limitSwitch   = 8;




void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

}


void loop() {

  static uint8_t count = 5;
  Serial.println(" I am alive");

  if ( count-- == 1) {

    Serial.flush();
    abort();
  }

  delay(2000);


  doOneThingOfTask1();
}
void doOneThingOfTask1()
{
  prevButtonVal = buttonVal;

  buttonVal = digitalRead(buttonPin);



  if (buttonVal == LOW && prevButtonVal == HIGH) {
    stepNumber ++;
    if (stepNumber > 1) {
      stepNumber = 0;

    }

    if (stepNumber == 1) {

      task1Status=0;
      switch (task1Status)

      {

      case 0:

        {   
          digitalWrite(Imupumppu,HIGH);


        }
      case 1:
        {

          delay(2000);
          digitalWrite(Nema42Pin,LOW);
          for(int n = 0; n <Nema42matka; n++) { 
            digitalWrite(Nema42Dir,LOW);
            delayMicroseconds(Nema42nopeus);
            digitalWrite(Nema42Dir,HIGH);

          }
        case 2:
          {

            digitalWrite(Nema34Pin,LOW);
            for(int n = 0; n <Nema34matka; n++) { 
              digitalWrite(Nema34Dir,LOW);
              delayMicroseconds(Nema34nopeus);
              digitalWrite(Nema34Dir,HIGH);

            }
          case 3:
            {

              delay(2000);
              digitalWrite(Imupumppu,LOW);

            }
          case 4:
            {
              delay(2000);
              digitalWrite(Nema34Pin,HIGH);
              for(int n = 0; n <Nema34matka; n++) { 
                digitalWrite(Nema34Dir,LOW);
                delayMicroseconds(Nema34nopeus);
                digitalWrite(Nema34Dir,HIGH);

              }
            case 9:
              {
                digitalWrite(Nema42Pin,HIGH);
                for(int n = 0; n <Nema42matka2; n++) { 
                  digitalWrite(Nema42Dir,HIGH);
                  delayMicroseconds(Nema42nopeus);
                  digitalWrite(Nema42Dir,LOW);


                }
              }
            }
          }
        }
      }
    }
  }
}
      task1Status=0;
      switch (task1Status)

These two lines mean that task1Status is always zero when you try to check it in the switch() statement.

You seem to have a lot of 'state variables' counting, steps and status or whatever. They are all being incremented together - each time count increments, you call doOneThingOfTask1() which has its own counter. That's going to cause problems. The task1Status seems to go up to 9 but due to the outer loop, it's only ever going to be called 5 times.

If the outer loop needs to know when the inner one has finished, then have it return a value to indicate that it's finished. Or give the outer loop the counter so that it then calls the doOneThingOfTask1() with a specific step number that it wants done.

Ahmo:
i tried to put in to my code but wont start program because my push button.

Did the program ever work as you expected. One thing I noticed is that you don't have break statements in your 'cases'. This means that if task1Status equals 0 the code for all cases will be executed and if e.g. task1Status equals 3, cases 3..9 will be executed. You also never change task1Status; it's 0 and stays 0 forever.

Is that your intention?

OK i make simple code and how to stop lop or my vacuum pump

without digitalWrite(Imupumppu,LOW);

because i will make a new code and i wish stop loop by limit switch.

my code it will be with push button.when i push button first time program will start and finish.
when i push second time button program will wait and when i push third time program will start and repeatedly

so i need limit switch to stop all my stepper motors and vacuum pump. and i have code with joystick so i can move stepper motors to home position.

/////////////////////////////
int Nema42matka   = 50;    //--- Nosturin matka (0,7 pelti alaspäin)
int Nema42matka2  = 100;   //--- Nosturin matka (2 x 0,7 pelti ylöspäin)
int Nema42nopeus  = 3000;  //--- Nosturin nopeus
int Nema34matka   = 8000;  //--- Kuljetin matka 
int Nema34nopeus  = 500;   //--- Kuljetin nopeus
/////////////////////////////





/////////////////////////
int Nema42Pin     = 3; //
int Nema42Dir     = 4; //
//
int Imupumppu     = 5; //
// 
int Nema34Pin     = 6; //
int Nema34Dir     = 7; //
int buttonPin     = 10;//
/////////////////////////


int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;
int limitSwitch    =8;




void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(250000);
}


void loop() {

  if (digitalRead(limitSwitch) == HIGH)
    return;


  { 
    prevButtonVal = buttonVal;

    buttonVal = digitalRead(buttonPin);


    if (buttonVal == LOW && prevButtonVal == HIGH) {
      stepNumber ++;
      if (stepNumber > 1) {
        stepNumber = 0;


      }

      if (stepNumber == 1) {



        digitalWrite(Imupumppu,HIGH);


      }
    }
  }
}

Ahmo:
and i wish stop loop by limit switch.

my code it will be with push button.when i push button first time program will start and finish.
when i push second time button program will wait and when i push third time program will start and repeatedly

so i need limit switch to stop all my stepper motors and vacuum pump. and i have code with joystick so i can move stepper motors to home position.

This is all very confusing. You need to provide a proper explanation of the machine you want to control.

I can understand the use of a limit switch to stop a stepper motor when the thing it is moving reaches the end of its travel - for example the head of a 3D printer.

But I don't understand how that idea can also relate to a vacuum pump.

The usual way to get a stepper motor to stop when a limit switch is reached is to move it one step, check the switch and repeat that until the switch is triggered.

It will be much easier to help if you write a program that moves your stepper motor correctly with your best attempt to include code for the limit switch, and post your program. Then we will have something specific on which to give advice.

...R
Stepper Motor Basics
Simple Stepper Code

Hi,
What is the application, what is the project?

How is your limit switch wired?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom. :slight_smile:

Google translate of the first comments in @Ahmo's code in reply #7; it seems to be Finnish.

/*
Nosturin matka    The crane travel
pelti alaspäin    sheet metal down
pelti ylöspäin    sheet metal up
Nosturin nopeus   The crane speed
Kuljetin matka    The conveyor mobile
Kuljetin nopeus   conveyor speed
*/

So we seem to have a machine that picks up metal sheets, moves them and drops them.

Ahmo:
without digitalWrite(Imupumppu,LOW);

Or does not drop them? What do you mean by this?

Ahmo:
my code it will be with push button.when i push button first time program will start and finish.
when i push second time button program will wait and when i push third time program will start and repeatedly

Please try to describe in more detail. To select the 'wait option', do you push twice in quick succession? Or does it wait for the single push to be completed and next when the button is pushed again wait (for what :wink: ).

Ahmo:
my code it will be with push button.when i push button first time program will start and finish.

So what needs to happen?
1)
Lower vacuum pump to sheet.
2)
Apply vacuum.
3)
Move vacuumpump up.
4)
Move sheet till limit switch activated.
5)
Stop motors.
6)
Release vacuum.
7)
Use joystick to move motors back to home position.

Is this more or less the sequence? Please correct what is wrong in above.

Ahmo:
when i push second time button program will wait

Wait for what?

Ahmo:
and when i push third time program will start and repeatedly

So this will be (based on the single press description above)
1)
Lower vacuum pump to sheet.
2)
Apply vacuum.
3)
Move vacuumpump up.
4)
Move sheet till limit switch activated.
5)
Stop motors.
6)
Release vacuum.
7)
Move motors back to home position (automatically, no joystick)

8)

go to 1

Ahmo:
so i need limit switch to stop all my stepper motors and vacuum pump

I think that that makes sense; see my attempt to describe the 'single' and 'repeated' action.

Please correct anything that is wrong and add more details if needed.

He he!! I LOVE YOU GUYS!!!!

I found easy way to so solve my problem.

I will use arduino reset button as a (limit switch to stop program)

Because my code start by pressing (push button " buttonPin")

so if arduino go to reset it will stop program and wait for somebody to press push button.

Then i can upload second code for moving two stepper motors by joystick.

Move them to position from they will start to do job when button is pressed.

Thank you guys. A r d u i n o is amazing . Definitely the best thing what i found and start to learn.

I'm lost. Why upload a second code?

But anyway, congratulations on finding a solution.

Hi guys.

I was wrong about reset button to stop program or void loop.
I can stop program by pressing reset button but then button stay stuck between two metal parts and then cannot upload second code for moving my Nema42 stepper motor.
My question is:

How to stop program by limit switch or turn off vacuum pump,turn off nema34 stepper,and move Nema42 cw direction.

I have two programs and they to same job so i will upload 1 and if we cannot solve then i will post second one.

My code start with push button.

I have two machines.Second machine push button and start first one (connected on arduino board) and firs 1 do job.
Then second machine push button again.In this case firs one do nothing(waiting until second one finish job).
And like this forever until limit switch is pressed (hire i need code for limit switch) to stop program or turn off vacuum pump, turn off nema34 stepper,and move Nema42 cw direction.

int Nema42matka     = 350;       //   NEMA42 CCW 
int Nema42matka2   = 400;       //   NEMA42 CW
int Nema42nopeus   = 2000;       //   NEMA42  SPEED 
int Nema34matka    = 8000;      //    nema34 cw and ccw
int Nema34nopeus   = 500;        //    nema34 speed


int Nema42Pin     = 3;
int Nema42Dir     = 4; 
int Nema42lamp    = 2;        // lamp                

int Imupumppu     = 5;         // vacuum pump
int Imupumppulampu = 6;    // lamp                    

int Nema34Pin     = 8; 
int Nema34Dir     = 9; 
int Nema34lamp    = 7;        // lamp

int buttonPin     = 10;          
int limitSwitc    =  11          //code missing


int task1Status=0;
int buttonVal     = 0;
int prevButtonVal = 0;
int stepNumber    = 0;





void setup()

{

  pinMode(Nema42Pin,OUTPUT);
  pinMode(Nema42Dir,OUTPUT);
  pinMode(Nema34Pin,OUTPUT);
  pinMode(Nema34Dir,OUTPUT);
  pinMode(Imupumppu,OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(Nema42lamp,OUTPUT);
  pinMode(Imupumppulampu,OUTPUT);
  pinMode(Nema34lamp,OUTPUT);
  pinMode(limitSwitch,INPUT);   // code missing
 
}


void loop() {

  doOneThingOfTask1();
}
void doOneThingOfTask1()
{
  prevButtonVal = buttonVal;

  buttonVal = digitalRead(buttonPin);


  if (buttonVal == LOW && prevButtonVal == HIGH) {
    stepNumber ++;
    if (stepNumber > 1) {
      stepNumber = 0;

    }

    if (stepNumber == 1) {

      task1Status=0;
      switch (task1Status)

      {

      case 0:

        {   
          digitalWrite(Imupumppu,HIGH);
          digitalWrite(Imupumppulampu,HIGH);


        }
      case 1:
        {

          delay(2000);
          digitalWrite(Nema42Pin,LOW);
          for(int n = 0; n <Nema42matka; n++) { 
            digitalWrite(Nema42Dir,LOW);
            delayMicroseconds(Nema42nopeus);
            digitalWrite(Nema42Dir,HIGH);
            digitalWrite(Nema42lamp,HIGH);

          }
        case 2:
          {

            digitalWrite(Nema34Pin,LOW);
            for(int n = 0; n <Nema34matka; n++) { 
              digitalWrite(Nema34Dir,LOW);
              delayMicroseconds(Nema34nopeus);
              digitalWrite(Nema34Dir,HIGH);
              digitalWrite(Nema34lamp,HIGH);
            }
          case 3:
            {

              delay(2000);
              digitalWrite(Imupumppu,LOW);
              digitalWrite(Imupumppulampu,LOW);

            }
          case 4:
            {
              delay(2000);
              digitalWrite(Nema34Pin,HIGH);
              for(int n = 0; n <Nema34matka; n++) { 
                digitalWrite(Nema34Dir,LOW);
                delayMicroseconds(Nema34nopeus);
                digitalWrite(Nema34Dir,HIGH);
                digitalWrite(Nema34lamp,LOW);
              }
            case 5:
              {
                digitalWrite(Nema42Pin,HIGH);
                for(int n = 0; n <Nema42matka2; n++) { 
                  digitalWrite(Nema42Dir,HIGH);
                  delayMicroseconds(Nema42nopeus);
                  digitalWrite(Nema42Dir,LOW);
                  digitalWrite(Nema42lamp,LOW);
                }
              }
            }
          }
        }
      }
    }
  }
}

Why have you started two Threads with this question (this is the other one) when your Original Thread on the subject would have been a much more sensible place so that we could see the whole background.

I am suggesting to the Moderator to merge them.

...R

i am so sorry it was by accident.I marked as do not read it.

Please also ask the Moderator to merge this with your Original Thread on this project as there is a lot of relevant background info in it which does not need to be repeated.

And WHY have you confused things by changing the title of that Thread? People took some trouble to try to help you in that Thread and now you seem to consider that they were just wasting their time.

...R
Edited to correct link - apologies for any confusion

You are right it will not happen anymore.I am a promise.i still learning about arduino and english to. :-[

I dont know how to conntact moderator. :blush:

i opened new topic for help to conntact moderator.Probably the wrong place :blush:

Hi Robin.i contact moderator to merge topics.