Class Name: Speaker
Functionality:
Initializes the speaker.
Function Prototype:
void begin()
Usage Example:
#include <M5Stack.h>
void setup(){
M5.Speaker.begin(); // Initialize the speaker
M5.Speaker.tone(661, 1000); // Set the speaker to sound at 661Hz for 1000ms
}
void loop(){
M5.Speaker.update();
delay(100);
}
Functionality:
Stops the speaker.
Function Prototype:
void end()
Usage Example:
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { // If button A was pressed
M5.Speaker.tone(661); // Set the speaker to continuously sound at 661Hz
}else if(M5.BtnB.wasPressed()){
M5.Speaker.end(); // Turn off the speaker
}
delay(100);
}
Functionality:
Outputs the speaker's settings.
Function Prototype:
void update()
Usage Example:
#include <M5Stack.h>
void setup(){
M5.Speaker.begin(); // Initialize the speaker
M5.Speaker.tone(661, 1000); // Set the speaker to sound at 661Hz for 1000ms
}
void loop(){
M5.Speaker.update();
delay(100);
}
Functionality:
Sets the speaker to sound at a frequency for a duration in milliseconds.
Function Prototype:
void tone(uint16_t frequency)
void tone(uint16_t frequency, uint32_t duration)
Parameter | Type | Description |
---|---|---|
frequency | uint16_t | Speaker frequency |
duration | uint32_t | Duration of sound (ms) |
Usage Example:
#include <M5Stack.h>
void setup(){
M5.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { // If button A was pressed
M5.Speaker.tone(661, 200); // Set the speaker to sound at 661Hz for 200ms
}else if(M5.BtnB.wasPressed()){
M5.Speaker.tone(112); // Set the speaker to continuously sound at 112Hz
}else if(M5.BtnC.wasPressed()){
M5.Speaker.end(); // Turn off the speaker
}
delay(100);
}
Functionality:
Sets the volume.
Function Prototype:
void setVolume(uint8_t volume)
Parameter | Type | Description |
---|---|---|
volume | uint8_t | Volume (0~11) |
Usage Example:
#include <M5Stack.h>
char i = 0;
void setup(){
M5.begin();
M5.Speaker.begin();
}
void loop(){
M5.update();
if(M5.BtnA.wasPressed()) { // If button A was pressed
M5.Speaker.tone(661, 200); // Set the speaker to sound at 661Hz for 200ms
}else if(M5.BtnC.wasPressed()){
M5.Speaker.setVolume(i++); // Set the speaker volume
}
delay(100);
}