adding condition in one dowhile loop in a if else if statment

Hi there,

This is my first Arduino project. I am basically stuck in a DO WHILE LOOP within an IF ELSE IF statement. following is the coding for the Arduino IDE. This coding is used by visual studio 2015 in VB. it contains a toggle on/off button. upon pressing On the LED starts blinking but when I press off button then no response. I had find out that it is in a loop and there is no condition to break the loop further I think that in order to the system to read the ELSE IF statement, it should read the serial port first. BUT I don't know how to do this . I had been trying for so long but failed. Can anyone here help me with this Please!

void setup() {
pinMode(52, OUTPUT);
Serial.begin (9600);

}

void loop() {
int val;

if (Serial.available()){
delay (100);
while (Serial. available()>0 )
val=Serial.read ();

if (val == '1'){

do{

digitalWrite(52,HIGH);
delay(500);
digitalWrite(52,LOW);
delay(500);}
while(val=='1');
}

}

else if (val == '0') {digitalWrite(52,LOW);};

}

do{
     
                       digitalWrite(52,HIGH);
                       delay(500);
                       digitalWrite(52,LOW);
                       delay(500);}
                       while(val=='1');

Once in that loop, there's no way out.

 if (Serial.available()){
    delay (100);

If there's something to read, waste enough time for the input buffer to overflow.
Not sensible.

Please use code tags when posting code.

I think you meant to say:

void setup() {
  pinMode(52, OUTPUT);
  Serial.begin (9600);
}

void loop() {
  static boolean blink = false;
  if (Serial.available()) {
    int val = Serial.read ();
    if (val == '1')
       blink = true;  // Turn blinker on
    if (val == '0')
       blink = false;  // Turn blinker off
  }

  if  (blink) {
    digitalWrite(52, HIGH);
    delay(500);
    digitalWrite(52, LOW);
    delay(500);
  }
}

thanks JOHNWASSER. your code really works. thanks again . a big relief for me.

Regards