Instead of analogWrite(analogOutPin, outputvalue); you could put something like...
if(suddenRiseConditionIsMet(inputvalue)) digitalWrite(analogOutPin); else analogWrite(analogOutPin, outputvalue);
The tricky part is that you don't control the cycle of the PWM. So even if you precisely timed the 20ms, you might end that period during the PWM on-time, so the pin is held on 'too long'. If that is important to you, then you need to dig deeper into the registers and reset timer 2 when the 20ms is over.
The function which does all the work would look something like...
bool suddenRiseConditionIsMet(int input) {
const int lowThreshold = 20; //pick your own numbers here
const int highThreshold = 500;
const int riseTime = 10; //milliseconds, rises shorter than this will be considered to be 'sudden'
const int holdTime = 20; //milliseconds, time to hold the output on when the input did rise suddenly
static unsigned long lastTimeBelowLow;
static unsigned long conditionMetTime;
static bool conditionWasMet = false;
if(conditionWasMet) {
if(millis() - conditionMetTime > holdTime) {
//period has expired - return false
conditionWasMet = false;
return false;
} else {
//we're inside the 20ms timing period - return true
return true;
}
}
//we can only get here if we are waiting for a sudden rise
if(input < lowThreshold) {
lastTimeBelowLow = millis();
return false;
}
if(input > highThreshold && millis()-lastTimeBelowLow <= riseTime) {
conditionWasMet = true;
conditionMetTime = millis();
return true;
}
//we get here when we're above the low threshold but below high OR the rise wasn't sudden
return false;
}