🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    using M5.Speaker.playWav in void loop()

    Arduino
    2
    2
    313
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      alecdvor
      last edited by

      Hello I am using Arduino IDE 2.3.3 and a M5stack core 2.
      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().

      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 ?

      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(&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(&fonts::FreeSans9pt7b);
        M5.Display.drawString("Please standby", screen_width / 2, screen_height / 2 + 24);
        //M5.Display.setTextFont(&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") && 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") && 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(&fonts::FreeSans9pt7b);
                      M5.Display.drawString("Unable to connect to server", screen_width / 2, screen_height / 2 + 24);
                      //M5.Display.setTextFont(&fonts::FreeSans12pt7b);
                      delay(2000);
                      current_state = 7;
                      Serial.println("Changed to state 7");
                  }
      
      kurikoK 1 Reply Last reply Reply Quote 0
      • kurikoK
        kuriko @alecdvor
        last edited by

        @alecdvor
        Maybe this function blocks the code that follows it?

        Good morning, and welcome to the Black Mesa Transit System.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post