if button 1 to 5 pressed send: Serial.println("button 1/5 pressed" not working

Hello, I just started coding arduino and i am working on a mp3 player project that has to play 5 different sound tracks, but I cant get the code working.
When a button is pressed it plays the specific file that only that button can play, so every button plays it specific track. So button 1 plays track 1, button 2 plays track 2 etc. (I am using a dfplayermini connected to my arduino nano via the “SoftwareSerial.h” libary, but this is not realy important for the sketch)

I have now connected 5 buttons with 10KΩ resistors to gnd, I would like to keep everything(code and hardware) as bare minimum as possible, and hope (if possible) to get rid of the 10KΩ resistors.

The “Serial.println” is only for testing (as visible in my sketch), it will later send “myDFPlayer.playFolder(1 to 5, 1);” over SoftwareSerial but this is not important for how the buttons operate and send their own specific command/data

#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

//SoftwareSerial mySoftwareSerial(10, 11); //serial connection Arduino <-> DFPlayerMini (TX=D10 RX=D11)
//DFRobotDFPlayerMini myDFPlayer;

const int Button1 = 2;      // assign pin 2 to Button1
const int Button2 = 3;      // assign pin 3 to Button2
const int Button3 = 4;      // assign pin 4 to Button3 
const int Button4 = 5;      // assign pin 5 to Button4 
const int Button5 = 6;      // assign pin 6 to Button5 
int var1 = 0;
int var2 = 0;
int var3 = 0;
int var4 = 0;
int var5 = 0;

void setup() {
  Serial.begin(115200);  //[**FOR TESTING ONLY**]
  mySoftwareSerial.begin(9600); //Serial Arduino <-> DFPlayerMini
  myDFPlayer.setTimeOut(500); //Serial timeout (ms)
  myDFPlayer.volume(10);  //Volume (0-30)
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // (U_DISK)|(AUX)|(SLEEP)|(FLASH)

  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(Button3, INPUT);
  pinMode(Button4, INPUT);
  pinMode(Button5, INPUT);

void loop(){
  {var1 = digitalRead(Button1);
  if (var1 == LOW) {
    Serial.println("button 1 pressed");  //[**FOR TESTING ONLY**]
//    myDFPlayer.playFolder(1, 1);  //[**DISABLED FOR TESTING**]

    {var2 = digitalRead(Button2);
  if (var2 == LOW) {
    Serial.println("button 2 pressed");  //[**FOR TESTING ONLY**]
//    myDFPlayer.playFolder(2, 1);  //[**DISABLED FOR TESTING**]

    {var3 = digitalRead(Button3);
  if (var3 == LOW) {
    Serial.println("button 3 pressed");  //[**FOR TESTING ONLY**]
//    myDFPlayer.playFolder(3, 1);  //[**DISABLED FOR TESTING**]

    {var4 = digitalRead(Button4);
  if (var4 == LOW) {
    Serial.println("button 4 pressed");  //[**FOR TESTING ONLY**]
//    myDFPlayer.playFolder(4, 1);  //[**DISABLED FOR TESTING**]

    {var5 = digitalRead(Button5);
  if (var5 == LOW) {
    Serial.println("button 5 pressed");  //[**FOR TESTING ONLY**]
//    myDFPlayer.playFolder(5, 1);  //[**DISABLED FOR TESTING**]

More research would have shown you to use “input-pullup” for the switch pins. Then test for zero when the switch is pressed.


Read this example

Also when you start numbering your variables it usually mean it’s time to consider an array