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

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

  pinMode(8, INPUT_PULLUP);
}

int countUp;
# define pin7 8

void loop()
{
  static int lineCounter;

  delay(124); // slow it down... everything else whistles

int statusSensor1 = digitalRead(8);
bool send;

// adjusted code

  if(digitalRead(pin7) == LOW) {
//      display.ssd1306_command(SSD1306_DISPLAYON);
      Serial.print(lineCounter);
      lineCounter++;
      Serial.println("   display   on");
      send = HIGH; 
  }

  if(send == HIGH) {
    countUp++;
  }

  if (countUp >= 10) {  // again life too short
   
//    display.ssd1306_command(SSD1306_DISPLAYOFF);
    Serial.print(lineCounter);
    lineCounter++;
    Serial.println("   display   off");
    send = LOW;
    countUp = 0;
  }
}