Toggle state of LED with button problem.

I am trying to toggle the state of a LED with a button, but I had some slight issues.

The toggling worked. but if I held down the button. it kept on toggling.
I want the led to toggle ONLY if the button released and pressed again.

Does anyone know how to achieve this?

btw, me and my friend made a contraption this evening that fires an air rifle when you walk past it :smiley: (Totally off-topic.)

#include <Button.h>//
#include <LED.h>//

Button button = Button(12,PULLUP);
LED led = LED(13);

void setup(){/nothing to setup/}

void loop(){
if(button.uniquePress()){ //button is pressed, need to release it for this to evaluate true again


Save the state of the button in a variable. Then only when the current button state is different from the remembered state increment another variable. Then flash the LED depending on if that variable is odd or even.

To find if a variable is odd look at the least significant bit with:-
if((var & 0x01) !=0) { // it’s odd }
{ // it’s even }

Haha, thanks :wink:
I’m not very much of a library fan, but I’ll see how it works.