Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Buzzer {
- private:
- int _pin;
- bool _state;
- bool _tone_up_phase;
- int _current_tone;
- int _lower_tone;
- int _upper_tone;
- int _tone_change_step;
- public:
- Buzzer (int pin, int lower_tone = 1000, int upper_tone = 9000, int tone_change_step = 5){
- _pin = pin;
- _state = false;
- _tone_up_phase = true;
- _current_tone = 0;
- _lower_tone = lower_tone;
- _upper_tone = upper_tone;
- _tone_change_step = tone_change_step;
- }
- void set_state(bool state) {
- _state = state;
- if (state) {
- _current_tone = _lower_tone;
- _tone_up_phase = true;
- }
- }
- bool get_state() {
- return _state;
- }
- void update(){
- if (_state){
- if (_tone_up_phase) {
- if (_current_tone < _upper_tone){
- _current_tone += _tone_change_step;
- } else {
- _tone_up_phase = false;
- }
- } else {
- if (_current_tone > _lower_tone){
- _current_tone -= _tone_change_step;
- } else {
- _tone_up_phase = true;
- }
- }
- tone(_pin, _current_tone);
- } else {
- noTone(_pin);
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement