What is flag doing that is useful? Why do you set the pin LOW in both cases? LOW is generally not how to make a pin HIGH.