StreamIn

An input unit that can receive values transmitted through a stream – for example, the Arduino serial line. Values are sent in clear text and separated by newlines and/or carriage returns.

Example

Controls the value of a LED using serial. Try opening the serial monitor and sending values between 0 and 1.

#include <Plaquette.h>

StreamIn serialIn(Serial);

AnalogOut led(9);

void begin() {}

void step() {
  serialIn >> led;
}

To run this example:

  1. Upload the code.

  2. In the Arduino software open the serial monitor: Tools > Serial Monitor.

  3. Make sure the default baudrate of 9600 bps is selected.

  4. Make sure one of the options “Newline”, “Carriage return”, or “Both NL + CR” is selected.

  5. Write a number between 0.0 and 1.0 and press “Enter”. This should allow you to set the LED intensity.

  6. Try different values.

Reference

class StreamIn : public AnalogSource

Stream/serial input. Reads float values using Arduino built-in parseFloat().

Public Functions

StreamIn(Engine &engine = Engine::primary())

Default constructor.

Parameters

engine – the engine running this unit

StreamIn(Stream &stream, Engine &engine = Engine::primary())

Constructor.

Parameters
  • stream – a reference to a Stream object

  • engine – the engine running this unit

inline virtual float mapTo(float toLow, float toHigh)

Maps value to new range.

inline virtual bool updated()

Returns true iff value was changed.

inline virtual void onUpdate(EventCallback callback)

Registers event callback on finish event.

inline virtual float get()

Returns value in [0, 1].

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.

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