AnalogIn

An analog (ie. continuous) input unit that returns values between 0 and 1 (ie. 0% and 100%).

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 value is expressed as a percentage of the reference voltage (Vref, typically 5V)

  • in INVERTED mode the value is inverted (ie. 0V corresponds to 100% while 2.5V corresponds to 50%).

Warning

If the analog input pin is not connected to anything, the value returned by get() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

Example

Control an LED using a potentiometer.

#include <Plaquette.h>

AnalogIn potentiometer(A0);

AnalogOut led(9);

SineWave oscillator;

void begin() {}

void step() {
  // The analog input controls the frequency of the LED's oscillation.
  oscillator.frequency(potentiometer.mapTo(2.0, 10.0));
  oscillator >> led;
}

Reference

class AnalogIn : public Unit, public PinConfig, public Smoothable

A generic class representing a simple analog input.

Public Functions

AnalogIn(uint8_t pin, Engine &engine = Engine::primary())

Constructor.

Parameters
  • pin – the pin number

  • mode – the mode (DIRECT or INVERTED)

inline virtual float get()

Returns value in [0, 1].

virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

float read() const

Direcly reads value from the pin (bypasses mode, smoothing, and engine).

int rawRead() const

Directly reads raw value from the pin (bypasses mode, smoothing, and engine).

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 operator float()

Object can be used directly to access its value.

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 explicit operator bool()

Operator that allows usage in conditional expressions.

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 mode(uint8_t mode)

Changes the mode of the component.

inline virtual void smooth(float smoothTime = PLAQUETTE_DEFAULT_SMOOTH_WINDOW)

Apply smoothing to object.

inline virtual void noSmooth()

Remove smoothing.

inline virtual void timeWindow(float seconds)

Changes the smoothing window (expressed in seconds).

inline float timeWindow() const

Returns the smoothing window (expressed in seconds).

inline virtual void cutoff(float hz)

Changes the smoothing window cutoff frequency (expressed in Hz).

inline float cutoff() const

Returns the smoothing window cutoff frequency (expressed in Hz).

Public Static Functions

static inline bool analogToDigital(float f)

Converts analog (float) value to digital (bool) value.

static inline float digitalToAnalog(bool b)

Converts digital (bool) value to analog (float) value.

See Also