<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[using M5.Speaker.playWav in void loop()]]></title><description><![CDATA[<p dir="auto">Hello I am using Arduino IDE 2.3.3 and a M5stack core 2.<br />
I have found that M5.Speaker.playWav will work for me when I call on it within void setup() but I am unable to use the same M5.Speaker.playWav function in void loop().</p>
<p dir="auto">Is there a work around or another option for me that would get my wav file to play during the point in the loop I would like it to ?</p>
<pre><code>void playSuccessChirp(){
  Serial.println("Success Tone Activated");
  M5.Speaker.tone(420,1000);
  delay(1000);
  // Load audio files and write to buffers
  success_chirp = LittleFS.open("/success_chirp.wav", "r");
  success_chirp_fileSize = success_chirp.size();
  success_chirp_Buffer = (uint8_t*)malloc(success_chirp_fileSize);
  success_chirp.read(success_chirp_Buffer, success_chirp_fileSize);
  success_chirp.close();
  Serial.println("Tone Chirp Activated");
  M5.Speaker.playWav(success_chirp_Buffer);
  delay(2000);
}
void setup(void) {
    Serial.begin(19200);
    auto cfg = M5.config();
    M5.begin(cfg);
    M5.Display.begin();
    SPIFFS.begin();
    // Open the font file
    File SpaceMono18 = LittleFS.open("/SpaceMono18.vlw","r");
    // Read font file contents into a buffer
    size_t SpaceMono18_fileSize = SpaceMono18.size();
    uint8_t* SpaceMono18_fontBuffer = (uint8_t*)malloc(SpaceMono18_fileSize);
    SpaceMono18.read(SpaceMono18_fontBuffer, SpaceMono18_fileSize);
    SpaceMono18.close();
    // Load Font 
    M5.Display.loadFont(SpaceMono18_fontBuffer);
    // Open the font file
    File SpaceMono16 = LittleFS.open("/SpaceMono16.vlw","r");
    // Read font file contents into a buffer
    size_t SpaceMono16_fileSize = SpaceMono16.size();
    uint8_t* SpaceMono16_fontBuffer = (uint8_t*)malloc(SpaceMono16_fileSize);
    SpaceMono16.read(SpaceMono16_fontBuffer, SpaceMono16_fileSize);
    SpaceMono16.close();
    // Load Font 
    M5.Display.loadFont(SpaceMono18_fontBuffer);
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(VOLTBLUE);
    screen_height = M5.Display.height();
    screen_width  = M5.Display.width();
    M5.Lcd.drawPng(CDTR_BG_IMG, 29320, 50, 0, 230, 230 );
    M5.Lcd.drawPng(GAMEVOLT_IMG, 13627, 320 - 98, 0, 98, 40 );
    
    // Open the audio file
    File arena_startup_chirp = LittleFS.open("/arena_startup_chirp.wav","r");
    // Read audio file contents into a buffer
    size_t arena_startup_chirp_fileSize = arena_startup_chirp.size();
    uint8_t* arena_startup_chirp_Buffer = (uint8_t*)malloc(arena_startup_chirp_fileSize);
    arena_startup_chirp.read(arena_startup_chirp_Buffer, arena_startup_chirp_fileSize);
    arena_startup_chirp.close();
    // Set Volume
    M5.Speaker.setVolume(volume);
    // Play Wav
    M5.Speaker.playWav(arena_startup_chirp_Buffer);
    delay(2000);
    free (arena_startup_chirp_Buffer);
    
    // Draw UI    
    M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 20, screen_width - 4, 40, ui_radius, MINTGREEN);
    M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 20, screen_width - 8, 36, ui_radius, BLACK);
    M5.Display.drawString("Initializing", screen_width / 2 + 8, screen_height / 2 - 8);
    
    pinMode(SD_CS, OUTPUT);
    digitalWrite(SD_CS, HIGH);
    SPI.begin(SD_SCK, SD_MISO, SD_MOSI);
    SPI.setFrequency(1000000);


    // Dramatic Pause 
    delay(2000);

    // Draw UI    
    M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 20, screen_width - 4, 40, ui_radius, MINTGREEN);
    M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 20, screen_width - 8, 36, ui_radius, BLACK);
    M5.Display.drawString("Please Connect Ethernet", screen_width / 2 + 8, screen_height / 2 - 6);

    m5::board_t board = M5.getBoard();
    switch (board) {
        case m5::board_t::board_M5Stack: {
            cs_pin  = 5;
            rst_pin = 0;
            int_pin = 35;
        } break;
        case m5::board_t::board_M5StackCore2: {
            cs_pin  = 33;
            rst_pin = 0;
            int_pin = 35;
        } break;
        case m5::board_t::board_M5StackCoreS3: {
            cs_pin  = 1;
            rst_pin = 0;
            int_pin = 10;
        } break;
      } 
    SPI.begin(SCK, MISO, MOSI, -1);
    LAN.setResetPin(rst_pin);
    LAN.reset();
    LAN.init(cs_pin);
    while (LAN.begin(mac) != 1) {
        Serial.println("Error getting IP address via DHCP, trying again...");
        delay(2000);
        }
   client.setServer(mqtt_server, mqtt_port);  // Sets the server details. 
   client.setCallback(callback);  // Sets the message callback function. 

  //I am unsure why the state bellow works but this state holds true when the device is waiting for DHCP
  //If someone can let me know why/when this state occurs it would be appreciated 
  button.initButton(&amp;M5.Lcd, 320 - 98, 0, 98, 40, VOLTBLUE, BLACK, BLACK, "" );
  //button.drawButton();
  M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 60, screen_width - 4, 120, ui_radius, MAGENTA);
  M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 60, screen_width - 8, 116, ui_radius, BLACK);
  M5.Display.drawString("DHCP issued:", screen_width / 2, screen_height / 2 - 40); 
  IPAddress IP = LAN.localIP();                                          
  M5.Display.drawString(IP.toString(), screen_width / 2, screen_height / 2 - 8);
  //M5.Display.setTextFont(&amp;fonts::FreeSans9pt7b);
  M5.Display.drawString("Please standby", screen_width / 2, screen_height / 2 + 24);
  //M5.Display.setTextFont(&amp;fonts::FreeSans12pt7b);

}
void loop(void) {
  //audio.loop();
  Serial.println("Start of loop");
  M5.update();
  delay(500);
  auto link = LAN.linkStatus();
  auto clientState = client.state();
    switch (link){ 
        case Unknown:
            Serial.println("LAN Link State: Unknown");
            if (current_state != 1){ 
              Serial.println("Begin change to state 2");
              M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 20, screen_width - 4, 40, ui_radius, ORANGE);
              M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 20, screen_width - 8, 36, ui_radius, BLACK);
              M5.Display.drawString("Please Connect Ethernet", screen_width / 2 + 15, screen_height / 2);
              current_state = 1;
              Serial.println("Changed to state 1");
            }
            break;      
        case LinkON: {
            Serial.println("LAN Link State: ON");
            IPAddress IP = LAN.localIP();
            Serial.println("Connected to local network at : " + IP.toString());
            if (client.connect("arduinoClient") &amp;&amp; current_state != 2){
                playSuccessChirp(); 
                Serial.println("Begin change to state 2");
                M5.Display.clearDisplay();
                M5.Lcd.drawPng(CDTR_BG_IMG, 29320, 50, 0, 230, 230 );
                M5.Lcd.drawPng(GAMEVOLT_IMG, 13627, 320 - 98, 0, 98, 40 );   
                M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 60, screen_width - 4, 120, ui_radius, GREEN);
                M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 60, screen_width - 8, 116, ui_radius, BLACK);
                M5.Display.drawString("Success !", screen_width / 2 + 8, screen_height / 2 - 40);                                           
                M5.Display.drawString("Connected to remote server", screen_width / 2 + 8, screen_height / 2 - 8);
                M5.Display.drawString("Test completed", screen_width / 2, screen_height / 2 + 24);
                // Dramatic Pause 
                delay(2000);
                current_state = 2;
                Serial.println("Changed to state 2");
            } else if (!client.connect("arduinoClient") &amp;&amp; current_state != 7){
                Serial.println("Begin change to state 7"); 
                M5.Display.clearDisplay();
                M5.Lcd.drawPng(CDTR_BG_IMG, 29320, 50, 0, 230, 230 );
                M5.Lcd.drawPng(GAMEVOLT_IMG, 13627, 320 - 98, 0, 98, 40 );   
                M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 60, screen_width - 4, 120, ui_radius, MAGENTA);
                M5.Display.fillSmoothRoundRect(4, screen_height / 2 - 60, screen_width - 8, 116, ui_radius, BLACK);
                M5.Display.drawString("Connected to local network", screen_width / 2, screen_height / 2 - 40);                                           
                M5.Display.drawString("IP:"+IP.toString(), screen_width / 2, screen_height / 2 - 8);
                //M5.Display.setTextFont(&amp;fonts::FreeSans9pt7b);
                M5.Display.drawString("Unable to connect to server", screen_width / 2, screen_height / 2 + 24);
                //M5.Display.setTextFont(&amp;fonts::FreeSans12pt7b);
                delay(2000);
                current_state = 7;
                Serial.println("Changed to state 7");
            }
</code></pre>
]]></description><link>https://community.m5stack.com/topic/6958/using-m5-speaker-playwav-in-void-loop</link><generator>RSS for Node</generator><lastBuildDate>Sun, 17 May 2026 01:59:36 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/6958.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 05 Nov 2024 18:58:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to using M5.Speaker.playWav in void loop() on Thu, 14 Nov 2024 09:45:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/alecdvor" aria-label="Profile: alecdvor">@<bdi>alecdvor</bdi></a><br />
Maybe this function blocks the code that follows it?</p>
]]></description><link>https://community.m5stack.com/post/27033</link><guid isPermaLink="true">https://community.m5stack.com/post/27033</guid><dc:creator><![CDATA[kuriko]]></dc:creator><pubDate>Thu, 14 Nov 2024 09:45:52 GMT</pubDate></item></channel></rss>