The structure you have is actually fine if you want a SINGLE blink per input. Change the inner 'while's to 'if's.
If you want repetitive blinking, with the inputs changing the state, the structure you have isn't really ideal, but can be used.What you might try is adding an additional condition to each of the inner while loops, similar to this:
while (incomingByte =='1' && Serial.available()==0)
This will cause the inner loop to end when another input is available, so that the outer loop can read it.
A better overall structure for the code could be selected, as well, but I will avoid that for the moment.
This doesn't require any 'while' loops be used since the "void loop()" structure will loop indefinitely on its own.
My suggestion would be check if there's a character available in the serial buffer and if so, read it. Then based on what that character is, either '1' or '2', set your delay variable accordingly, then toggle the output pin as desired. If the character isn't '1' or '2', don't do anything other than keep toggling the pin.
int togglePin = 2; // pin to toggle
int incomingByte = '1'; // for incoming serial data, default to 'slow' blink
int randomDelay; // stores random slow blink rate, will change later
/* Setup system */
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(togglePin, OUTPUT); // set toggling pin to output mode
}
/* Main program loop */
void loop() {
if (Serial.available()) { // Character in serial buffer?
incomingByte = Serial.read(); // get the character
Serial.print ("The incomingByte value is: ");
Serial.println (incomingByte, DEC); // Output what was typed to the user
}
if (incomingByte == '1'){ // Was the character a '1'?
randomDelay = random(500,1000); // generate a "slow" random delay
Serial.print ("Now disrupting in slow mode with delay: ");
Serial.println(randomDelay); // Output delay interval to the user
incomingByte = '0'; // set to '0' so we don't jump back in here later
}
if (incomingByte == '2'){ // Was the character a '2'?
randomDelay = random(100,500); // generate a "fast" random delay
Serial.print ("Now disrupting in fast mode with delay: ");
Serial.println(randomDelay); // Output delay interval to the user
incomingByte = '0'; // set to '0' so we don't jump back in here later
}
/* toggle the pin according to the randomDelay value determined above */
digitalWrite(togglePin, HIGH);
delay(randomDelay);
digitalWrite(togglePin, LOW);
delay(randomDelay);
} // loop over and over.