programming error code to play WAV issue

I cannot seem to get this to work, when the button is pressed a number of times I need it to stop the loop while it plays then continue it. I have it working 80% but am completely stumped, anyone have any input? I'm utilizing bare conductive paint as a subsitute for the button, so once the user presses the paint I wanted it to play the MP# through the SD card reader. My code keeps giving me errors

ERROR: expected '}' before 'else'

// Including header files here -----------------------
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
// ----------------------------------------------------

TMRpcm tmrpcm;

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton 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 Speaker pin @ pin 9
  tmrpcm.speakerPin = 9;
// initialize serial communication:
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {
  Serial.println("SD fail");
  return;
  }
}

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 wend 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;

 if (buttonPushCounter % 4 == 0) 
 {
    digitalWrite(ledPin, HIGH);
    tmrpcm.setVolume(5);
    tmrpcm.play("3.wav");
    } else {
    digitalWrite(ledPin, LOW);
  }

My code keeps giving me errors

No, the compiler is telling you about the errors - everything about them, including the line number that the error occurred on.

    {
      else

Sure looks to me like that curly brace needs to do an about face.

The issue is when it hits 4, I just hear it start up and theres static, the goal is to have the wav play in this loop and when its done to start going again if the user hits the pad once more? This is where I'm completely stuck. When 4 is reached the loop stops and you can hear the wav start up but its just a long moan sound

maybe this section needs some better code?

if (buttonPushCounter == 4){
    tmrpcm.setVolume(5);
    tmrpcm.play("3.wav");
    } 
    
    else {
    Serial.println("");
  }
}

This is the entire project:

// Including header files here -----------------------

#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"
// ----------------------------------------------------
TMRpcm tmrpcm;

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton 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 Speaker pin @ pin 9
  tmrpcm.speakerPin = 9;
// initialize serial communication:
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {
  Serial.println("SD fail");
  return;
}
}

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
      // wend 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;

if (buttonPushCounter == 4){
    tmrpcm.setVolume(5);
    tmrpcm.play("3.wav");
    } 
    
    else {
    Serial.println("");
  }
}

Before trying to get fancy as to when the track is played, just play the track once in setup().

If you get nothing but static and noise, then the problem isn't with the counting of the state changes.

Move the code then to loop(), with a delay() after it, and see if the problem recurs. If not, then add code back to read the state of the switch, and increment the counter. You'll very quickly determine what code being added, IF it is a code problem, causes the noise and static issue.

It IS possible that the use of the external interrupt pin for the switch is interfering with the library. Try a different pin.

it plays without the loop fine, its when I want to press down and want it to play thats when the static occurs and I'm completely at a blank. Maybe is because the loop is still going and the song cant play because its stuck in that loop?

If there is no static in setup(), move the code to loop(), in a while loop that iterates until the instance's methods to determine when the track is done playing say it is done. Then, delay() for a short while, and let loop() run again, and play the track again.

If there is still no static, then the problem is likely that you have a switch connected to a pin that the library uses.

Hello mate

I am not sure but try this

  1. Put the set volume on the setup section like this
void setup(){
pinMode (Speaker , OUTPUT);
tmrpcmspeakerPin = 9; //or on what pin you put the speaker
if(!SD.begin(SD_ChipSelectPin)){
  Serial.println("SD fail");
  return;
} 
tmrpcm.setVolume(5);
}
  1. After that on your loop, where you wanted to play the sound. Put speaker oh high first and then you plat the sound like this
void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(Speaker, HIGH);
  tmrpcm.play("23.wav");
  delay(3000);
   }
   else{
    digitalWrite(Speaker, LOW);
    delay(100);
   }
}

then you plat the sound like this

No you don't. You can't have an else without an if statement.

yeah that's why I left a space for the if statement on the beginning of the loop so he can put whatever he wants to do with the if statement.