Troubles at breaking from loops

I developed this code to simulate a PWM signal for a step motor. I couldn't implement the stepper library because how my driver works, but that isin't the issue.
I cannot find a way for the loop function to stop when the limit switch is pressed (LOW) and so it remains indefinitely running. I already tried using break, but it just appears to be ignoring the state1 variable while it is in the loop. I'm using the ezButton library for the switch.

ls1.loop();
ls2.loop();
int state1 = ls1.getState();
int state2 = ls2.getState();
if (state1 == HIGH){
  while(state1 == HIGH){
    digitalWrite(4, HIGH);
    delayMicroseconds(5000);
    digitalWrite(4, LOW);
    state1;
    if(state1 == LOW){
      break;
    }
  }
}

Hi, @marvin0909
Welcome to the forum.

To add code please click this link;

In the while loop you have

state1;

What is that supposed to do?

Shouldn't it be

state1 = ls1.getState();

So you update state1 while in the loop.

Also look up how to use a while loop;
You don't need the break or if statement.

Tom... :smiley: :+1: :coffee: :australia:

That's their nature.

Why? (Don't tell me.)

It helps a lot if you post all your code. Often the problem is outside where a beginner thinks it is.

The break command only gets you out of the current structure you are in like a while. If you break in the loop function that will only result in the loop function being run again.

You need to use an if statement to stop pulsing your motor.

You also need two delays in that while statement otherwise the low time is going to be about 4uS.

In principle for-loops and while-loops have no place in Arduino code. The reason is that they result in blocking code. If state1 does not change for e.g. 20 seconds, you can basically not do anything else in those 20 seconds; you will have a hard time to e.g. blink a LED in those 20 seconds.

The answer to your question is given by TomGeorge

Another comment is about the use of magic numbers (the 4 in your digitalWrite statement). Define a const variable in the beginning of your code like

const uint8_t thePin = 4;

And use thePin in pinMode() and digitalRead() and/or digitalWrite(); if you ever want to move the pin to another pin, you only have to change it in one place and not at various places (with the risk that you overlook something).

ls1.loop();  // actualise state of "ls1"  just here before while-loop
ls2.loop(); // actualise state of "ls2"  just here before while-loop
int state1 = ls1.getState(); // assign state of "ls1" to variable state1
int state2 = ls2.getState(); // assign state of "ls2" to variable state2
if (state1 == HIGH){ // check if variable state1 is HIGH
  while(state1 == HIGH){  // start a while loopl that runs as long as variable state1 is HIGH
    digitalWrite(4, HIGH);  
    delayMicroseconds(5000);
    digitalWrite(4, LOW);
    state1;  // take variable state1 in the hand and put it down again (without having done anything with it
    if(state1 == LOW){ // check if variable state1 is LOW
      break;
    }
  }
}

as you never update the content of variable "state1" the if-condition

    if(state1 == LOW)

can never be true. the value of variable state1 will stay HIGH forever
if you want the variable "state1" to represent the actual state of your limit-switch you have to code

state1 = ls1.getState(); 

inside the while-loop
and if you have done it the condition here

  while(state1 == HIGH)

will terminate the while-loop
because as soon as state1 is assigned the value LOW
the condition
state1 == HIGH is no longer true

if variable state1 has value LOW this means
LOW == HIGH which results of course in false

best regards Stefan

It looks like what you want is:

void loop()
{
  ls1.loop();

  if (ls1.getState() == HIGH)
  {
    digitalWrite(4, HIGH);
    delayMicroseconds(5000);
    digitalWrite(4, LOW);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.