m5stack / M5Unified

Unified library for M5Stack series
MIT License
296 stars 53 forks source link

speaker on core 2 #118

Open slidekor opened 1 month ago

slidekor commented 1 month ago

I am trying to use the speaker so that it works when the button is pressed, but it turns on when it is turned on and does not respond to the button, how to fix it

lovyan03 commented 1 month ago

Hello, @slidekor

Please try following code. You should hear a sound when you touch the button area at the bottom of the screen.

#include <M5Unified.h>

void setup() {
  M5.begin();
}

void loop() {
  M5.update();

  if (M5.BtnA.wasPressed()) {
    M5.Speaker.tone(800, 100);
  }
  if (M5.BtnB.wasPressed()) {
    M5.Speaker.tone(1600, 100);
  }
  if (M5.BtnC.wasPressed()) {
    M5.Speaker.tone(3200, 100);
  }

  M5.Display.fillRoundRect(  0, 10, 100, 100, 10, M5.BtnA.isPressed() ? TFT_YELLOW : TFT_RED);
  M5.Display.fillRoundRect(100, 10, 100, 100, 10, M5.BtnB.isPressed() ? TFT_YELLOW : TFT_RED);
  M5.Display.fillRoundRect(200, 10, 100, 100, 10, M5.BtnC.isPressed() ? TFT_YELLOW : TFT_RED);
}