A Demo-Code explaining the switch-case state-machine and how to do things (almost) in parallel

Yes this is true.
usually I use 115200 bauds like in the demo-code and then printing 50 characters is done in

50 characters * 10 bits / 115200 bits per second = 4,3 milliseconds
fast enough for a demo-code that is only printing to the serial monitor.
For real applications there would be no need for "cleaning" the serial monitor.

best regards Stefan

When the TX buffer is full, the code is blocking.

100 characters as println sends CR and LF (If I remember right)

Another Demo-Code. This function is the functionality:

code waits for HIGH on the PIR-Pin.
If high s detected switch on onboard LED and start a timer
check if 5 seconds have passed by since PIR-HIGH was detected
if 5 seconds have passed by switch LED off
wait for PIR_signal to go LOW
if PIR-LOW is detectd wait anothner second until code goes into state wait for detection again

The code

const byte IR_Pin =  2; //Pin2 as IR sense  pin.
const byte LEDpin = 13; //Pin13 as Output pin.
const byte heartBeatLED_Pin = 12;

enum myStates {
  waiting_for_detection,
  detected_led_on,
  detected_led_off,
  no_detection_wait
} myStateVar;

unsigned long myTimer = 0;       // Timer-variables MUST be of type unsigned long
const unsigned long LED_OnTime = 5000;
const unsigned long noObstacleWaitTime = 1000;

void setup() {
  Serial.begin(115200);
  Serial.println( F("Setup-Start") );
  PrintFileNameDateTime();

  pinMode(IR_Pin, INPUT);
  pinMode(LEDpin, OUTPUT);
  pinMode(heartBeatLED_Pin, OUTPUT);

  myStateVar = waiting_for_detection;
}


void loop() {
  BlinkHeartBeatLED(heartBeatLED_Pin, 250);

  myMonoStableStateMachine();

}


void myMonoStableStateMachine() {

  switch (myStateVar) {

    case waiting_for_detection:
      if (digitalRead(IR_Pin) == HIGH ) {
        Serial.println( F("object detected switching on LED") );
        myTimer = millis(); // store snapshot of time
        digitalWrite(LEDpin, HIGH);
        myStateVar = detected_led_on;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case detected_led_on:
      Print_a_dot(500);
      if ( TimePeriodIsOver(myTimer, LED_OnTime) ) {
        Serial.println( F("LED ON-time over switching LED off") );
        digitalWrite(LEDpin, LOW);
        myStateVar = detected_led_off;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case detected_led_off:
      Print_a_dot(500);
      if (digitalRead(IR_Pin) == LOW ) {
        Serial.println( F("No object start ignore-waiting") );
        myTimer = millis();
        myStateVar = no_detection_wait;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH


    case no_detection_wait:
      Print_a_dot(500);
      if ( TimePeriodIsOver(myTimer, noObstacleWaitTime) ) {
        Serial.println( F("ignore-waiting is over start wait for detection") );
        Serial.println();
        Serial.println();
        myStateVar = waiting_for_detection;
      }
      break; // IMMIDIATELY jump down to END-OF-SWITCH

  } // END-OF-SWITCH
}

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);

  if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
    digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
  }
}

void Print_a_dot(unsigned long interval) {
  static unsigned long myWaitTimer;

  if ( TimePeriodIsOver (myWaitTimer,interval) ) {
    Serial.print(".");
  }
}

The WOKSim

best regards Stefan