// https://forum.arduino.cc/t/how-to-shorten-this-code/1035307
// https://forum.arduino.cc/t/how-to-shorten-this-code/1035307/9 gcjr minimal

# define pin7 8

void setup()
{
  Serial.begin(115200);
  Serial.println("\nhello world.\n");

  pinMode(pin7, INPUT_PULLUP);
}

unsigned long lineCounter;

void loop()
{
  static unsigned long lastLook;
  unsigned long now = millis();
  if (now - lastLook < 50)
    return;

  lastLook = now;

  func();
}

# define MAX_COUNT 50  // 2.5 seconds

unsigned long countUp;
bool send;

void
func  (void)
{
  if (LOW == digitalRead (pin7)) {
    if (false == send)  {
      //            display.ssd1306_command (SSD1306_DISPLAYON);
      Serial.print(lineCounter); lineCounter++;
      Serial.println("  turn  ON");
      
      send = true;
    }
    countUp = 0;
  }
  else {
    if (MAX_COUNT <= ++countUp)  {
      //            display.ssd1306_command (SSD1306_DISPLAYOFF);
      if (send == true) {
        Serial.print(lineCounter); lineCounter++;
      	Serial.println("  turn           OFF");
      }

      countUp = 0;
      send    = false;
    }
  }
}