You don't want to delay ever. Instead you want to change your thinking to "X miliseconds have passed, time to print and reset":
int maxval = 0;
uint32_t lastSample = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
if (millis() - lastSample > 100) { // Every 100ms:
lastSample = millis();
Serial.println(maxVal);
maxVal = 0;
}
int reading = analogRead(0);
if (reading > maxVal) {
maxVal = reading;
}
}
Every 100ms it will print the maximum analog value read during the past 100ms.
You can replace the 100ms with any other value you choose, or even with a check to see if a button has been pressed, in which case you get the maximum reading since the last time the button was pressed:
// global
bool lastButtonValue = HIGH;
// in loop()
if (digitalRead(3) != lastButtonValue) {
lastButtonValue = digitalRead(3);
if (lastButtonValue == LOW) {
// ... do the print and reset
}
}