Playing audio.wav by pushing a micro switch

Hi Guys,
I would like to play a sound from my SD card by pushing a microswith.
But nothing happen.
Could you pleas help. THANKS.
Code below:


#include "SD.h"
#define SD_ChipSelectPin 53
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;
int s4=A15;
void setup()
{
tmrpcm.speakerPin=46;
pinMode(s4,INPUT);

void loop() {
  Serial.begin(9600); 
  SD.begin(SD_ChipSelectPin);
 if (digitalRead(s4==1)){


tmrpcm.setVolume(5);
tmrpcm.play("test.wav");
}

           }

 if (digitalRead(s4==1)){

Did you mean to write

 if (digitalRead(s4) == 1){

Hi UKHeliBob
Thanks you are right i didn't pay attention
I correct that but still not working.
strange..

Post the corrected code in a new reply and either describe in great detail how the project is wired or preferably post a schematic. A picture of a hand drawn circuit is good enough

Which Arduino board are you using and how is it powered ?

Arduino Mega
By the way, Audio is working automatically by itself once uploading is DONE
it's powered right now with USB cable.

I am very suspicious of the connection between the SD card reader and the Arduino Vin pin. What is the voltage at that pin ?

Measured Voltage = 4.8V

We need to see your sketch

indeed

#include "SD.h"
#define SD_ChipSelectPin 53
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;
int s4=A15;
void setup()
{
tmrpcm.speakerPin=46;
pinMode(s4,INPUT);


void loop() {
Serial.begin(9600);   
 
 if (digitalRead(s4)==1){
SD.begin(SD_ChipSelectPin);
Serial.begin(9600);
tmrpcm.setVolume(5);
tmrpcm.play("test.wav");
}

           }

What is keeping pin A5 LOW when the switch is not closed ?

You should be using INPUT_PULLUP. Without it, the input pin will be floating and might not change when the microswitch is pressed.

Once you have corrected this line as suggested by @UKHeliBob , and you have changed to INPUT_PULLUP, the pin will read LOW when the microswitch is pressed and HIGH when not pressed. The digitalRead() function returns HIGH or LOW, it is better to use those, than to use 1 and 0.

1 Like

it's the microswitch when not pushing it it will return to 0 position by itself

Thank you everyone
need to use INPUT_PULLUP and HIGH

You have a missing } at the end of the setup() function.

If you use Tools-->Auto Format to correct the indentation of your code, this will make it more readable for yourself and others on the forum. It will also help you know if you have missing { or }. If the last } in the code is indented, or the code ends with two or more } that are not indented, then you know you have at least one } or { missing from somewhere in your code.

Thank you so much.
The only thing now is that the sketch is working the first time but then when i push the sensor again nothing is working.

Once again you have changed the sketch, so please post the revised version and a revised schematic

#include "SD.h"
#define SD_ChipSelectPin 53
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;
int s4 = A15;

void setup()
{
  tmrpcm.speakerPin = 46;
  pinMode(s4, INPUT_PULLUP);
}

void loop() {
  Serial.begin(9600);

  if (digitalRead(s4) == HIGH) {
    SD.begin(SD_ChipSelectPin);
    tmrpcm.setVolume(5);
    tmrpcm.play("test.wav");
  }

}
  if (digitalRead(s4) == HIGH) {

Because of the use of INPUT_PULLUP the pin will be HIGH until the switch is closed to take it LOW so this test makes no sense