ボタンを押して音を鳴らす



概要

開発環境:ArdinoIDE
→ダウンロードしていない人はこちら

ボタンを押して音を鳴らす方法を解説
サンプルコード:speak.ino
プログラム概要:左のボタンを押せば音がなる。

解説

「Tool」→「Board」の順で使用するボードの設定をM5stack core2にする。
(1つ下のCOM設定を忘れがち)

「File」→「Examples」→「M5Core2」→「Basics」→「speak」の順でサンプルスケッチ例を開く。

speakのソースコード

/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
*                  Equipped with M5Core2 sample source code
*                          配套  M5Core2 示例源代码
* Visit for more information: https://docs.m5stack.com/en/core/core2
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/core2
*
* Describe: Speaker example.  喇叭示例
* Date: 2022/7/26
*******************************************************************************
*/
#include <M5Core2.h>

void DisplayInit(void) {     // Initialize the display. 显示屏初始化
  M5.Lcd.fillScreen(WHITE);  // Set the screen background color to white.
                             // 设置屏幕背景色为白色
  M5.Lcd.setTextColor(
      BLACK);  // Set the text color to black.  设置文字颜色为黑色
  M5.Lcd.setTextSize(2);  // Set font size to 2.  设置字体大小为2
}

//在 M5Core
//启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。
void setup() {
  M5.begin(true, true, true, true);  // Init M5Core2.  初始化 M5Core2
  DisplayInit();
  M5.Lcd.setTextColor(RED);
  M5.Lcd.setCursor(10,
                   10);  // Set the cursor at (10,10).  将光标设在(10,10)处
  M5.Lcd.printf("Speak Test!");  // The screen prints the formatted string and
      // wraps it.  屏幕打印格式化字符串并换行
  M5.Lcd.setTextColor(BLACK);
  M5.Lcd.setCursor(10, 26);
  M5.Lcd.printf("Press Left Button to listen DingDong!");
  M5.Spk.begin();     // Initialize the speaker.  初始化扬声器
  M5.Spk.DingDong();  // Play the DingDong sound.  播放 DingDong 声音
  delay(100);
}

/* After the program in setup() runs, it runs the program in loop()
The loop() function is an infinite loop in which the program runs repeatedly
在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
loop()函数是一个死循环,其中的程序会不断的重复运行 */
void loop() {
  TouchPoint_t pos =
      M5.Touch.getPressPoint();  // Stores the touch coordinates in pos.
                                 // 将触摸坐标存储在pos.内
  if (pos.y > 240)
    if (pos.x < 109) {
      M5.Axp.SetLDOEnable(3, true);  // Open the vibration.   开启震动马达
      delay(100);
      M5.Axp.SetLDOEnable(3, false);
      delay(100);
      M5.Spk.DingDong();  // Play the DingDong sound.  播放 DingDong 声音
    }
  delay(10);
}

Uploadをクリックし、M5stack Core2にプログラムを書き込む。
左の☑を押さなくてもコンパイルをして書き込むことができる。
(書き込みはM5stack Core2を繋ぐ必要あり)
書き込みはせず、コンパイルだけ試したい場合は左の☑を押すとできる。

M5stack core2の画面が画像のようになり、左ボタンを押すと音が鳴れば成功

ボタンが押されたことを検知する関数は別のものを使用したサンプルプログラムを作成したので共有
プログラム解説
左ボタンを押せば「A」が表示される。
真ん中のボタンを押せば「B」が表示される。
真ん中のボタン0.7秒間押すと画面がクリアされる。
右ボタンを押せば「C」が表示され音が鳴る。


#include <M5Core2.h>

void setup() {
  M5.begin();
  M5.Lcd.setTextSize(3);
  M5.Lcd.print("Hello World");
  M5.Spk.begin();     // Initialize the speaker.
  M5.Spk.DingDong();  // Play the DingDong sound.
  delay(100);
}

void loop() {
  M5.update();  
  if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
    M5.Lcd.print('A');
  } else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
    M5.Lcd.print('B');
  } else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
    M5.Lcd.print('C');
    M5.Spk.DingDong();  // 音が鳴る
  } else if (M5.BtnB.wasReleasefor(700)) {
    M5.Lcd.clear(
        WHITE);  // 画面をクリアし、背景を白色にする
    M5.Lcd.setCursor(0, 0);
  }
  delay(20);
}

コメント