Very interesting. In your main code you have an IF statement to detect PORTB.7 =1 and another IF statement to detect PORTB.7=0.

or you could just call the subroutine from the main program as it does both checks.