How can I deactivate the while-loop?

How can I deactivate the while loop in the following code?

int LED[] = {0, 10, 2, 3, 4, 5, 6, 7, 8};
int pinCount = 9;
int taster = 11;
int x = 1;
int f = 1;
bool merker = 0;
unsigned long previousMillis = 0;
int intervall = 100;

void setup() {
 Serial.begin(9600);
 for(int pinZahl = 0; pinZahl < pinCount; pinZahl++) {
  pinMode(LED[pinZahl], OUTPUT);
  }
  pinMode(taster, INPUT); 
}

void loop()
{
  int tasterstatus = digitalRead(taster);
    unsigned long currentMillis = millis();
      if(tasterstatus == 1) {
          if(currentMillis - previousMillis >= intervall) {
              previousMillis=currentMillis;
              merker = !merker;
              Serial.print(merker); }
        }
          
    while(merker == 1) {  
      if(x == (pinCount)){
          x = 7;
            f = -1;
            }

      if(x == 0){
            x = 2;
            f = 1;
            }

      for(x;(x<pinCount)&&(x>0); x+=f){
                digitalWrite(LED[x], 1);
                delay(500);
                digitalWrite(LED[x], 0);
                delay(500);
                
           } 
     }
}
      

Use the break() function to exit a while loop.

To exit the while() you created, you coded while(merker == 1) which I take to mean that somewhere something is going to chnage merker to a 0. Where in the while() you wrote is a check made to see if the merker state has changed and that will be the cause to exit the while().

this is what I got so far.

int LED[] = {0, 10, 2, 3, 4, 5, 6, 7, 8};
int pinCount = 9;
int taster = 11;
int x = 1;
int f = 1;
bool merker = 0;
unsigned long previousMillis = 0;
int intervall = 100;

void setup() {
  Serial.begin(9600);
  for (int pinZahl = 0; pinZahl < pinCount; pinZahl++) {
    pinMode(LED[pinZahl], OUTPUT);
  }
  pinMode(taster, INPUT);
}
//
bool Tick = true;
void loop()
{
  if ( Tick )
  {
    if ( digitalRead(taster) == HIGH)
    {
      previoustMillis = millis();
      Tick = false;

    } else {
      while (merker == 1)
      {

        if (x == (pinCount)) {
          x = 7;
          f = -1;
        }

        if (x == 0) {
          x = 2;
          f = 1;
        }

        //for(x;(x<pinCount)&&(x>0); x+=f){
        //          digitalWrite(LED[x], 1);
        //          delay(500);
        //          digitalWrite(LED[x], 0);
        //          delay(500);
        //     }

        if ( (millis() - previousMillis) >= intervall)
        {
          merker = 0;
          Tick = false;
          Serial.print(merker);
        }
      }
    }
  } //if ( Tick )
  ////


}


just need to make the LED blink.

Nowhere in your while loop do you change the value of merker. I'm not sure exactly what you intend to do but it appears that changing the while to an if may solve your issue. Is taster a button? If so, how is it wired?

merker is defined as a bool. It is customary to use true and false for the value of bool types.

Also, I'm not sure what Arduino you are using but some of them (UNO and Nano for sure) reserve pins 0 and 1 for the serial port. setup() configures the pin mode for pin 0 which may be an issue.

It is not a break() function but a break statement...

bool somethingHappened = False;
while(True) {
     Somecode (that might set somethingHappened True)
     if(somethingHappened) {
          break;
     }
}

Setting the condition of the while loop to False is a better way to leave. Break is a bit like an emergency exit...

I believe your design is not mature enough to start writing the code. This has little to do with undferstanding how a WHILE loop works.

Once Tick becomes FALSE, there is not code to make it TRUE again, so your sketch will not do anything useful.

Also, x is always 1, it never becomes 9 (pinCount)

You have 9 LEDs. Perhaps if you share what is the intended behavior of the sketch...

And, your code has compilation errors.

i think your trying to use a button to enable code that alternately the cycles thru a sequence of LEDs for an interval and then stops for an interval.

there are several problems:

  • as already mentioned, there's nothing to change the state of "merker" inside the loop.
  • a while loop isn't needed since loop() is repeated called. instead an if can be used and an index into the Led pins array needs to be incremented and wrapped in each iteration
  • i assume you want the taster to toggle an "run" flag that enables the timer that alternately toggles the state of "merker"
  • but an "interval" of 100 msec and a delay between LEDs on/off of 500 msec doesn't make sense, shouldn't "interval" be many times the LED delay

consider
if "taster" really is just an input possibly from some other device, just delete the code that checks for a button press and replaced "but" with "run"

#undef MyHW
#ifdef MyHW
byte  PinLED [] = { 10, 11, 12, 13 };
byte  PinTaster = A1;

#else
byte  PinLED [] = {0, 10, 2, 3, 4, 5, 6, 7, 8};
byte  PinTaster = 11;
#endif

byte butState;      // see setup()

unsigned   idx;     // default to zero;
bool merker;
bool run;

unsigned long previousMillis;
unsigned long Intervall = 2000;     // Capitalize Constants
unsigned long LedDelay  =  100;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void loop ()
{
    byte but = digitalRead (PinTaster);
    if (butState != but)  {
        butState = but;
        delay (20);             // debounce

        if (LOW == but)  {      // pressed
            run = ! run;
            if (! run)
                merker = 0;
        }
    }

    unsigned long currentMillis = millis ();
    if (run && currentMillis - previousMillis >= Intervall) {
        previousMillis=currentMillis;
        merker = ! merker;
    }

    if (merker) {
        digitalWrite (PinLED [idx], On);
        delay (LedDelay);
        digitalWrite (PinLED [idx], Off);
        delay (LedDelay);

        if (sizeof(PinLED) <= ++idx)
            idx = 0;
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    for (idx = 0; idx < sizeof(PinLED); idx++) {
        pinMode      (PinLED [idx], OUTPUT);
        digitalWrite (PinLED [idx], Off);
    }

    pinMode (PinTaster, INPUT_PULLUP);
    butState = digitalRead (PinTaster);
}

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