How to stop program by limit switch (solveed)

There is a 'report to moderator' link underneath every post. Click the one in your opening post and ask it to be merged with the other thread(s).

In the Thread I hve asked you to merge with this one you said you still have a problem.

Can you outline the problem here and explain what parts of the advice already given (in this Thread) you have tried and what was the result.

I suspect most of what you need is already here.

...R

The description in reply #13 is still not clear (to me). Can you write down in steps what needs to happen? Something like I did in reply #10.

Can you also please give a description what those two machines do? And how they relate (if they do) to Nema42, Nema34 and the pump?

I cannot explain easily.it Is so difficult for me to explain.

This code working 100% and machine working constantly so i need only to stop all when limit switch is pressed,
something like emergency stop.But after that i need normally upload my second code and control machine manually by joystick.i taught i can do with reset button but problem is board wont let me to upload sketch because button is pressed and stay pressed (stuck between metal parts )

/////////////////////////////
int Nema42matka   = 300;    //--- Nosturin matka (0,7 pelti alaspäin)
int Nema42matka2  = 450;   //--- 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 Nema42lamp    = 2;                      

int Imupumppu     = 5; 
int Imupumppulampu = 6;                      

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

int buttonPin     = 10;

/////////////////////////

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);

}


void loop() {

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

  buttonVal = digitalRead(buttonPin);


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

    }

    if (stepNumber == 1) {

      
      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);

                }
              }
            }
          }
        }
      }
    }
  }
}

Ahmo:
I cannot explain easily.it Is so difficult for me to explain.

At least 50% of the solution for any programming problem is wrapped up in a clear description - for your own benefit more than for ours. Spend some time writing an explanation for yourself that could be understood by your granny. (Seriously).

This code working 100% and machine working constantly so i need only to stop all when limit switch is pressed,
something like emergency stop.But after that i need normally upload my second code and control machine manually by joystick.i taught i can do with reset button but problem is board wont let me to upload sketch because button is pressed and stay pressed (stuck between metal parts )

This sounds like you want two modes of operation - automatic and manual. If so, that is entirely possible within a single program.

If you need something to stop when a switch is pressed you need to set a variable according to the state of the switch and in the code that makes the thing work you regularly check the state of that variable and only keep going if the variable shows that the switch was not pressed.

...R
PS, please edit your Original Post and put the title back to its original text.

I understand that you have difficulty explaining. But if you don't manage to explain properly, we will have a difficult time trying to understand and advise accordingly.

So lets try it in steps, shall we?

wait for button to be pressed and released
2)
activate pump
3)
move Nema42
4)
you tell us
5)
you tell us
...
...
101)
wait for button to be pressed and released (second time)
102)
you tell us
103)
you tell us
...
...
151)
if limit switch not activated back to 102 (or back to 2?)
152)
pump off, motors off
153)
joystick control

You can make the joystick code part of your current code so you don't have to upload new code.

And your code from reply #13 seems to have plenty '}' in the wrong place. It might work but if one sees something like

                 }
                }
              }
            }
          }
      }
    }
  }
}

either something is wrong in your approach (e.g. to many if/else which is not the case here) or '}' in the wrong place. It might work but it is not correct.

THANK YOU STERRETJE !!!!

wait for button to be pressed and released
2)
activate pump
3)
move Nema42
4)
move Nema34
5)
deactivate pump
6)
move nema34
7)
move nema42
101)
wait for button to be pressed and released (second time)
102)
do nothing
103)
do nothing
...
...
150)

if limit switch is not activated keep continue from beginning
1)
...
151)

if limit switch is activated
Nema42 go back delayMicroseconds(2000)
Nema34 stop or break
Pump stop or off
this option is better because after i can a put joystick code as you said.

I don't think that there is a second switch press. You press a switch, something happens. Next you press the switch again and the same thing happens. And so on and so on till the limit switch is activated.

Well, I think you're nearly there if the above is a correct description.

I would start by using names in you switch/case.

This is based on the code in reply #13

      switch (task1Status)
      {
        case PUMP_ON:
          ...
          ...
          break;
        case MOTOR1_FORWARD:
          ...
          ...
          break;
        case MOTOR2_FORWARD:
          ...
          ...
          break;
        case PUMP_OFF:
          ...
          ...
          break;
        case MOTOR2_BACKWARD:
          ...
          ...
          break;
        case MOTOR2_BACKWARD:
          ...
          ...
          break;
      } // end of switch

Note that the code does not use the fall-through of the cases as you did. The intention is that at the end of every step you you indicate what the next step will be. By repeatedly calling doOneThingOfTask1() in loop() (as you do), you will step through the steps

Next you can add an extra one in the beginning.

      switch (task1Status)
      {
        case WAIT_FOR_BUTTON:
          ...
          ...
          break;
        case PUMP_ON:
          ...
          ...
          break;
        case MOTOR1_FORWARD:
          ...
          ...
          break;
        case MOTOR2_FORWARD:
          ...
          ...
          break;
        case PUMP_OFF:
          ...
          ...
          break;
        case MOTOR2_BACKWARD:
          ...
          ...
          break;
        case MOTOR1_BACKWARD:
          ...
          ...
          break;
      } // end of switch

Next we can associate numbers with those names. Place this before setup

#define WAIT_FOR_BUTTON 1
#define PUMP_ON 2
#define MOTOR1_FORWARD 3
#define MOTOR2_FORWARD 4
#define PUMP_OFF 5
#define MOTOR2_BACKWARD 6
#define MOTOR1_BACKWARD 7
#define JOYSTICK_CONTROL 8

// your variables, constants  and pins here
...
...

int task1Status = WAIT_FOR_BUTTON;

...
...

The numbers are totally irrelevant but each name needs an unique number. I have added something for the joystick. Note that task1Status is now assigned the value WAIT_FOR_BUTTON (so you need to change that).

Next you can read the button and the limit switch. The limit switch is also wired with internal pull-up and should be normally closed (so reads low if not activated). This is done so in case of a broken wire, your machine will stop.

void doOneThingOfTask1()
{
  prevButtonVal = buttonVal;
  buttonVal = digitalRead(buttonPin);
  limitVal = digitalRead(limitSwitch);

  switch (task1Status)
  {
    ...
    ...
  } // end of switch

And now you can fill in your cases. For the waiting on the button press

    case WAIT_FOR_BUTTON:
      if (buttonVal == LOW && prevButtonVal == HIGH)
      {
        task1Status = PUMP_ON;
      }
      break;

Basically the same as you did, but the next task1Status is specifically set if the button is pressed. If the button is not pressed, task1Status will not change and the next time that you call doOneThingOfTask1() it will check the button again.

Because loop is executed thousands of times per second (if you don't use delay), the next test for the button press will be within a millisecond.

All the other cases basically stay as they are but you have to specifically indicate the next step that will be executed.

The exception is the MOTOR1_BACKWARD step where you want to test the limit switch.

    case MOTOR1_BACKWARD:
      digitalWrite(Nema42Pin, HIGH);
      for (int n = 0; n < Nema42matka2; n++)
      {
        digitalWrite(Nema42Dir, HIGH);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir, LOW);
        digitalWrite(Nema42lamp, LOW);
      }
      // if limit switch not activated
      if (limitVal == LOW)
      {
        // we can do another sheet
        task1Status = WAIT_FOR_BUTTON;
      }
      else
      {
        // do something else
        task1Status = JOYSTICK_CONTROL;
      }
      break;

Your modified doOneThingOfTask1() in full

void doOneThingOfTask1()
{
  prevButtonVal = buttonVal;
  buttonVal = digitalRead(buttonPin);
  int limitVal = digitalRead(limitSwitch);

  switch (task1Status)
  {
    case WAIT_FOR_BUTTON:
      if (buttonVal == LOW && prevButtonVal == HIGH)
      {
        // if button pressed, go to next step
        task1Status = PUMP_ON;
      }
      break;
    case PUMP_ON:
      digitalWrite(Imupumppu, HIGH);
      digitalWrite(Imupumppulampu, HIGH);
      // go to next step
      task1Status = MOTOR1_FORWARD;
      break;
    case MOTOR1_FORWARD:
      delay(2000);
      digitalWrite(Nema42Pin, LOW);
      for (int n = 0; n < Nema42matka; n++)
      {
        digitalWrite(Nema42Dir, LOW);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir, HIGH);
        digitalWrite(Nema42lamp, HIGH);
      }
      // go to next step
      task1Status = MOTOR2_FORWARD;
      break;
    case MOTOR2_FORWARD:
      digitalWrite(Nema34Pin, LOW);
      for (int n = 0; n < Nema34matka; n++)
      {
        digitalWrite(Nema34Dir, LOW);
        delayMicroseconds(Nema34nopeus);
        digitalWrite(Nema34Dir, HIGH);
        digitalWrite(Nema34lamp, HIGH);
      }
      // go to next step
      task1Status = PUMP_OFF;
      break;
    case PUMP_OFF:
      delay(2000);
      digitalWrite(Imupumppu, LOW);
      digitalWrite(Imupumppulampu, LOW);
      // go to next step
      task1Status = MOTOR2_BACKWARD;
      break;
    case MOTOR2_BACKWARD:
      delay(2000);
      digitalWrite(Nema34Pin, HIGH);
      for (int n = 0; n < Nema34matka; n++)
      {
        digitalWrite(Nema34Dir, LOW);
        delayMicroseconds(Nema34nopeus);
        digitalWrite(Nema34Dir, HIGH);
        digitalWrite(Nema34lamp, LOW);
      }
      // go to next step
      task1Status = MOTOR1_BACKWARD;
      break;
    case MOTOR1_BACKWARD:
      digitalWrite(Nema42Pin, HIGH);
      for (int n = 0; n < Nema42matka2; n++)
      {
        digitalWrite(Nema42Dir, HIGH);
        delayMicroseconds(Nema42nopeus);
        digitalWrite(Nema42Dir, LOW);
        digitalWrite(Nema42lamp, LOW);
      }
      // if limit switch not activated
      if (limitVal == LOW)
      {
        task1Status = WAIT_FOR_BUTTON;
      }
      else
      {
        task1Status = JOYSTICK_CONTROL;
      }
      break;
    case JOYSTICK_CONTROL:
      if (joystick_control() == true)
      {
        task1Status = WAIT_FOR_BUTTON;
      }
      break;
  } // end of switch
}

I've also added a case for the joystick control.

Below is a joystick controller simulation that uses a non-blocking delay to simulate a long-ish action (5 seconds in the example).

bool joystick_control()
{
  static unsigned long delayStarttime = 0;
  unsigned long currentTime = millis();

  // if delay not started
  if (delayStarttime == 0)
  {
    delayStarttime = currentTime;
  }
  // else if delay in progress, check if delay has lapsed
  else if (currentTime - delayStarttime >= 5000)
  {
    // reset delay starttimme so next time we call this function the deay start time will be set again
    delayStarttime = 0;
    // indicate we're done
    return true;
  }
  // indicate we're not done yet
  return false;
}

You will obviously have to implement a real joystick control.

The reason that I show this code is that you basically should not use delay but use non-blocking delays based on millis(). I leave it up to you if you want to replace your current delays by something based on the above.

I hope this helps; it's probably not perfect but I wanted you to be able to follow and making massive changes would probably have made it more difficult for you to follow.

PS code not tested.

Really appreciated your help.we still have good people in this world,my self helping children through Unicef.

Your code working but only ones and then limit switch mast be activated to let me start job again.
I mast have two cases

push button // start program;
from case 0: to case 5:

2)push button // wait for case 1)

push button // start program;
from case 0: to case 5:

2)push button // wait for case 1)

like this forever until limit switch is pressed (we can put that code inside of case 5: so when limit Switch is activated stop pump,stop nema34,go back nema42 delayMicroseccond (2000)
if limit Switch not activated just keep continue

I tried with this in case 5: with lamps to see if is working an yeaaaaaa is working

Thanks to you and Robin2 l learn many things

I made my code done

so if somebody need i can help and hire is code:

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





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

int Imupumppu     = 5; 
int Imupumppulampu = 6;                      

int Nema34Pin     = 8; 
int Nema34Dir     = 9; 
int Nema34lamp    = 7;
int limitSwitch   = 11;
int buttonPin     = 10;

/////////////////////////

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);
}


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(3000);
              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);
               
                }
           if (digitalRead (limitSwitch)==LOW) {
              
              digitalWrite(Nema34lamp,HIGH);
              delay(1000);
              digitalWrite(Nema34lamp,LOW);
              delay(1000);
              return;
           
           
         
                }
              }
            }
          }
        }
      }
    }
  }
}