distance sensor and timing

DMAG:

while(distance<20){

timeElapsed=0;
}

The value of distance is never changed in that while loop and so the Arduino will be stuck in it forever. You need to change your program to a non-blocking system so that the ping code can continue to run constantly.

Please use Tools > Auto Format before posting code to the forum. This will make it easier for us to read and you will find the automatic indentation is helpful for spotting bugs in code.