Help with a project button in loop on serial monitor

Hey there, this is the first project that I'm creating on my own and I'm having a problem that I cant fix :frowning:

The project is a simple 4 button audio player/sampler. button 1 plays audio 0001.wav, button 2 0002.wav and it goes until button 4.
This project worked fine on protoboard, but once I solded everything on a PCB and replaced it for an arduino nano, its not working anymore. All my 4 buttons are in a loop on serial monitor and it keeps showing:

Button 1: 1
Button 2: 1
Button 3: 1
Button 4: 1

In an infinite loop. It recognizes when I press the button, but it keeps going on the loop and it doesnt play the sample.

This is my code:

#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>

TMRpcm tmrpcm;  // Criando um objeto player.

const int chipSelect = 4;
const int buttonPin1 = 2;
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 8;

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);

  tmrpcm.speakerPin = 9;     // O alto-falante está conectado ao pino 9.
  Serial.begin(9600);         // Inicializando a porta serial com a velocidade de 9600 bauds.
  if (!SD.begin(chipSelect))  // Se o cartão estiver disponível.
  {
    Serial.println("Falha no SD");
    return;
  }
  Serial.println("SD funcionando");
}

void loop() {
  int buttonState1 = digitalRead(buttonPin1);
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);

  Serial.print("Button 1: ");
  Serial.println(buttonState1);
  Serial.print("Button 2: ");
  Serial.println(buttonState2);
  Serial.print("Button 3: ");
  Serial.println(buttonState3);
  Serial.print("Button 4: ");
  Serial.println(buttonState4);

  if (buttonState1 == LOW) {
    tmrpcm.play("0001.wav");
    Serial.println("Botão 1 pressionado");
    delay(500);  // Aguarda um pouco para evitar acionamentos múltiplos do botão.
  }

  if (buttonState2 == LOW) {
    tmrpcm.play("0002.wav");
    Serial.println("Botão 2 pressionado");
    delay(500);
  }

  if (buttonState3 == LOW) {
    tmrpcm.play("0003.wav");
    Serial.println("Botão 3 pressionado");
    delay(500);
  }

  if (buttonState4 == LOW) {
    tmrpcm.play("0004.wav");
    Serial.println("Botão 4 pressionado");
    delay(500);
  }
}

Any suggestion might be useful

Thankssss

"Replaced it for an Arduino Nano" -- what were you using before?

1 Like

Hi @lualeo ,

Welcome to the forum..
Sounds like TMRpcm is having an issue..
You know, if you don't press a button, then yes, you print at loop speed..
Maybe only print when the button is pressed..

And what nano specifically are you using now??
their not all the same..

good luck.. ~q

1 Like
  • In your projects, suggest you only do something when a switch condition changes to a level, not while the switch is sitting at a level.
1 Like

See if this fixes it if you like (no guarantees :wink: )

#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>

TMRpcm tmrpcm;  // Criando um objeto player.

const int chipSelect = 4;
const int buttonPin1 = 2;
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 8;

int buttonState1, buttonState2, buttonState3, buttonState4;

int track; // to drive the state machine based on which button was pressed

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);
  buttonState1 = HIGH;
  buttonState2 = HIGH;
  buttonState3 = HIGH;
  buttonState4 = HIGH;
  track = 0;

  tmrpcm.speakerPin = 9;     // O alto-falante está conectado ao pino 9.
  Serial.begin(9600);         // Inicializando a porta serial com a velocidade de 9600 bauds.
  if (!SD.begin(chipSelect))  // Se o cartão estiver disponível.
  {
    Serial.println("Falha no SD");
    return;
  }
  Serial.println("SD funcionando");
}

void loop() {
  checkButtons();
  switch (track) {
    case 1:
      tmrpcm.play("0001.wav");
      Serial.println("reproduciendo la pista 1");
      break;
    case 2:
      tmrpcm.play("0002.wav");
      Serial.println("reproduciendo la pista 2");
      break;
    case 3:
      tmrpcm.play("0003.wav");
      Serial.println("reproduciendo la pista 3");
      break;
    case 4:
      tmrpcm.play("0004.wav");
      Serial.println("reproduciendo la pista 4");
      break;
  }
  debugging(); // comment out once you don't need
}

void checkButtons() {
  buttonState1 = digitalRead(buttonPin1);
  delay(20); // simple debouncing
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == LOW) {
    track = 1;
  }
  buttonState2 = digitalRead(buttonPin2);
  delay(20); // simple debouncing
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == LOW) {
    track = 2;
  }
  buttonState3 = digitalRead(buttonPin3);
  delay(20); // simple debouncing
  buttonState3 = digitalRead(buttonPin3);
  if (buttonState3 == LOW) {
    track = 3;
  }
  buttonState4 = digitalRead(buttonPin4);
  delay(20); // simple debouncing
  buttonState4 = digitalRead(buttonPin4);
  if (buttonState4 == LOW) {
    track = 4;
  }
  if ((buttonState1 == HIGH) && (buttonState2 == HIGH) &&
      (buttonState3 == HIGH) && (buttonState4 == HIGH)) {
    track = 0;
  }
}

void debugging() {
  Serial.print("Button 1: ");
  Serial.println(buttonState1);
  Serial.print("Button 2: ");
  Serial.println(buttonState2);
  Serial.print("Button 3: ");
  Serial.println(buttonState3);
  Serial.print("Button 4: ");
  Serial.println(buttonState4);
}
1 Like

Hi! So I was using an Arduino Uno before, i started using this and the protoboard. Now I'm on Arduino nano and a PCB

Hi @qubits-us, thanks so much for replying.

As soon as I get home tonight I'm testing to only print when button is pressed. What I think its kind weird is that I've tested this circuit using an Arduino Uno and on protobard, it worked just fine.
Now I'm using an Arduino Nano V3 and the buttons are soldered to the PCB, and I doesnt work :frowning:

Thankssss

Hey @LarryD, good advice!

Thanks a lot, I'll incorporate that to my projects :slight_smile:

@hallowed31, woww thankssss so much for this code!

I'm gonna test it out as soon as I get home tonight, lets see if it works

Thanks a lot!! <3

1 Like

Given the uno working, but nano/pcb not working premise, it's likely wiring and/or "power".

1 Like

Yess, you're right! I guess the problem is on wiring/power.

I've uploaded the code that @hallowed31 posted and I kept having the same issue. Also made a test with only 1 button and it shows the same message on serial monitor: "Button 1: 1" in a continuous loop.

I really appreciate all the messages and suggestions. Now I'm gonna remake the soldering, testing everything before soldering (lesson learned haha)

Thanks a lot guys

2 Likes

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