How to Give a pulse for 1 sec after a counter counts button pressed for 2 times

Hi
This is my first project on Arduino ..
What i want is .. if a button is pressed 2 times it should give a pulse for a second and reset by itself ..
i tried the State Change Detection (Edge Detection) for push buttons from the standard library but it does not reset ..
i have to push switch the third time for reset ..or change to 0
tried a small change .. but failed .. it has become like a timer based counter ..

Can someone help please ..
Thanks

The code that i copied ..

/*
State change detection (edge detection)
Often, you don't need to know the state of a digital input all the time, but
you just need to know when the input changes from one state to another.
For example, you want to know when a button goes from OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes from off to on
and on to off.

The circuit:

  • pushbutton attached to pin 2 from +5V
  • 10 kilohm resistor attached to pin 2 from ground
  • LED attached from pin 13 to ground (or use the built-in LED on most
    Arduino boards)

created 27 Sep 2005
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 2 == 0)
{
delay(2000);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
}
}

"i tried the State Change Detection (Edge Detection) for push buttons from the standard library but it does not reset ..
i have to push switch the third time for reset ..or change to 0 ".

Of course you have to reset the condition. How else would you be able to detect the next button push. Only you know when you are through using the detected condition and it may be reset. That's the way it all works. Carry on, you are on the right path.

Paul

You really should avoid using delay().

Here is an example how to do so.

/*
  GetRidOfDelay.ino
  LarryD
  Version YY/MM/DD
  1.00    17/02/01  Running code
 
*/
 
//LED wiring:
// +5V---220 ohm resistor---LED anode---LED cathode---Arduino pin
 
const byte heartBeatLED            = 13;    //used to see if the sketch is blocking
const unsigned long heartBeatDelay = 100UL;
const byte myLED                   = 12;    //wired so a LOW turns the LED on
 
//SRAM variables
unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long myLEDmillis;
unsigned long waitTime = 0;
unsigned long currentMicros;
 
byte LEDstate = 0;
 
//                          s e t u p ( )
//**********************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);
 
  pinMode(myLED, OUTPUT);
  digitalWrite(myLED, HIGH);        //HIGH is LED off
 
} //                 E n d  o f  s e t u p ( )
 
//                          l o o p ( )
//**********************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing
  currentMicros = micros(); //for mirco second timing
 
  //***************************
  //HeartBeat LED, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    heartBeatMillis = heartBeatMillis + heartBeatDelay; //reset timing
 
    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }
 
  //***************************
  //swap the comment marks // on the next two lines to see a comparison
 
  //usingDelay();
  usingMillis();
 
 
} //                    E n d  o f  l o o p ( )
 
//======================================================================
//                        F U N C T I O N S
//======================================================================
 
//                     u s i n g D e l a y ( )
//**********************************************************************
void usingDelay()
{
  digitalWrite(myLED, LOW);  //LOW  is LED on
  delay(2000);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(1000);
  digitalWrite(myLED, LOW);  //LOW  is LED on
  delay(500);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(250);
 
}//                E n d  o f  u s i n g D e l a y ( )
 
//                     u s i n g M i l l i s ( )
//**********************************************************************
void usingMillis()
{
  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }
 
  //Yes, it is now time
  myLEDmillis = currentMillis;     //reset timing
 
  //state code
  switch (LEDstate)
  {
    case 0:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 1;                //next state
      waitTime = 2000UL;           //setup the wait value
      break;
 
    case 1:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 2;                //next state
      waitTime = 1000UL;           //setup the wait value
      break;
 
    case 2:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 3;                //next state      
      waitTime = 500UL;            //setup the wait value
      break;
 
    case 3:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 0;                //next state
      waitTime = 250UL;            //setup the wait value
      break;
 
  } //End of switch/case
 
}//                E n d  o f  u s i n g M i l l i s ( )
 
//======================================================================
//                        E N D  O F  C O D E
//======================================================================

I suspect that in this IF clause

 if (buttonPushCounter % 2 == 0) 
 {
   delay(2000);
   digitalWrite(ledPin, HIGH);
   delay(200);
   digitalWrite(ledPin, LOW);
 }

you should be setting the counter back to 0

If you are only interested in two button presses I don’t see why you would use the modulus (%) operator. Keep it simple and use

if (buttonPushCounter  == 2)

…R
PS please post your programs using the code button </> so it looks like this. See How to use the Forum