Yet another Button question

Hello, I'm new to the adrunio scene. Bought a starter kit and a book and have been learning every day since. Hooked

I've done as much searching as I could for a similar post or a reference to get me in the right direction, the topics I do find get off topic or are slightly different.. really wanted to figure it out on my own but driving myself crazy.

I have a normal closed circuit that I need open for only 50ms when the button is pushed.. the button will be pushed and possibly held for half a second. That's where my problem is coming in.. I dont want to repeat the 50ms while the button is held.. the button will be pushed a few times within a minute and only need 50ms open

Any help or links to where I should look for ideas would be great thanks

Look at the State Change Example. It is about having a variable to keep the previous state so you can do something once only when the state changes.

Thanks I read that a couple times before and it must of been shooting way over my head lol... Took another look and was able to get it working!! Thanks for the info.. What I ended up with incase anybody else is seeking similar answer

const int  buttonPin = 2;    
const int ledPin = 13;       

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      digitalWrite(ledPin, LOW);
    } else {

   if (buttonState != lastButtonState)
  lastButtonState = buttonState;


This line digitalRead(buttonPin);does nothing for you

50ms when you release the button is a pretty long debounce time, you might want to have different delay() within your if/else - 15ms is usually plenty for debounce