how can i improve my code?

You should look into interrupts. I don't think you need to debounce anything if it just sends out a pulse, but if you want efficiency then interrupts are the way to go.