Function:
Initialize the speaker
Syntax:
void begin()
Pay Attention:
1.If you don't want to use M5.begin() Initialize the speaker, please call this function before using the speaker
Example:
#include <M5Stack.h>
void setup(){
M5.Speaker.begin(); //Initialize the speaker
M5.Speaker.tone(661, 1000); //Set the speaker to tone at 661Hz for 1000ms
}
void loop(){
M5.Speaker.update();
delay(100);
}
Function:
Stop speaker
Syntax:
void end()
Example:
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { //If ButtonA was pressed
M5.Speaker.tone(661); //Set the speaker to tone continuously at 661Hz
}else if(M5.BtnB.wasPressed()){
M5.Speaker.end(); //Turn off the speaker
}
delay(100);
}
Function:
Set the speaker for output
Syntax:
void update()
Pay Attention:
1.It needs to be used in conjunction with the sound function to make M5Core sound
Example:
#include <M5Stack.h>
void setup(){
M5.Speaker.begin(); //Initialize the speaker
M5.Speaker.tone(661, 1000); //Set the speaker to tone at 661Hz for 1000ms
}
void loop(){
M5.Speaker.update();
delay(100);
}
Function:
Set the speaker to sound continuously at frequency / (Tone duration milliseconds)
Syntax:
void tone(uint16_t frequency)
void tone(uint16_t frequency, uint32_t duration)
Function parameter:
Parameter | Type | Description |
---|---|---|
frequency | uint16_t | Speaker frequency |
duration | uint32_t | Ring time (milliseconds) |
Example:
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { //If buttonA was pressed
M5.Speaker.tone(661, 200); //Set the speaker to tone 200ms at 661Hz
}else if(M5.BtnB.wasPressed()){
M5.Speaker.tone(112); //Set the horn to tone continuously at 112Hz
}else if(M5.BtnC.wasPressed()){
M5.Speaker.end(); //Turn off the speaker
}
delay(100);
}
Function:
Set volumn
void setVolume(uint8_t volume)
Function parameter:
Parameter | Type | Description |
---|---|---|
volume | uint8_t | Volumn(0~11) |
Example:
#include <M5Stack.h>
char i = 0;
void setup(){
M5.begin();
M5.Speaker.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { //If buttonA was pressed
M5.Speaker.tone(661, 200); //Set the speaker to tone 200ms at 661Hz
}else if(M5.BtnC.wasPressed()){
M5.Speaker.setVolume(i++); //Set speaker volumn
}
delay(100);
}