Pushbutton in my testproject sometimes doesn't register

Dear reader,

I'm currently working on a schoolproject with a punching bag.
For the punching bag I need to test some stuff (which i haven't got home yet, but that's not the point)

I'm making a little sketch to simulate the punching on the bag which i used a button for, a press on the button indicates that the bag has been punched. Also I'm using a potentiometer and map the value to simulate the speed of a punch.

But i have a problem with the button, sometimes when i press the button it just doesn't want to register.
I'll leave my schematic and my code as an attachment, but I don't think thats the problem.
I used some comments to illustrate my thinking progress, hope it contains readable english.

I find it quite weird and my mates don't see the problem either, but it sometimes just skips the reading of the button as if it's going to fast (no I'm not spamming the button like a madman xD)

Hope you guys can help me with the code of the test.

I just saw there are 2 comments which contain dutch words, but those aren't important so you can ignore those saying print("klaar") and print("niet klaar"). :slight_smile:


Punchingbag Test Code.txt (2.02 KB)

Your button switch code is too primitive. It has no debouncing and also does not perform state change detection. Look at the switch examples that ship with the IDE to see how it's done.