Skip to content

Instantly share code, notes, and snippets.

@Tamakichi
Last active January 22, 2020 13:52
Show Gist options
  • Save Tamakichi/40da70f0d138f597a600ed54c5b6f9f1 to your computer and use it in GitHub Desktop.
Save Tamakichi/40da70f0d138f597a600ed54c5b6f9f1 to your computer and use it in GitHub Desktop.
M5Stackで音量制御付きtoneのサンプル
#include <M5Stack.h>
// toneEx
// 引数
// frequency (Hz)
// vol (0 ~ 9、0:無音 9:最大)
void toneEx(uint16_t frequency, uint16_t vol) {
ledcSetup(TONE_PIN_CHANNEL, frequency, 10);
ledcWrite(TONE_PIN_CHANNEL,0x1FF>>(9-vol));
}
// NoToneEx
void noToneEx() {
ledcWriteTone(TONE_PIN_CHANNEL, 0);
digitalWrite(SPEAKER_PIN, 0);
}
void setup() {
M5.begin(true, false, true);
M5.Power.begin();
ledcSetup(TONE_PIN_CHANNEL, 0, 10);
ledcAttachPin(SPEAKER_PIN, TONE_PIN_CHANNEL);
Serial.begin(115200);
M5.Lcd.clear(BLACK);
M5.Lcd.setTextSize(3);
M5.Lcd.setCursor(40, 10);
M5.Lcd.println("Tone example");
}
void loop() {
M5.update();
if (M5.BtnA.wasReleased()) {
toneEx(440,1);
delay(100);
noToneEx();
} else if (M5.BtnB.wasReleased()) {
toneEx(440,3);
delay(100);
noToneEx();
} else if (M5.BtnC.wasReleased()) {
toneEx(440,5);
delay(100);
noToneEx();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment