Class Name: Speaker
Description:
Initializes the speaker.
Syntax:
void begin();
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);}
Description:
Stops the speaker.
Syntax:
void end();
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);}
Description:
Outputs the speaker's settings.
Syntax:
void update();
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);}
Description:
Sets the speaker to sound at a frequency for a duration in milliseconds.
Syntax:
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) |
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);}
Description:
Sets the volume.
Syntax:
void setVolume(uint8_t volume);
Parameter | Type | Description |
---|---|---|
volume | uint8_t | Volume (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 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);}