The analogWrite is functioning inside the "if" statement BTW. It's just that you are not using it properly.