Loop section only if its ment to

First time post, sorry if its not correct. WIll update it if things are needed.
Just got my Arduino Eleven and love it! I'm currently building a project that reads a serial value and if that value is correct it makes an led green. If not it lights the red led and sounds a buzzer. I want to loop the buzzing and flashing until the value is correct. (using visual basic program, so when button and serial info is pushed). I just can not figure out how to loop the flashing and buzzing whilst also checking for an update to the serial data. Any help would be fantastic, pulling my hair out! photo attached just in case
TDLR: Want to loop buzzing and flashing of led but also check if serial data (set to value) has been updated, if so stop
Code:
```
int val;

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT); //buzzer
  pinMode(13, OUTPUT); //red led
  pinMode(7, OUTPUT); //green led
}
//this just sets the buzzer stuff. I googled it :
void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2;
  long numCycles = frequency * length/ 1000;
  for (long i=0; i < numCycles; i++){ digitalWrite(targetPin,HIGH); delayMicroseconds(delayValue); digitalWrite(targetPin,LOW); delayMicroseconds(delayValue);
  }
}

//This is where the problem starts
void loop() {
  val = Serial.read();
  if (val == 110) { //110 is dec ascii for 'n'
    digitalWrite(13, LOW);
    digitalWrite(7,HIGH);

}

if (val == 109){ //109 = m
  for (int i=0; i <= 2; i++){ //I want this to loop but only until serial = 110
      digitalWrite(7,LOW);
      digitalWrite(13, LOW); //change pin numbers if necessary
      delay(1000); // time inbetween flashes/buzzes
      digitalWrite(13, HIGH);
        buzz(12, 300, 1100);

}
}

}
```

you should make a small state machine, have a global variable called int state = 0;

void loop()
{
  if (Serial.available())
  {
    val = Serial.read();
    switch(val)
    { 
    case 'n': 
      state = 1; 
      break;
    case 'm': 
      state = 2; 
      break;
    default: 
      state = 0; 
      break;
    }
  }

  if (state == 0)
  {
    // switch all of or whatever
  }
  if (state == 1)  // n mode
  {
    digitalWrite(13, LOW);
    digitalWrite(7,HIGH);
  }
  if (state == 2)  // m mode
  {
    digitalWrite(7,LOW);
    digitalWrite(13, LOW); //change pin numbers if necessary
    delay(1000); // time inbetween flashes/buzzes  
    digitalWrite(13, HIGH);
    buzz(12, 300, 1100);
  }
}

ah thanks heaps! got it looping forever (until input) by removing the default (it kept defaulting after loop). Again, thanks heaps :slight_smile: