-1

I want to create a circuit that plays mainly 2 audio, one when it gets power and another audio by pressing a button. I want to create it without SD card. Library used is PCM. Here is the code sample.

#include <PCM.h>

const unsigned char sample[] PROGMEM = {
  0,6,14,22,30,38,46,54,60,68,74,82,90,98,106,114,112,
  };

void setup()
{
    startPlayback(sample, sizeof(sample));
}

void loop()
{
 
}

if anyone knows how to play audio in arduino using button (without sd card) help me...

Solved : By adding INPUT_PULLUP

3
  • 2
    You didn't ask a question. Learn how to read digital inputs arduino.cc/reference/en/language/functions/digital-io/… and do PCM on the Nano arduino.cc/reference/en/libraries/pcm Commented Nov 18, 2021 at 9:30
  • Does that sample work for you? I doubt that these 17 bytes of data produce some recognizable sound. The original sample has a couple thousand bytes. Commented Nov 18, 2021 at 11:24
  • The added sound signal is not actual, when I added the actual sound it works Commented Nov 18, 2021 at 12:54

1 Answer 1

1
const unsigned char sample2[] PROGMEM = {
    100,96,84,72,60,58,46,34,20,18,4,12,20,38,46,54,62,
};

int inPin = 7;
void setup()
{
    pinMode(inPin, INPUT_PULLUP);
}

int lastPin = HIGH;  // HIGH means not pressed for Pullup Inputs
void loop()
{
    int pin = digitalRead(inPin);

    if (pin == lastPin)
        return;

    if (pin == HIGH) {
       startPlayback(sample1, sizeof(sample1));
    } else {
       startPlayback(sample2, sizeof(sample2));
    }

    lastPin = pin;
}
Sign up to request clarification or add additional context in comments.

4 Comments

you have to trigger the pin status change from LOW to HIGH ( or opposite ) and debounce it ( or wait some time for the sound to finish )
Tried the answer of @mmixLinus, it not playing both. But when I changed digitalRead to analogRead and changed pin they both works . But now the problem is that the second sound(play when switch pressed) is playing continuously. Not stopping until switch released.
modified code to make the button input pullup (so digitalRead should work) and handle playing both samples. Start playing 1 if button is pressed, start playing 2 if button is released.
Thank you, @mmixLinus and @datafiddler Earlier I forget to add the INPUT_PULLUP , Now its working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.