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 PinConfig, public Debounceable
A generic class representing a simple digital input.
Public Functions
-
DigitalIn(uint8_t pin, Engine &engine = Engine::primary())
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.
-
float read() const
Directly reads value from the pin as 1 or 0 (bypasses mode, debounce, and engine).
-
int rawRead() const
Directly reads raw value from the pin as HIGH or LOW (bypasses mode, debounce, and engine).
-
inline virtual bool isOn()
Returns true iff the input is “on”.
-
inline virtual bool putOn(bool value)
Pushes value into the unit.
- Parameters
value – the value sent to the unit
- Returns
the new value of the unit
-
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 bool toggle()
Switches between on and off.
-
inline virtual int8_t changeState()
Difference between current and previous value of the unit.
-
inline virtual void onRise(EventCallback callback)
Registers event callback on rise event.
-
inline virtual void onFall(EventCallback callback)
Registers event callback on fall event.
-
inline virtual void onChange(EventCallback callback)
Registers event callback on change event.
-
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 virtual bool on()
Sets output to “on” (ie. true, 1).
-
inline virtual bool off()
Sets output to “off” (ie. false, 0).
-
inline virtual float put(float value)
Pushes value into the unit.
- Parameters
value – the value sent to the unit
- Returns
the new value of the unit
-
inline virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
-
inline operator bool()
Operator that allows usage in conditional expressions.
-
inline float seconds() const
Returns engine time in seconds.
-
inline uint32_t milliSeconds() const
Returns engine time in milliseconds.
-
inline uint64_t microSeconds() const
Returns engine time in microseconds.
-
inline unsigned long nSteps() const
Returns number of engine steps.
-
inline float sampleRate() const
Returns engine sample rate.
-
inline float samplePeriod() const
Returns enginesample period.
-
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 virtual void smooth(float smoothTime = PLAQUETTE_DEFAULT_DEBOUNCE_WINDOW)
Deprecated. Left for backwards compatibility.
-
inline virtual void noSmooth()
Remove smoothing.
-
inline virtual void timeWindow(float seconds)
Changes the debouncing window (expressed in seconds).
-
inline float timeWindow() const
Returns the debouncing window (expressed in seconds).
-
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, Engine &engine = Engine::primary())