Advertisement
Guest User

SukBuzzer

a guest
Feb 20th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. class Buzzer {
  2. private:
  3. int _pin;
  4. bool _state;
  5. bool _tone_up_phase;
  6. int _current_tone;
  7. int _lower_tone;
  8. int _upper_tone;
  9. int _tone_change_step;
  10. public:
  11. Buzzer (int pin, int lower_tone = 1000, int upper_tone = 9000, int tone_change_step = 5){
  12. _pin = pin;
  13. _state = false;
  14. _tone_up_phase = true;
  15. _current_tone = 0;
  16. _lower_tone = lower_tone;
  17. _upper_tone = upper_tone;
  18. _tone_change_step = tone_change_step;
  19. }
  20. void set_state(bool state) {
  21. _state = state;
  22. if (state) {
  23. _current_tone = _lower_tone;
  24. _tone_up_phase = true;
  25. }
  26. }
  27. bool get_state() {
  28. return _state;
  29. }
  30. void update(){
  31. if (_state){
  32. if (_tone_up_phase) {
  33. if (_current_tone < _upper_tone){
  34. _current_tone += _tone_change_step;
  35. } else {
  36. _tone_up_phase = false;
  37. }
  38. } else {
  39. if (_current_tone > _lower_tone){
  40. _current_tone -= _tone_change_step;
  41. } else {
  42. _tone_up_phase = true;
  43. }
  44. }
  45. tone(_pin, _current_tone);
  46. } else {
  47. noTone(_pin);
  48. }
  49. }
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement