if-statementarduinoarduino-unoarduino-idearduino-nano

Arduino audio playback without SD card not working?


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


Solution

  • 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;
    }