ESP32-audioI2S how to get two audio files to play sequencially #729
Unanswered
markbiasotti
asked this question in
Q&A
Replies: 2 comments
-
I figured it out from the "time clock" example - you use the audio_eof_mp3() to loop the audio files... void audio_eof_mp3(const char* info) { |
Beta Was this translation helpful? Give feedback.
0 replies
-
I figured out how to do it from the "time clock" sketch example - you use the audio_eof_mp3(const char* info) to loop the audio |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using the ESP32-audioI2S library to create a pill reminder box.
I'm struggling to get two audio files from SD card to play successively (one after the other) when a button is push. I've seen examples in "Talking clock" and using audio_eof_mp3() to do this but cannot get that to work. What I want to do first is (as a test) is that when a button is press an audio prompt would sound "Page 1" and then "turntone". If I simply place one after the other, only one plays:
if ((page_forward_value == LOW) && (page_count < total_page_count)) { Serial.println("page forward"); audio.setVolume(volume_level); // 0...21 audio.connecttoFS(SD, audiofilenames[0][3]); // Play page no. audio.connecttoFS(SD, audiofilenames[1][page_count]); // Play page no.
I will be applying what I learn about how to do this to a time keeping function and the fact that I'll want to announce the current time along with "am" or "pm".
Here is my entire Arduino sketch:
`/* sketch for READ prototype Humanity Press using Huzzah32 Feather .
Board Adafruit ESP32 Feather on COM8 Programmer: ESPtool Upload Speed 921600 on COM 8
DS3231 RTC breakout on SLC and SDA
Sparkfun ST25DV64KC Dynamic RFID Tag Breakout
*/
#include "Adafruit_NeoPixel.h"
#include "RTClib.h"
#include "SparkFun_ST25DV64KC_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_ST25DV64KC
#include "Arduino.h"
#include "Audio.h"
#include "SD.h"
#include "FS.h"
#include "esp_sleep.h"
// microSD Card Reader connections
#define SD_CS 21
#define SPI_MOSI 18
#define SPI_MISO 19
#define SPI_SCK 5
// I2S Connections
#define I2S_DOUT 25
#define I2S_BCLK 12
#define I2S_LRC 17
// Create Audio object
Audio audio;
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
SFE_ST25DV64KC tag;
#define PIN DAC2 // On Trinket or Gemma, suggest changing this to 1
#define NUMPIXELS 1 // Popular NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int const lid_sw = A4; //reed switch
int const page_back = 33;
int const page_forward = 27;
int const Pwr_but = 15;
int const Vol_up = 14;
int const Vol_dn = 32;
int const Pill_confirm = A3;
int const Lang_select = A2;
const char* audiofilenames[][14] = {
/* 0 / { "am.mp3", "pm.mp3", "tick.mp3", "turntone.mp3" },
/ 1 / { "page_1.mp3", "page_2.mp3", "page_3.mp3", "page_4.mp3", "page_5.mp3", "page_6.mp3", "page_7.mp3", "page_8.mp3", "page_9.mp3", "page_10.mp3", "page_11.mp3", "page_12.mp3" },
/ 2 / { "1clock.mp3", "2clock.mp3", "3clock.mp3", "4clock.mp3", "5clock.mp3", "6clock.mp3", "7clock.mp3", "8clock.mp3", "9clock.mp3", "10lock.mp3", "11clock.mp3", "12clock.mp3" },
/ 3 / { "day14inst.mp3", "end_book.mp3", "instruct.mp3", "intro_lg.mp3", "intro_sht.mp3", "med_close.mp3", "closelid.mp3", "clselid2.mp3" },
/ 4 / { "next_7am.mp3", "next_8am.mp3", "next_9am.mp3", "next_10am.mp3" },
/ 5 / { "nexttake.mp3", "not_med.mp3", "not_med2.mp3" },
/ 6 */ { "day_1.mp3", "day_2.mp3", "day_3.mp3", "day_4.mp3", "day_5.mp3", "day_6.mp3", "day_7.mp3", "day_8.mp3", "day_9.mp3", "day_10.mp3", "day_11.mp3", "day_12.mp3", "day_13.mp3", "day_14.mp3" }
};
static const uint16_t ORANGE = 0xFD20;
static const uint16_t PINK = 0xF81F;
static const uint16_t BLUE = 0x001F;
static const uint16_t RED = 0xF800;
static const uint16_t GREEN = 0x07E0;
static const uint16_t CYAN = 0x07FF;
static const uint16_t MAGENTA = 0xF81F;
static const uint16_t YELLOW = 0xFFE0;
static const uint16_t BLACK = 0x0000;
uint16_t LED_color;
////////////////////////////// Timing ///////////////////////////////
int long current_Millis;
int long prev_LED_millis = 0;
int long prev_vol_millis = 0;
int long prev_page_millis = 0;
int read_button_time = 200;
int LED_pause_time = 100;
bool LED_status = false;
int volume_level = 15;
int Vol_up_buttonState; // the current reading from the input pin
int Vol_dn_buttonState; // the current reading from the input pin
int Prev_vol_up_ButtonState = LOW; // the previous reading from the input pin
int Prev_vol_dn_ButtonState = LOW; // the previous reading from the input pin
int page_count = 0;
int const total_page_count = 12;
int audio_toggle = 0;
void setup() {
Wire.begin();
Serial.begin(115200);
pinMode(lid_sw, INPUT);
pinMode(page_back, INPUT);
pinMode(page_forward, INPUT);
pinMode(Lang_select, INPUT);
pinMode(Pwr_but, INPUT);
pinMode(Vol_up, INPUT);
pinMode(Vol_dn, INPUT);
pinMode(Pill_confirm, INPUT);
#ifndef ESP8266
while (!Serial)
; // wait for serial port to connect. Needed for native USB
#endif
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// When time needs to be re-set on a previously configured device, the
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
if (!tag.begin(Wire)) { // check if RFID breakout is connected
Serial.println(F("ST25 not detected. Freezing..."));
while (1)
;
} // Do nothing more
Serial.println(F("ST25 connected."));
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.clear(); // clear all pixels
pixels.show();
pinMode(SD_CS, OUTPUT); // Set microSD Card CS as OUTPUT and set HIGH
digitalWrite(SD_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); // Initialize SPI bus for microSD Card
delay(1000);
if (!SD.begin(SD_CS)) { // Start microSD Card
Serial.println("Error accessing microSD card!");
while (true)
;
}
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); // Setup I2S
//audio.setVolume(volume_level);
//audio.connecttoFS(SD, "intro_sht.mp3"); // Play click
}
void loop() {
current_Millis = millis(); // with audio.loop() nothing in the loop can be blocking
audio.loop();
//if (!audio.isRunning())
// sleep(1);
//void audio_eof_mp3(const char *info) { //put anything in here that you wanted in main loop
Volume_control();
turn_page();
Flash_LED(LED_color); // put anything from the original loop function
}
void audio_eof_mp3(const char* info) {
}
void turn_page() {
if (current_Millis - prev_page_millis > read_button_time) { // millis delay put in here for debounce
int page_forward_value = digitalRead(page_forward);
int page_back_value = digitalRead(page_back);
}
}
void Volume_control() {
if (current_Millis - prev_vol_millis > read_button_time) { // millis delay put in here for debounce
int Vol_up_value = digitalRead(Vol_up);
int Vol_dn_value = digitalRead(Vol_dn);
}
}
void Flash_LED(int color) {
if (LED_status == true) {
pixels.setPixelColor(0, color); // pixel no, pixel color
pixels.show();
prev_LED_millis = current_Millis;
LED_status = false;
}
if (current_Millis - prev_LED_millis > LED_pause_time) {
pixels.setPixelColor(0, BLACK); // pixel no, pixel color
pixels.show();
}
}
void Read_time() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC), Serial.print('/'), Serial.print(now.month(), DEC), Serial.print('/'), Serial.print(now.day(), DEC);
Serial.print(" ("), Serial.print(daysOfTheWeek[now.dayOfTheWeek()]), Serial.print(") "), Serial.print(now.hour(), DEC), Serial.print(':');
Serial.print(now.minute(), DEC), Serial.print(':'), Serial.print(now.second(), DEC), Serial.println();
}
void RFID_read() {
uint8_t values[8] = { 0 };
if (tag.getDeviceUID(values)) {
Serial.print(F("Device UID: "));
for (uint8_t i = 0; i < 8; i++) {
if (values[i] < 0x0a)
Serial.print(F("0"));
Serial.print(values[i], HEX);
Serial.print(F(" "));
}
Serial.println();
}
}`
Beta Was this translation helpful? Give feedback.
All reactions