Button Counter problem.

I have this problem with setting up a button counter… this is my code so far but it goes into an infinite loop and it never stops adding.
I just want it to add 1 to the value of val = 28 when I press the button and if not I want it to keep on displaying val = 28.

so far this is my code…

#include <SoftwareSerial.h>

#define rxPin 4 // rxPin is immaterial - not used - just make this an unused pin number
#define txPin 3
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int switchPin = 9; // switch is connected to pin 2
int val = 28; // variable for reading the pin status
int buttonState; // variable to hold the button state
int buttonPresses = val; // how many times the button has been pressed

void setup(){
pinMode(txPin, OUTPUT);
mySerial.begin(9600); // 9600 baud is chip comm speed
delay(1000); // pause to allow LCD EEPROM to program
mySerial.print("?Bff"); // set backlight to 40 hex
delay(1000); // pause to allow LCD EEPROM to program
mySerial.print("?s6"); // set tabs to six spaces
delay(1000); // pause to allow LCD EEPROM to program
mySerial.print("?c0"); // turn cursor off
mySerial.print("?f"); // clear the LCD
mySerial.print(" Senior I?n"); //note new line
mySerial.print("?n"); //space between lines
mySerial.print(" Project Grim?n");
delay(3000); // pause three secs to admire
mySerial.print("?f"); // clear the LCD
pinMode(switchPin, INPUT); // Set the switch pin as input

Serial.begin(9600); // Set up serial communication at 9600bps buttonState = digitalRead(switchPin); // read the initial state
buttonState = digitalRead(switchPin); // read the initial state

void loop(){
val = digitalRead(switchPin); // read input value and store it in val

if (val != buttonState) { // the button state has changed!
if (val == HIGH) { // check if the button is pressed
buttonPresses++; // increment the buttonPresses variable
mySerial.print(“Desired pressure: “);
} else {
buttonState = val; // save the new state in our variable
mySerial.print("Desired pressure: ");


The line:- buttonState = val; is only ever executed if buttonState == val; In the case where it is not equal it will never be set and so go in an endless loop. Move that line to the last thing in the loop()

Another example of a similar program is to be found, with explanation, at...