Arduino Quick Start
Air Quality buzzer related APIs and example program.
#include <M5Unified.h> #define BUZZER_PIN G9 // Note frequencies#define NOTE_C4 262#define NOTE_D4 294#define NOTE_E4 330#define NOTE_F4 349#define NOTE_G4 392#define NOTE_A4 440 // Melody and beat (first 14 notes of "Twinkle Twinkle Little Star")int melody[] = { NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4};int noteDurations[] = { 4,4,4,4,4,4,2, 4,4,4,4,4,4,2}; void setup() { // Initialize M5Unified M5.begin(); // Clear screen and set text properties M5.Display.clear(TFT_BLACK); M5.Display.setTextSize(2); M5.Display.setTextColor(TFT_WHITE, TFT_BLACK); // Display English title on the screen M5.Display.setCursor( 20, 20); M5.Display.print("Playing:"); M5.Display.setCursor( 20, 60); M5.Display.print("Twinkle Twinkle"); M5.Display.setCursor( 20, 100); M5.Display.print("Little Star"); pinMode(BUZZER_PIN, OUTPUT);} void loop() { int notes = sizeof(melody) / sizeof(melody[0]); for (int i = 0; i < notes; i++) { // Play current note int duration = 1000 / noteDurations[i]; tone(BUZZER_PIN, melody[i], duration); delay(duration * 1.2); noTone(BUZZER_PIN); M5.update(); } // After finishing, stay on last note. Uncomment to loop indefinitely:// while (true) {// delay(1000);// }}
After uploading, you will see the following effect, looping “Twinkle Twinkle Little Star”: