Counter with two switch

Hi guys,I want to make a counter with 2 switches.any of the switch is pressed then count value is increased by 1.
Example if count is 0 and if switch 1 is pressed then count becomes 1 after if switch 2 is pressed then count becomes 2 and again switch 2 is pressed then count becomes 3 etc
please help me in programming.

#include <LiquidCrystal.h>
LiquidCrystal lcd(14,13,12,11,6,4);
int pbutton1 = 7;
int pbutton2 = 6;
int count =0;
void setup() {
// put your setup code here, to run once:
lcd.print(counter 1:);
lcd.print(counter 2:);


void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(pbutton1)== HIGH)
while(digitalRead(pbutton1) ==HIGH);
if (digitalRead(pbutton2)== HIGH);
while(digitalRead(pbutton2) ==HIGH);

The following code illustrates the use of transition detection as used by the state change detection tutorial modified to detect a high to low state change. The code is modified, from the example, to use active low switches and internal pullups. Wire the switches from ground to a digital input with an optional 0.1uf debounce capacitor across the switch.

const byte buttonPin[] = {3, 4, 5};
byte numberOfButtons = 0;

int buttonPushCounter = 0;   // counter for the number of button presses

void setup()
   numberOfButtons = sizeof(buttonPin) / sizeof(buttonPin[0]);
   for (byte n = 0; n < numberOfButtons; n++)
      pinMode(buttonPin[n], INPUT_PULLUP);  
   // initialize serial communication:

void loop()

void checkButtons()
   int buttonState = 0;         // current state of a button
   static int lastButtonState[3] = {0, 0, 0};   // previous state of each button
   for (byte n = 0; n < numberOfButtons; n++)
      buttonState = digitalRead(buttonPin[n]);  
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState[n])
         if (buttonState == LOW)
            // if the current state is LOW then the button
            // went from off to on (active low button).
            buttonPushCounter++;  // add one to counter
      // save the current state of this button as the last state,
      //for next time through the loop
      lastButtonState[n] = buttonState;

What's wrong with the code you have?

I'd change this:


To this:


So that the button press is immediately reflected on the LCD.

A bit of debounce wouldn't hurt and your design means that if one button is pressed, the other is locked out. However, it looks like it more or less works.

when one button is pushed it is detecting and increasing counter by 1 also when 2 buttons pressed alternately counter is increased but when 1 button is pressed twice it is not working

I posted the code in reply #1 before the OP edited his original post to add his code.