user58104: pressing and number in the serial monitor to start code and stop code.  

user58209: This can be done by using the Serial.read()command as referenced in the Arduino documentation here.
Below is an example of how you might apply this.

/**
 * NAME: Serial interupt
 * DATE: 18/7/19
 * AUTHOR: Jeremy Fox
 * DESCRIPTION:
 *    FLashes LED until recives enter keystroke from serial monitor.
 */
int incomingByte = 0; // for incoming serial data
int LED = 13; // defines the LED to flash
boolean flash = true; // for flashing loop

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  while (flash == true){
    // flash LED
    digitalWrite(LED, HIGH);
    delay(100);
    digitalWrite(LED, LOW);
    delay(100);
  }

  // record data if available:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();   
  }

  // checks if enter key was pressed: 
  if (incomingByte == 13) {
    flash = true; // enter loop
    incomingByte = 1; // reset incomingByte to avoid feedback loop
  }
}



58104: <p>pressing and number in the serial monitor to start code and stop code.  </p>
 58209: <p>This can be done by using the <code>Serial.read()</code>command as referenced in the Arduino documentation <a href="https://www.arduino.cc/reference/en/language/functions/communication/serial/read/" rel="nofollow noreferrer">here</a>.</p>

<p>Below is an example of how you might apply this.</p>

<pre><code>
/**
 * NAME: Serial interupt
 * DATE: 18/7/19
 * AUTHOR: Jeremy Fox
 * DESCRIPTION:
 *    FLashes LED until recives enter keystroke from serial monitor.
 */
int incomingByte = 0; // for incoming serial data
int LED = 13; // defines the LED to flash
boolean flash = true; // for flashing loop

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  while (flash == true){
    // flash LED
    digitalWrite(LED, HIGH);
    delay(100);
    digitalWrite(LED, LOW);
    delay(100);
  }

  // record data if available:
  if (Serial.available() &gt; 0) {
    // read the incoming byte:
    incomingByte = Serial.read();   
  }

  // checks if enter key was pressed: 
  if (incomingByte == 13) {
    flash = true; // enter loop
    incomingByte = 1; // reset incomingByte to avoid feedback loop
  }
}

</code></pre>
