Files
J1B3-Sensor-boxes/docs/LearningProcessBram/ArduinoExperience/classesArduino/buzzerClassDoc.md
2024-04-05 00:48:13 +02:00

2.0 KiB

Buzzer class

To provide a demonstration of how classes work in C++/Arduino, I decided to code something simple and repeatable.

This class is intended to assign an input and output source for a button to activate a buzzer, and for a buzzer to be activated by a button. This is achieved by using private variables and public functions that access these variables.

This class code allows the user to easily add new buzzers by simply creating an object and calling the appropriate function.

constructor

Inside the class, a constructor is defined. In C++, this constructor shares the same name as the class itself. Every time the class is called, the constructor is activated with the given data.

Class functions

Within a class, functions can be defined. These functions can interact with the variables made inside the class. These functions can then be called outside the class to execute their action.

In this case, the "activation" function is defined within the class and is used in the loop function. This function contains the if-statement that determines if the button is pressed and activates the buzzer pin accordingly.

Object Creation

After defining the class, an object needs to be made. This object will immediately trigger the constructor and ask for input variables. These given data points will be inserted into the private variables.

In this instance, the buzzer pin is set to 12 and the button pin to 20.

using the class

In the loop function, the new object is placed along with the function defined in the class to activate it. This function executes with the variable data provided in the constructor. Here, it checks if the specified pin is receiving a signal and then only outputs a signal to the buzzer when there is one.

(https://gitlab.fdmci.hva.nl/propedeuse-hbo-ict/onderwijs/2023-2024/out-a-se-ti/blok-3/qaajeeqiinii59/-/blob/main/docs/LearningProcessBram/ArduinoExperience/classesArduino/buzzerclasses.ino?ref_type=heads)