DigitalIn
A digital (ie. binary) input unit that can be either “on” or “off”.
The unit is assigned to a specific pin
on the board.
The mode
specifies the behavior of the component attached to the pin:
in
DIRECT
mode (default) the unit will be “on” when the voltage on the pin is high (Vref, typically 5V)in
INVERTED
mode the unit will be “on” when the voltage on the pin is low (GND)in
INTERNAL_PULLUP
mode the internal pullup resistor is used, simplifying usage of switches and buttons
Debouncing
Some digital inputs such as push-buttons often generate spurious open/close transitions when pressed, due to mechanical and physical issues: these transitions called “bouncing” may be read as multiple presses in a very short time, fooling the program.
The DigitalIn object features debouncing capabilities which can prevent this kind of problems. Debouncing
can be achieved using different modes: stable (default) (DEBOUNCE_STABLE
), lock-out (DEBOUNCE_LOCK_OUT
)
and prompt-detect (DEBOUNCE_PROMPT_DETECT
). For more information please refer to the documentation
of the Bounce2 Arduino Library.
Example
Turns on and off a light emitting diode (LED) connected to digital pin 13, when pressing a pushbutton attached to digital pin 2. Pushbutton should be wired by connecting one side to pin 2 and the other to ground.
#include <Plaquette.h>
DigitalIn button(2, INTERNAL_PULLUP);
DigitalOut led(13);
void begin() {
button.debounce(); // debounce button
}
void step() {
// Toggle the LED each time the button is pressed.
if (button.rose())
led.toggle();
}
Reference
-
class DigitalIn : public DigitalSource, public PinUnit, public Debounceable
A generic class representing a simple digital input.
Public Functions
-
DigitalIn(uint8_t pin, uint8_t mode = DIRECT)
Constructor.
- Parameters:
pin – the pin number
mode – the mode (DIRECT, INVERTED, or INTERNAL_PULLUP)
-
virtual void mode(uint8_t mode)
Changes the mode of the component.
-
inline virtual bool isOn()
Returns true iff the input is “on”.
-
inline virtual bool rose()
Returns true if the value rose.
-
inline virtual bool fell()
Returns true if the value fell.
-
inline virtual bool changed()
Returns true if the value changed.
-
inline virtual int8_t changeState()
Difference between current and previous value of the unit.
-
inline virtual bool isOff()
Returns true iff the input is “off”.
-
inline virtual int getInt()
Returns value as integer (0 or 1).
-
inline virtual float get()
Returns value as float (either 0.0 or 1.0).
-
inline uint8_t pin() const
Returns the pin this component is attached to.
-
inline uint8_t mode() const
Returns the mode of the component.
-
inline virtual void debounce(float debounceTime = PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Apply smoothing to object.
-
inline virtual void noDebounce()
Remove smoothing.
-
inline uint8_t debounceMode() const
Returns the debounce mode.
-
inline void debounceMode(uint8_t mode)
Sets debounce mode.
- Parameters:
mode – the debounce mode (DEBOUNCE_DEFAULT, DEBOUNCE_LOCK_OUT or DEBOUNCE_PROMPT_DETECT)
-
DigitalIn(uint8_t pin, uint8_t mode = DIRECT)