Switching issues pb problems

Hi,
I’d really appreciate some help. I have a custom waveform generator. The system should run for a pre determined period once a PB is pushed. the PB creates a high signal the code reads the change state and starts the program. Press it again and it should stop. Issue is sometimes it needs to be pressed twice other times once to start the program there is no consistency the other thing is some times the code doesnt run for the entire predetermined time as in it cuts short. Can anyone offer some advice please. Not from a coding background so the more info the better. Board is a mega. Switch is wired to provide 5V to analog read pin once pressed.
Thanks
Peter

1-1_PW4_INT_16_65_F30.ino (2.37 KB)

What is a “PB”?

Can you post diagram of connections?

Also, post the code in the body of your post using code tags, not as attached.ino file - that makes it easier for people to help you, esp since many of us answer questions from our mobile devices, where a .ino file is hard to work with

Do you have a 10k pulldown resistor from buttonPin to GND?

Hi jca79b I'm using a 1kohm resistor.

DrAzzy pb is a push button sorry. Standard config with a 1k resistor to gnd providing 5v o/p when pressed I'll upload code to theard once I get back to laptop.