Time Dependent Momentary Push Button Action

I am attempting to have a momentary push button cause a desired action (in the case just light an LED) iff it is held for a certain period of time. The code posted below seems to work in a simulator. However, I am having no such luck on my actual board. I realize that this is most likely a very inelegant way of accomplishing my desired goal, but I have very little (read: I’m incompetent) programming experience and this was dropped on me at the 11th hour. Aside from possible troubleshooting, any suggestions on increasing stability or ease of use would also be greatly appreciated. Thank you.

int outPin = 9;
int inPin = 1;
int counter = 0;
int buttonState = 0;
void setup(){
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
void loop()
  buttonState = digitalRead(inPin);          // inital check of button status

 while (buttonState == 1)                    // while loop to take place while button is pressed
      counter += 1;                          // counter to keep track of time.
      delay(10);                             // 10 ms delay to allow for time calculation
      buttonState = digitalRead(inPin);      // check to see if button is still pressed

  if (counter >= 100 && counter <= 300)  // device is only powered if button is pressed for a time between 1 second and 3 seconds
      digitalWrite(outPin,HIGH);        // simulating sending signal to transistor to allow device to be powered
      delay(10000);			// 10 sec delay simulating 120 second run time	
      digitalWrite(outPin,LOW);         // turning signal off
      counter=0;			// reseting counter 	

Link to photograph of wiring.

Your input on Pin 1 will have some problem, because it used as Tx pin for serial. Try to get another pin as input.

What you should do is describe what is going to happen before you start coding. Drawing a state diagram is a good way of doing this(see attachment). Once that is done and verified you can start to code. Again using the state machine concept is one of the best ways to do that.
My state mchine library is found here: http://playground.arduino.cc/Code/SMlib
instruction for installning: http://arduino.cc/en/Guide/Libraries

here is a example:

#include <SM.h>

const int buttonPin = 2;//should not be 1
const int ledPin = 13;//on board led

SM btnCtrl(Waiting);

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

void loop(){

State Waiting(){
  if(digitalRead(buttonPin)) btnCtrl.Set(Pressed);
  digitalWrite(ledPin, 0);

State Pressed(){
  if(btnCtrl.Timeout(300)) btnCtrl.Set(Holding);
  if(!digitalRead(buttonPin)) btnCtrl.Set(Waiting);

State Holding(){
  digitalWrite(ledPin, 1);
  if(!digitalRead(buttonPin)) btnCtrl.Set(Waiting);

hold button state diagram.png

Hi @nilton61 ,

This is very nice work. There are other State Machine libraries but some are not being maintained.

Are you willing to accept questions/suggestions on this? Perhaps you could identify yourself on the Playground page. I only found you through this forum question.

I will be trying some applications of your library to Robotics.


@ terryking228.

Of course I'll accept suggestions and questions. You can Pm or email me