EasyManua.ls Logo

Freenove ESP32 - Page 97

Default Icon
159 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Need support? support@freenove.com
95
Chapter 8 Buzzer
www.freenove.com
20
21
22
23
24
25
26
27
28
29
#define MELODY_DHTG 13
#define MELODY_BB_CLEAR_1 14
#define MELODY_BB_CLEAR_2 15
……
void setupBuzzer();
void setBuzzer(uint16_t freq);
void setMelodyToPlay(int m);
void task_Buzzered(void *pvParameters);
#endif
Buzzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Buzzer.h"
#define BUZZER_PIN (33)
#define BUZZER_CHN (0)
#define BUZZER_BIT (10)
#define BUZZER_FREQ (5000)
int melody = -1;
void setupBuzzer() {
ledcSetup(BUZZER_CHN, BUZZER_FREQ, BUZZER_BIT); // setup pwm channel
ledcAttachPin(BUZZER_PIN, BUZZER_CHN);
ledcWrite(BUZZER_CHN, 0);
ledcWriteTone(BUZZER_CHN, 0);
}
void setBuzzer(uint16_t freq) {
if (freq != 0) {
ledcWrite(BUZZER_CHN, 512);
ledcWriteTone(BUZZER_CHN, freq);
} else {
ledcWrite(BUZZER_CHN, 0);
ledcWriteTone(BUZZER_CHN, 0);
}
}
template<int N> //Non-type template parameters
void playMelody(const int (&tune)[N], const float (&beat)[N], float speed) {
for (int i = 0; i < N; i++) {
setBuzzer(tune[i]);
delay(beat[i] * speed);
setBuzzer(0);

Related product manuals