Hello can anyone help me? This will work ???

#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 4 //using digital pin 4 on arduino nano 328
#include <TMRpcm.h> // also need to include this library...

TMRpcm tmrpcm; // create an object for use in this sketch
int SWA;
int SWB;
int SWG;
int SWD;
int SWE;
int SWV;
int SWZ;
int SWT;
int SWI;
int SWK;
int SWL;
int SWM;
int SWN;
int SWO;
int SWP;
int SWJH;
int SWR;
int SWS;
int SWTH;
int SWU;
int SWF;
int SWQ;
int SWGH;
int SWY;
int SWSH;
int SWCH;
int SWC;
int SWDZ;
int SWWH;
int SWLH;
int SWX;
int SWJ;
int SWH;

void setup(){
pinMode(14,INPUT); //Define A0 as digital input.
pinMode(15,INPUT); //Define A1 as digital input.
pinMode(16,INPUT); //Define A2 as digital input.
pinMode(17,INPUT); //Define A3 as digital input.
pinMode(18,INPUT); //Define A0 as digital input.
pinMode(19,INPUT); //Define A1 as digital input.
pinMode(20,INPUT); //Define A2 as digital input.
pinMode(21,INPUT); //Define A3 as digital input.
pinMode(22,INPUT); //Define A1 as digital input.
pinMode(24,INPUT); //Define A2 as digital input.
pinMode(26,INPUT); //Define A3 as digital input.
pinMode(28,INPUT); //Define A1 as digital input.
pinMode(30,INPUT); //Define A2 as digital input.
pinMode(31,INPUT); //Define A3 as digital input.
pinMode(32,INPUT); //Define A1 as digital input.
pinMode(33,INPUT); //Define A2 as digital input.
pinMode(34,INPUT); //Define A3 as digital input.
pinMode(35,INPUT); //Define A1 as digital input.
pinMode(36,INPUT); //Define A2 as digital input.
pinMode(37,INPUT); //Define A3 as digital input.
pinMode(38,INPUT); //Define A1 as digital input.
pinMode(39,INPUT); //Define A2 as digital input.
pinMode(40,INPUT); //Define A3 as digital input.
pinMode(41,INPUT); //Define A1 as digital input.
pinMode(43,INPUT); //Define A2 as digital input.
pinMode(44,INPUT); //Define A3 as digital input.
pinMode(47,INPUT); //Define A1 as digital input.
pinMode(48,INPUT); //Define A2 as digital input.
pinMode(49,INPUT); //Define A3 as digital input.
pinMode(50,INPUT); //Define A1 as digital input.
pinMode(51,INPUT); //Define A2 as digital input.
pinMode(52,INPUT); //Define A3 as digital input.
pinMode(53,INPUT); //Define A0 as digital input.

tmrpcm.speakerPin = 11; //11 on Mega, 9 on Uno, Nano, etc

if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
return; // don't do anything more if not
}
tmrpcm.volume(1);
tmrpcm.play("START.wav"); //the sound file "1" will play each time the arduino powers up, or is reset
}

void loop(){
SWA=digitalRead(14);
SWB=digitalRead(15);
SWG=digitalRead(16);
SWD=digitalRead(17);
SWE=digitalRead(18);
SWV=digitalRead(19);
SWZ=digitalRead(20);
SWT=digitalRead(21);
SWI=digitalRead(22);
SWK=digitalRead(24);
SWL=digitalRead(26);
SWM=digitalRead(28);
SWN=digitalRead(30);
SWO=digitalRead(31);
SWP=digitalRead(32);
SWJH=digitalRead(33);
SWR=digitalRead(34);
SWS=digitalRead(35);
SWTH=digitalRead(36);
SWU=digitalRead(37);
SWF=digitalRead(38);
SWQ=digitalRead(39);
SWGH=digitalRead(40);
SWY=digitalRead(41);
SWSH=digitalRead(43);
SWCH=digitalRead(44);
SWC=digitalRead(47);
SWDZ=digitalRead(48);
SWWH=digitalRead(49);
SWLH=digitalRead(50);
SWX=digitalRead(51);
SWJ=digitalRead(52);
SWH=digitalRead(53);

if (SWA == HIGH) { //if SW1 pressed then play file "6.wav"
tmrpcm.play("A.wav");
} else if(SWB == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("B.wav");
} else if(SWG == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("G.wav");
} else if(SWD == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("D.wav");
} else if(SWE == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("E.wav");
} else if(SWV == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("V.wav");
} else if(SWZ == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("Z.wav");
} else if(SWT == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("T.wav");
} else if(SWI == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("I.wav");
} else if(SWK == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("K.wav");
} else if(SWL == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("L.wav");
} else if(SWM == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("M.wav");
} else if(SWN == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("N.wav");
} else if(SWO == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("O.wav");
} else if(SWP == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("P.wav");
} else if(SWJH == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("JH.wav");
} else if(SWR == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("R.wav");
} else if(SWS == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("S.wav");
} else if(SWTH == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("TH.wav");
} else if(SWU == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("U.wav");
} else if(SWF == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("F.wav");
} else if(SWQ == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("Q.wav");
} else if(SWGH == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("GH.wav");
} else if(SWY == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("Y.wav");
} else if(SWSH == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("SH.wav");
} else if(SWCH == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("CH.wav");
} else if(SWC == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("C.wav");
} else if(SWDZ == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("DZ.wav");
} else if(SWWH == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("WH.wav");
} else if(SWLH == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("LH.wav");
} else if(SWX == HIGH){ //if SW4 pressed then play file "3.wav"
tmrpcm.play("X.wav");
} else if(SWJ == HIGH){ //if SW2 pressed then play file "4.wav"
tmrpcm.play("J.wav");
} else if(SWH == HIGH){ //if SW3 pressed then play file "5.wav"
tmrpcm.play("H.wav");
} else if(SWD == HIGH){
}
}

I can save you a lot of typing --
all 'pins' are INPUTs by default

Those buttons all need a pulldown resistor (assuming they connect to Vcc when pressed). That's a bunch of resistors you need to wire up.

If you wire them the other way, so that they connect to ground when pushed, set the pins to INPUT_PULLUP instead of INPUT, and look for a pin that reads LOW instead of HIGH, you don't need the external pulldowns (this is generally the "go to" way of wiring buttons).

There are also more graceful ways of doing this with a few arrays, but there probably isn't a need for it in your case.

You have copy-paste errors in your comments - it's bad practice to have comments that don't match the code.

And each int variable name doesn't have to be preceded by typing 'int'.
Just use a comma --
i.e.
int SWA, SWB, SWG, SWD;

The comments are useless so either correct them or delete them.

Upload it. Run it. Then you can tell us if it works.

Steve

Did you try it? Did it work as you expected? If not, how did it behave vs. how you expected it to behave?
You also might want to seriously look into using arrays and for loops.

static int pins[] = { 14, 15, 16, 17, 18, 19, 21, 21, 22, 24, 26,... };
static int pinCount = sizeof(pins) / sizeof(pins[0]);
bool switches[pinCount];
....
void loop() {
  for ( i = 0; i < pinCount; ++i ) {
    switches[i] = digitalRead(pins[i]);
  }

  // now use the switches array to figure out what to play

also - don't cut and paste code and leave the comments the same (A0, A1, A2, A3, A0, ...) A wrong comment is worse than no comment at all

Also, pins are INPUTs by default you you don't need to declare them as such in setup()

runaway_pancake:
And each int variable name doesn't have to be preceded by typing 'int'.
Just use a comma --
i.e.
int SWA, SWB, SWG, SWD;

Nor do they need to be 'int' just to hold a 1-bit result. 'uint8_t' would work nicely.

I do not know whether this code will work or not?
I'm doing it for the first time. I saw the code on the Internet and add buttons.
I need 33 buttons. Comments in the code are old code. I do not have a resistor
. I need help

Thank you to everyone for helping me :slight_smile: :slight_smile: :slight_smile:

gotik123:
I do not know whether this code will work or not?
I'm doing it for the first time. I saw the code on the Internet and add buttons.
I need 33 buttons. Comments in the code are old code. I do not have a resistor
. I need help

If you do not have external pulldown resistors on the switch pins, the code you have will not work, because when the switch is not pressed, the pins will be floating (connected to nothing) and will read a random value as they pick up ambient electrical noise. You need to keep these pins at a defined level, this is done with a resistor (values of 10k to 100k are commonly used).
If you have the switches connect the pin to Vcc, you need to keep the pin at ground (LOW) when not pressed, so you'd need an external pulldown resistor (between switch pin and ground).

If you have the switches connect the pin to Gnd when pushed, you need the pins pulled up to Vcc - that could be done with an external resistor, but you can use the chip's internal pullups (INPUT_PULLUP - there isn't an internal pulldown, at least not on the AVR microcontrollers. Some other architectures have both) this is the recommended way to do it.

But, though it looks slightly ugly, but I see no reason it wouldn't work.

maybe resistor R1 1W 0 R 22 will be worked ? on all gnd wire ??

With this --

for (byte index = 14; index < 54; index ++)
{
  pinMode (index, INPUT_PULLUP);
}

no resistors are necessary, provided the pushbuttons/switches are connected with one lead connected to an input pin and the other to GND.

"Nor do they need to be 'int' just to hold a 1-bit result. 'uint8_t' would work nicely."
Or byte.

runaway_pancake:
.

"Nor do they need to be 'int' just to hold a 1-bit result. 'uint8_t' would work nicely."
Or byte.

byte = int8_t an 8 bit integer with a sign, uint8_t is an 8 bit value without a sign,
for digital pins the result is actually bool. If you want to correct you should try to be as correct as possible.

Deva_Rishi:
byte = int8_t an 8 bit integer with a sign

Have another guess.

sorry a typo i mean 7 bit integer + sign(or an 8 bit integer including the sign) i am not guessing !

Deva_Rishi:
sorry a typo i mean 7 bit integer + sign(or an 8 bit integer including the sign) i am not guessing !

I'm feeling uncharacteristically generous - have another guess.

If in doubt, there's always the documentation

am i getting my languages confused, this is why i always use the _t versions, there is just no doubt.

Deva_Rishi:
byte = int8_t an 8 bit integer with a sign, uint8_t is an 8 bit value without a sign, for digital pins the result is actually bool. If you want to correct you should try to be as correct as possible.

Oh? Well, thanks, but I wasn't correcting anyone/anything, merely suggesting.

Anyway - according to the Arduino Reference:
"A byte stores an 8-bit unsigned number, from 0 to 255"

runaway_pancake:
Oh? Well, thanks, but I wasn't correcting anyone/anything, merely suggesting.

Anyway - according to the Arduino Reference:
"A byte stores an 8-bit unsigned number, from 0 to 255"
byte - Arduino Reference

Yeah Sorry i got my languages confused...