Button
In this file we will explore the button.h and the button.cpp file
Grinduino/
├── lib/
│ └── button/
│ | └── button.h
| | └──button.cpp
Overview
This header file defines a ButtonClass that manages the button's functionality. The class is designed to detect button presses and handle the initialization of a grinding process.
Class Definition
ButtonClass
The ButtonClass is responsible for managing a button connected to a specific pin on the Arduino board. It provides methods to check the button's current state and detect changes in its state.
Public Methods
-
ButtonClass(int pin)- Constructor that initializes a new ButtonClass object.
- Parameter:
pin- The Arduino pin number to which the button is connected.
-
bool isPressed()- Checks if the button is currently pressed.
- Returns:
trueif the button is pressed,falseotherwise.
-
bool wasPressed()- Detects if the button was pressed since the last check.
- Returns:
trueif the button was pressed,falseotherwise.
-
void begin()- Initializes the button by setting up the pin and initial state.
Private Members
-
int _pin- Stores the Arduino pin number to which the button is connected.
-
int _lastState- Keeps track of the button's previous state.
-
int _currentState- Stores the button's current state.
Global Instance
The header file also declares an external global instance of the ButtonClass named Button. This allows easy access to a single button object throughout the project.
Usage
To use this ButtonClass in your project:
- Include this header file in your main Arduino sketch.
- Use the global
Buttonobject to interact with the button. - Call
Button.begin()in yoursetup()function to initialize the button. - Use
Button.isPressed()orButton.wasPressed()in yourloop()function to check the button's state and trigger actions accordingly.
Example:
void setup() {
Button.begin();
}
void loop() {
if (Button.wasPressed()) {
// Start the grinding process
}
}