58 lines
3.7 KiB
Markdown
58 lines
3.7 KiB
Markdown
### 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.
|
|
|
|
##### Link to code
|
|
(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)
|
|
|
|
### The board
|
|
The fysical board should look something like this:
|
|

|
|
The board is simple and easy to recreate and has lots of space left for other functions.
|
|
|
|
By making this class, the addition of more buzzers is very easy. As long as there are pins, there can be a buzzer.
|
|
|
|
For this example, I have only used one, but adding more would be no problem. Just make a object, give the pin numbers and call the function.
|
|
|
|
The wireframe for this board would looke something like this:
|
|

|
|
|
|
I tried making it as clear as possible by moving some parts around and clearing some intertwining wires.
|
|
|
|
###### My Fysical Recreation.
|
|
To properly demonstrate this code in action, I recreated the board. In the image, you can see that I plugged in three different LEDs.
|
|

|
|
In the image, you can see that I plugged in three different LEDs.
|
|
This was done to show off that the board/code works, as it's not possible to see if a buzzer is working on camera.
|
|
|
|
For the time being, pretend that the LED and resistor combinations are separate buzzers.
|
|
|
|
To make this board turn all the buzzers/LEDs on, I needed to create three different objects. These objects looked like this:
|
|

|
|
The first variable is where I want the buzzer to connect, and the second variable is where I want the button to be connected.
|
|
|
|
After this, I simply placed the objects in the loop and called the function we made in the class:
|
|

|
|
|
|
Now, we simply push the code to The Esp and try it out.
|
|

|
|
Now, we have effectively made the LEDs/buzzers turn on with the push of a single button. |