counting button presses

hey guys,
I am new to the arduino and need some advice on counting button presses. My main goal was to blink an led 5 times when a button is pressed once, 10 times if pressed 2 times, and 15 times if pressed yet a 3rd time. My problem is that the arduino, well, its like its reading the code all out of order.

so for example;
1st press it says its been pressed 1 time
2nd press, it still says 1 time
3rd press it says its been pressed 2 times
and so on.

here is my code
I have taken out the LED program and used the println funtion to simplify it (and I have tested this code and have gotten the same result as I did with the code for the LED).

If anyone can give a tip or some advice as to how or what I am doing wrong that would be awsome:)

int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the button state
int buttonPresses = 0;          // how many times the button has been pressed
   
    void setup() {
      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
      
    }


    void loop(){
      val = digitalRead(switchPin);      
      
      if (val != buttonState) {         
        if (val == LOW) {               
          buttonPresses++;               
          Serial.println("1st button press");
          //this is were the code for input/output code for the pins would go(like turning on an led etc).
        }
      }
      buttonState = val;                
    
    
    val = digitalRead(switchPin);     

    if (val==HIGH && buttonState==LOW); 
      if (val != buttonState) {         
        if (val == LOW) {               
          buttonPresses++;               
          Serial.println("2nd button press");
          //this is were the code for input/output code for the pins would go(like turning on an led etc).
        }
      }
      buttonState = val;                 // save the new state in our variable
    
    
     if (val==HIGH && buttonState==LOW);
       if (val != buttonState) {
         if (val == LOW) {
           buttonPresses++;
           Serial.println("3rd button press");
           //this is were the code for input/output code for the pins would go(like turning on an led etc).
         }
         
       }
       buttonState = val;
       
    }

Thanks

It looks like you are expecting the code to halt after each section of code when a signal edge is detected. You are keeping track of the number of button presses with a variable, why not use that to determine how many presses?

      val = digitalRead(switchPin);      
      
      if (val != buttonState) {

If you used names like currentState and previousState, some order would be implied, and your code would be much easier to understand.

It is easy to infer a relationship between currentState and previousState; it is impossible to infer a relationship between val and buttonState.

    if (val==HIGH && buttonState==LOW);

Ending an if statement with a semicolon is rarely a good thing to do.

And, of course, I agree with what Arrch said.

I probably shouldn’t do this. I got bored at work.

// Counts number of button presses
// output count to serial
// blink a led according to count

byte switchPin = 2;                    // switch is connected to pin 2
byte ledPin = 13;                      // led on pin 13
byte buttonPresses = 0;                // how many times the button has been pressed 
byte lastPressCount = 0;               // to keep track of last press count

void setup() {
  pinMode(switchPin, INPUT);          // Set the switch pin as input
  digitalWrite(switchPin, HIGH);      // set pullup resistor
  Serial.begin(9600);                 // Set up serial communication at 9600bps
}

void loop(){
  if (digitalRead(switchPin) == LOW)  // check if button was pressed
  {
    buttonPresses++;                  // increment buttonPresses count
    delay(250);                       // debounce switch
  }
  if (buttonPresses == 4) buttonPresses = 0;         // rollover every fourth press
  if (lastPressCount != buttonPresses)              // only do output if the count has changed
  {
    Serial.print ("Button press count = ");          // out to serial
    Serial.println(buttonPresses, DEC);
    for (byte n = 0; n <= 5 * buttonPresses; n++)    // lets blink
    {
      digitalWrite(ledPin, HIGH);      // turn on led
      delay(500);                      // wait half a second
      digitalWrite(ledPin, LOW);       // turn off led
      delay(500);                      // wait again
    }
    lastPressCount = buttonPresses;    // track last press count
  }
}

This compiles fine but I don’t have a board to run it on here.

Oh ok thanks, I see what you mean. Thanks for the advice guys:)

what is the circuit???????