Created
May 2, 2022 11:59
-
-
Save dj1711572002/a7aeab039ecce7730c4738b8b1266ca7 to your computer and use it in GitHub Desktop.
M5Stack Passive Buzzer UNIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include<M5Core2.h> | |
#define buzzer_pin 26 | |
int freq = 4000;//音の高さ | |
int ledChannel = 0; | |
int resolution = 10; | |
int vol=512;//Volume 0-1024(10it) | |
int i,j,k; | |
int t0,tb,tw; | |
uint8_t flags; | |
void setup() { | |
M5.begin(); | |
vol=512; | |
ledcSetup(ledChannel, freq, resolution); //PWM CH0,Freuency4000,10Bit | |
ledcAttachPin(buzzer_pin, ledChannel); //PWM Pin26,PWM CH0 | |
ledcWrite(ledChannel, vol); //PWM Duty | |
t0=millis(); | |
tb=30; | |
tw=2000; | |
} | |
void loop() { | |
//Serial.printf("t0=%d, millis=%d\n\r",t0,millis()); | |
if(millis()-t0>10000){ | |
i++; | |
i=i%3; | |
t0=millis(); | |
if(i==0){ | |
flags=131; | |
vol=512; | |
} | |
if(i==1){ | |
flags=67; | |
vol=512; | |
} | |
if(i==2){ | |
flags=1; | |
vol=512; | |
} | |
} | |
Abeep(flags,vol); | |
} | |
//ブザーSub (flag値,周波数,Duty,駆動時間,休止時間) | |
void Abeep(uint8_t flag,int vol){ | |
if (flag<67){ | |
flag=1; | |
} | |
switch(flag) { | |
case 131: | |
freq= int(3000); | |
ledcSetup(ledChannel, freq, resolution); //PWM CH0,Freuency4000,10Bit | |
tb=20; | |
tw=1000; | |
Serial.printf("flag=%d,freq=%d,vol=%d\n\r",flag,freq,vol); | |
break; | |
case 67: | |
freq= int(1000); | |
ledcSetup(ledChannel, freq, resolution); //PWM CH0,Freuency4000,10Bit | |
tb=400; | |
tw=1000; | |
Serial.printf("flag=%d,freq=%d,vol=%d\n\r",flag,freq,vol); | |
break; | |
case 1: | |
freq= int(200); | |
ledcSetup(ledChannel, freq, resolution); //PWM CH0,Freuency4000,10Bit | |
tb=700; | |
tw=1000; | |
Serial.printf("flag=%d,freq=%d,vol=%d\n\r",flag,freq,vol); | |
break; | |
} | |
//===========Beep Action=================================== | |
ledcWrite(0, vol); //Output PWM START at Volume vol | |
delay(tb); | |
ledcWrite(0, 0); //Output PWM. STOP at Volume 0 | |
delay(tw); | |
//========================================================== | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment