//include SD module library
#include "SD.h"
#include <TMRpcm.h>
//include speaker control library https://github.com/TMRh20/TMRpcm/archive/master.zip
#define SD_ChipSelectPin 10 //define CS pin
#define button_Pin 2
#define speaker_Pin 9
TMRpcm sound; //create an object for speaker library
int pitch=440;
//debounce variables
int debounce = 500; // wanted debounce time in milliseconds
long last_updated = 0; //timestamp of previous interrupt
long curr_time; // current time in as reported by millis()
// change lighting mode on button click
void button_ISR(){
curr_time = millis();
if ((curr_time - last_updated) > debounce){
sound.stopPlayback();
sound.play("like.wav"); // 8-bit, mono, 16-24khz at best
}
last_updated = curr_time;
}
void setup(){
//interrupt setup
pinMode(button_Pin, INPUT_PULLUP);
pinMode(speaker_Pin, OUTPUT);
attachInterrupt(0, button_ISR, FALLING);
int const pitch=440;
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initializing SD card...");
if (!SD.begin(SD_ChipSelectPin)) { //see if the card is present and can be initialized
Serial.println("Initialization failed...");
return; //don't do anything more if not
}
else {
Serial.println("Initialization done...");
}
sound.speakerPin = 9; //define speaker pin.
//you must use pin 9 of the Arduino Uno and Nano
//the library is using this pin
sound.quality(1); // 0 or 1 -- 1 equals 2x oversampling
sound.setVolume(6); //0 to 7. Set volume level
}
void loop(){
int state=digitalRead(button_Pin);
if (state==LOW)
tone(speaker_Pin,pitch);
else
noTone(speaker_Pin);
}