Buttons not making action occur

Before anyone says this is a hardware issue, I ran a sketch for 3 buttons, to test my buttons on my breadboard, and they all work, and my leds work. That being said.

I have a cigar box with a breadboard and an Arduino Uno in it, along with a small powered speaker and an aux out cord, along with a red and green led. I currently have it working where using the TMRpcm library I can play wav files from my SD card, which is on an ethernet shield. When I plug in my Arduino it plays 1.wav. I can use serial commands to change tracks, adjust volume, and other things. I would like to use 3 buttons to do these tasks instead. I would like button 1 to play and pause. I would like button 2 to play the next track, and if it is the last one, play the first one. I would like button 3 to increase volume from 1-4, then back to 1 if it is at 4, in order to not distort my output.

I have tried so many things, and all I get is either static instead of a song playing, or I get "wav error".

Can you smart people look over my code and tell me what I did wrong? Thanks in advance.

/* UltimateUnoWavplayer V1
 * Created by Nickademus420 aka William R. Miller, 2019
 */
#include <SD.h>               // need to include the SD library
#include <TMRpcm.h>           //  also need to include this library...
#include <SPI.h>

//#define SD_ChipSelectPin 53 
                              //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 4    //using digital pin 4 on arduino nano 328, can use other pins

TMRpcm tmrpcm;                // create an object for use in this sketch

int song_number=0;            //variable used to keep track of current song number

                              // constants won't change. They're used here to set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int buttonPin3 = 5;     // the number of the pushbutton pin
const int ledPin1 =  7;       // the number of the LED pin
const int ledPin2 =  8;       // the number of the LED pin

                              // variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttonState3 = 0;         // variable for reading the pushbutton status

int currentVolume = 4;

                              //Start of Setup 
void setup(){
                              //Initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

                              //Initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);

tmrpcm.speakerPin = 9;        //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  
Serial.begin(9600);           //Open Serial connection for debugging
                              //See if the card is present and can be initialized:
if (!SD.begin(SD_ChipSelectPin)) {
                              //Print error because sd card could not be loaded  
Serial.println("SD fail");  
return;                       //Don't do anything more if sd card is not initialized
  }                           //End of If statement
else{                         //Send message to serial that tells user the SD Card was loaded
  Serial.println("SD card was initialized. Playing first track.");
}                             //End of Else statement
tmrpcm.setVolume(4);          //Now the volume is set to level 4 (0-6)
tmrpcm.play("1.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
if (tmrpcm.isPlaying() == 1){ //Check the status of the track playing, 1 means it is, so we check that with == 1
                              //Send message to serial that tells user that Track 1 is playing
Serial.println("Track 1 is playing, ENJOY!");
song_number = 1;              //Change the stored value of the song number to 1, for use when skipping to next track (It's at 2 now, so that when song 1 ends, the if statement plays 2.wav)
}                             //End of If statement

digitalWrite(ledPin1, HIGH);   // turn the LED on (HIGH is the voltage level)
digitalWrite(ledPin2,HIGH); 
delay(100);
digitalWrite(ledPin1, LOW);    // turn the LED off by making the voltage LOW
digitalWrite(ledPin2, LOW);
}                           //End of Setup section

                              //Start of loop section
void loop(){  
                              //If statement to see if the serial port monitor is open and available (i think)
if(Serial.available()){    
    switch(Serial.read()){    //Change the serial connection to incoming on the Uno (i think)
                              //If user sends 1 through serial to Uno, Play the file 1.wav    
    case '1': tmrpcm.play("1.wav"); break;
                              //If user sends 2 through serial to Uno, Play the file 2.wav
    case '2': tmrpcm.play("2.wav");
    Serial.println("Track 2 is playing, ENJOY!");
    song_number = 2;  break;
                              //If user sends 3 through serial to Uno, Play the file 3.wav
    case '3': tmrpcm.play("3.wav");
    Serial.println("Track 3 is playing, ENJOY!");
    song_number = 3;  break;
                              //If user sends 4 through serial to Uno, Play the file 3.wav
    case '4': tmrpcm.play("4.wav");
    Serial.println("Track 4 is playing, ENJOY!");
    song_number = 4;  break;
                              //If user sends p through serial to Uno, Pause playback, if playback is already paused it will un-pause i.e, continue playback of current track
    case 'p': tmrpcm.pause(); break;
                              //If user sends ? through serial to Uno, a message will be sent to the serial monitor telling the user that a wav file is being played
    case '?': 
    Serial.println("Track");
    Serial.println(song_number);
    Serial.println("is being played");break;
                              //If user sends s through serial to Uno, playback of current track is stopped 
    case 's': tmrpcm.stopPlayback(); break;
                              //If user sends + through serial to Uno, the volume is increased by 1 level (0-6)
    case '+': tmrpcm.volume(1); break;
    currentVolume ++; break;
                      //End of if statement
                              //If user sends - through serial to Uno, the volume is decreased by 1 level (0-6)
    case '-': tmrpcm.volume(0); break;
    currentVolume --; break;
                              //If user sends 5 through serial to Uno, the audio quality is 1xsampling 
    case '5': tmrpcm.quality(0); break;
                              //If user sends 6 through serial to Uno, the audio quality is 2xsampling
    case '6': tmrpcm.quality(1); break;
                              //If user sends v through serial to Uno, a message will be sent to the serial monitor telling the user the current volume
    case 'v': Serial.println("Volume is");
              Serial.println(currentVolume);break;
}                             //End of switch serial.read
}                             //End of if serial.available statement   

digitalWrite(ledPin1, HIGH);   // turn the LED on (HIGH is the voltage level)
digitalWrite(ledPin2,HIGH);



/*
if (song_number == 1){
tmrpcm.play("1.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
Serial.println("Track 2 is being played");
}                             //End of If statement

if (song_number == 2){
tmrpcm.play("2.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
Serial.println("Track 2 is being played");
}                             //End of If statement

if (song_number ==3){
tmrpcm.play("3.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
Serial.println("Track 3 is being played");
}                             //End of If statement 

if (song_number == 4){
tmrpcm.play("4.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
Serial.println("Track 4 is being played");
}                             //End of If statement 


// read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  // check if the #1 pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin1, HIGH);
  if (song_number == 1){
    tmrpcm.play("2.wav");         //The sound file "1.wav" will play each time the arduino powers up, or is reset
  } 
  if (song_number == 2){
    tmrpcm.play("3.wav");
  }
  if (song_number == 3){
    tmrpcm.play("4.wav");
  }
  if (song_number == 4){
    tmrpcm.play("1.wav");
  }
  }

   // read the state of the #2 pushbutton value:
  buttonState2 = digitalRead(buttonPin2);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState2 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin2, HIGH);
   
  } 

   else {
    // turn LED off:
    digitalWrite(ledPin2, LOW);
    
  }

   // read the state of the pushbutton value:
  buttonState3 = digitalRead(buttonPin3);

  // check if the #3 pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState3 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin3, HIGH);
    
  }
  else {
    // turn LED off:
    digitalWrite(ledPin3, LOW);
     
  }      
*/
}                             //End of Loop

Do you have pulldown resistors in place on the inputs ?

Yes, 10k on each negative for the pushbuttons.

Try this (attached).

(Compiles but not tested at all. Hardly reviewed, in fact…)

wave_player.ino (8.21 KB)

Thank you once again Blackfin for the assist. You are amazing, and almost everything is working now as I wanted it to.

I changed max volume to 4 and min volume to 1, as that seems to be the threshold with my setup, not sure why.

After that, all buttons work as intended, except 3. It increases volume from 1 to 2 to 3 to 4, and should stop there, but it goes up to 6 and distorts instead of resetting to 1.

Any idea why?

Nickademus420:
Thank you once again Blackfin for the assist. You are amazing, and almost everything is working now as I wanted it to.

I changed max volume to 4 and min volume to 1, as that seems to be the threshold with my setup, not sure why.

After that, all buttons work as intended, except 3. It increases volume from 1 to 2 to 3 to 4, and should stop there, but it goes up to 6 and distorts instead of resetting to 1.

Any idea why?

Maybe. I made a boo boo. In the button 3 logic, change:

tmrpcm.volume(currentVolume);

to

tmrpcm.setVolume(currentVolume);

FWIW, the rationale behind my limits were as follows:

If you look at the library for TMRpcm the setVolume method works as follows:

void TMRpcm::setVolume(char vol) {
    volMod = vol - 4 ;
}

I wasn’t sure of the rationale for subtracting 4 before applying it to volMod because I didn’t dig deeper because I was lazy. I assumed the range of values for volMod was >=0. In fact it can go negative:

if(volMod > 0){ mod = *OCRnA[tt] >> volMod; }else{ mod = *OCRnA[tt] << (volMod*-1); }

So feel free to adjust MAX_VOLUME and MIN_VOLUME to whatever suits you. If you want to use negative values change currentVolume from byte to char.

Thank you so much! It all works perfectly now! My last step is to clean up the wiring a bit, and then post my project in the Playground or Project Hub. You rock!

Nickademus420: Thank you so much! It all works perfectly now! My last step is to clean up the wiring a bit, and then post my project in the Playground or Project Hub. You rock!

that is great

Nickademus420:
Thank you so much! It all works perfectly now!
My last step is to clean up the wiring a bit, and then post my project in the Playground or Project Hub.
You rock!

Nice. It’s a neat project.

If you find the buttons a bit sensitive you can add a delay to the top of the ReadButtons() function like this:

void ReadButtons( void )
{
    static unsigned long
        timeButtons = 0;

    if( (millis() - timeButtons) < 50 )
        return;
    timeButtons = millis();
.
.
.

Although not a true debounce, it will take some sensitivity away.

Yeah, so far my buttons are solid, but other sources for music player sketches included debounce failsafes or redundancies or whatever you call them. God I love making things like this. I have 3 other projects I need to share with the community. Most are in cigar boxes and look fairly nice, and another is a 16 LED mask, it's sweet.

Nickademus420: Yeah, so far my buttons are solid, but other sources for music player sketches included debounce failsafes or redundancies or whatever you call them. God I love making things like this. I have 3 other projects I need to share with the community. Most are in cigar boxes and look fairly nice, and another is a 16 LED mask, it's sweet.

so curious to see