#include <SD.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10
#define SDCARD_SS_PIN 10
#define led 13
#define button 7
TMRpcm audio;
String fileNames[] = {"message1.wav", "45min.wav", "30min.wav", "15min.wav", "10min.wav", "5min.wav", "1min.wav"}; // list of message file names
int messageTimes[] = {0, 45*60, 30*60, 15*60, 10*60, 5*60, 60}; // message times in seconds
int numMessages = sizeof(messageTimes)/sizeof(messageTimes[0]); // number of messages
unsigned long startTime = 0; // time the button was last pressed
int lastMessageIndex = 0; // index of the last played message
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
Serial.println("SD OK");
audio.speakerPin = 9;
}
void loop() {
if (digitalRead(button) == LOW) {
digitalWrite(led, HIGH);
if (startTime == 0) { // if this is the first button press, start the timer and play the first message
audio.play(fileNames[0]);
lastMessageIndex = 0;
startTime = millis();
}
unsigned long elapsedTime = (millis() - startTime) / 1000; // elapsed time in seconds
int nextMessageIndex = -1;
for (int i = 1; i < numMessages; i++) { // start from i=1 to skip the first message
if (elapsedTime >= messageTimes[i] && i > lastMessageIndex) {
nextMessageIndex = i;
}
}
if (nextMessageIndex != -1) { // if there's a new message to play, play it
audio.play(fileNames[nextMessageIndex]);
lastMessageIndex = nextMessageIndex;
}
digitalWrite(led, LOW);
}
}