buzzer.cpp 770 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <Arduino.h>
  2. #include "buzzer.h"
  3. #define PIN_BUZZER 18
  4. #define PWM_CHANNEL_BUZZER 0
  5. #define RESOLUTION_BUZZER 8
  6. void buzzer_setup() {
  7. ledcSetup(PWM_CHANNEL_BUZZER, 2000, RESOLUTION_BUZZER);
  8. ledcAttachPin(PIN_BUZZER, PWM_CHANNEL_BUZZER);
  9. ledcWrite(PWM_CHANNEL_BUZZER, 128);
  10. }
  11. void buzz() {
  12. ledcWriteTone(PWM_CHANNEL_BUZZER, 300);
  13. delay(800);
  14. ledcWriteTone(PWM_CHANNEL_BUZZER, 0);
  15. }
  16. void beep() {
  17. ledcWriteTone(PWM_CHANNEL_BUZZER, 2000);
  18. delay(200);
  19. ledcWriteTone(PWM_CHANNEL_BUZZER, 0);
  20. }
  21. void test_buzzer() {
  22. for (int freq = 255; freq < 10000; freq = freq + 250){
  23. Serial.print("Testing frequency: "); Serial.println(freq);
  24. ledcWriteTone(PWM_CHANNEL_BUZZER, freq);
  25. delay(1000);
  26. }
  27. }