HC-SR04 quits pinging when distance < 0 || distance < MAXDISTANCE
The HC-SR04's range is 2cm to 300 cm, if you exceed this range or come below this range, which is exactly what you are doing, the utrasonic sensor will not receive any echo and hence there will be no data on the output. 
If you bring it too close and since both the transmitter and receiver located close to each other, the receiver will not receive any ultrasonic. 
Put a function that checks if the ultrasonic comes down to 2-3. If it becomes true beep a buzzer that tells you that you are too close. Don't let it reach 0.