Newb help, please

This is my first post, so please bear with me.

I'm using an Uno and trying to get it to play one of ten wav files contained in one of four folders based upon the number of presses of a momentary button. I researched and found what I thought was the right combination of code, but things just aren't working.

Files are in folders on a wav player board, connected to the Uno. The audio output is connected to a small amp. Small momentary button connected to the Uno.

Any help? I'm happy to add any additional info needed.

Thanks!


#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //connect pin 10 of arduino to cs pin of sd card
#include <TMRpcm.h> //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h> //  need to include the SPI library
long randNumber;
byte switchPin = 2;                    // switch is connected to pin 2
byte buttonPresses = 0;                // how many times the button has been pressed 
byte lastPressCount = 0;               // to keep track of last press count
TMRpcm tmrpcm; 
String fold;
void setup()
{ 

// pinMode(pp,INPUT_PULLUP);
// pinMode(next,INPUT_PULLUP);
// pinMode(prev,INPUT_PULLUP);
  pinMode(switchPin, INPUT);          // Set the switch pin as input
 digitalWrite(switchPin, HIGH);      // set pullup resistor
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) // returns 1 if the card is present
{
 Serial.println("SD fail");
 return;
}

 tmrpcm.setVolume(7); 
  tmrpcm.play("startup.wav"); //the sound file "startup" will play each time the arduino powers up, or is reset
}

void loop(){
if (digitalRead(switchPin) == LOW)  // check if button was pressed
 {
   buttonPresses++;                  // increment buttonPresses count
   delay(250);                       // debounce switch
 }
    if (buttonPresses == 5) buttonPresses = 0;    // rollover every fifth press
 if (lastPressCount != buttonPresses)              // only do output if the count has changed
 {
   Serial.print ("Button press count = ");          // out to serial
   Serial.println(buttonPresses, DEC);
  
  
 }
 if (buttonPresses == 1) 
 {
   fold = "Short";
 }
 if (buttonPresses == 2) 
 {
   fold = "Medium";
 }
 if (buttonPresses == 3) 
 {
   fold = "Long";
 }
 if (buttonPresses == 4)
 {
   fold = "Wet";
 }
randNumber = random(1, 10);
    String myString1  = "/";
   String myString2  = ".WAV";
   String myFile = myString1 + fold + myString1 + randNumber + myString2;
   Serial.println(myFile);
 tmrpcm.play(myFile.c_str());
lastPressCount = 0;    // track last press count
 }

Describe these. Show a drawing or schematic of your setup. Thank you.

one button, that's all..
you're either scrooging or poor like me..
don't even have a button, just 2 bare wires..
i rub em together real fast and make electricity.. :slight_smile:
but makes it a bit tricky..
try this for the button counting..

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

unsigned long lastPress = 0;
int intervalDebounce = 50;
byte lastState = 1;// pulled up
bool timerStarted = false;
unsigned long timerStart = 0;
int intervalTimer = 5000;//5 seconds

void setup()
{
  pinMode(switchPin, INPUT_PULLUP);// Set the switch pin as input with pull up resistor
  Serial.begin(9600);
  Serial.println("Ready");
}

void loop() {

  if (millis() - lastPress >= intervalDebounce) {
    lastPress = millis();
    byte state = digitalRead(switchPin);
    if (state != lastState) {
      lastState = state;
      //start timer to count press..
      if (!timerStarted) {
        timerStarted = true;
        timerStart = millis();
      }
      if (state == HIGH) buttonPresses++;
    }
  }

  if (timerStarted) {
    if (millis() - timerStart >= intervalTimer) {
      Serial.print("Pressed:");
      Serial.println(buttonPresses);
      //do something with presses..


      //reset for next shot
      timerStarted = false;
      buttonPresses = 0;

    }
  }
}

Not only am I a newb with Arduino, but I draw like a drunk 5-year-old :man_shrugging:

Hopefully you can make sense of this.

1 Like

Why are you connecting 9v to the PAM8403 ?

What kind if button is that? I strongly advise to remove the 3v3 lead... (if it is a standard push button).
Push buttons have 4 legs, but they are internally connected in pairs. Best way of connection is diagonal. Then the orientation does not matter
Did you try any library examples? Did you get them to work?

I like it!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.