Serial Wombat Arduino Library
Public Member Functions | List of all members
SerialWombatAnalogInput Class Reference

A class to make analog measurements with the Serial Wombat. More...

#include <serialWombatAnalogInput.h>

Public Member Functions

 SerialWombatAnalogInput (SerialWombat &SerialWombat)
 Constructor for the SerialWombatAnalogInput class. More...
 
void begin (uint8_t pin)
 Initialize an analog input on a given pin. More...
 
void begin (uint8_t pin, uint16_t averageSamples, uint16_t filterConstant)
 Initialize an analog input on a given pin. More...
 
uint16_t readVoltage_mV ()
 Retreive a raw A/D measurement and convert it to mV. More...
 
uint16_t readCounts ()
 Retreive a raw A/D measurement. More...
 
uint16_t readFiltered_mV ()
 Retreive a filtered A/D measurement and convert it to mV. More...
 
uint16_t readFilteredCounts ()
 Retreive a filtered A/D measurement. More...
 
uint16_t readAveraged_mV ()
 Retreive an averaged A/D measurement and convert it to mV. More...
 
uint16_t readAveragedCounts ()
 Retreive an averaged A/D measurement. More...
 
uint16_t updateSupplyVoltage_mV ()
 Provides a wrapper around the readSupplyVoltage_mV() method for the SerialWombat hosting this pin mode. More...
 
 ~SerialWombatAnalogInput ()
 

Detailed Description

A class to make analog measurements with the Serial Wombat.

The SerialWombatAnalogInput class is used to make measurements on a given pin.

Any analog-capable pin may be used to make a measurement.

Averaging of samples and first order IIR filtering (1 kHz sampling) of input are available

For a good explanation of 1st order FIR filter calculations, see: https://www.monocilindro.com/2017/04/08/how-to-implement-a-1st-order-iir-filter-in-5-minutes/

Some filter cut-off (3dB down) frequency and constant values:

Filtering adds lag. The higher the filter constant value, the longer it takes for the filter to settle when given a steady input.

Declare and initialize a SerialWombatAnalogInput instance for each pin being used as an analog input.

The SW4B_ard_analogInput example included with the Arduino library shows how to use this class.

A Tutorial video is also avaialble:

https://youtu.be/_EKlrEVaEhg

Constructor & Destructor Documentation

◆ SerialWombatAnalogInput()

SerialWombatAnalogInput::SerialWombatAnalogInput ( SerialWombat SerialWombat)

Constructor for the SerialWombatAnalogInput class.

Parameters
SerialWombata reference to the Serial Wombat on which the Analog Input will be measured

◆ ~SerialWombatAnalogInput()

SerialWombatAnalogInput::~SerialWombatAnalogInput ( )

Member Function Documentation

◆ begin() [1/2]

void SerialWombatAnalogInput::begin ( uint8_t  pin)

Initialize an analog input on a given pin.

This intialization turns on sample averaging to 64 samples and turns on first order IIR filtering with a cutoff frequency of 0.5 Hz

Parameters
pinThe Serial Wombat pin to set. Valid values for SW4A: 0-3 SW4B: 1-3

◆ begin() [2/2]

void SerialWombatAnalogInput::begin ( uint8_t  pin,
uint16_t  averageSamples,
uint16_t  filterConstant 
)

Initialize an analog input on a given pin.

Parameters
pinThe Serial Wombat pin to set. Valid values for SW4A: 0-3 SW4B: 1-3
averageSamplesNumber of samples to average. Valid values for SW4A and SW4B are 0 or 64. All non-zero values will be treated as 64 on these platforms.
filterConstantFirst Order IIR filter constant, expressed as 1/65536ths . Values closer to 65536 give heavier filtering. Sample frequency is 1kHz.

◆ readAveraged_mV()

uint16_t SerialWombatAnalogInput::readAveraged_mV ( )

Retreive an averaged A/D measurement and convert it to mV.

Conversion is based on the most recent averaged A/D result taken by the Serial Wombat at the command time and the last reference measurement made on the Serial Wombat using the SerialWombat.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ readAveragedCounts()

uint16_t SerialWombatAnalogInput::readAveragedCounts ( )

Retreive an averaged A/D measurement.

Conversion is based on the most recent averaged A/D value taken by the Serial Wombat at the command time.

All Serial Wombat products will return a 16-bit value. However the SW4A and SW4B products only have 10-bit A/D converters. Averaging will potentially increase the effective resolution slightly for signals that have a small amount of randomly distributed noise.

Returns
A 16 bit unsigned value indicating the counts of the A/D conversion

◆ readCounts()

uint16_t SerialWombatAnalogInput::readCounts ( )

Retreive a raw A/D measurement.

Conversion is based on the most recent A/D conversion taken by the Serial Wombat at the command time.

All Serial Wombat products will return a 16-bit value. However the SW4A and SW4B products only have 10-bit A/D converters, so the returned value moves by 64 counts at a time, except for the topmost value. For all Serial Wombat products, the highest possible reading (0xFFC0 for the SW4A/SW4B) is changed to 0xFFFF to indicate maximum possible hardware value.

Returns
A 16 bit unsigned value indicating the counts of the A/D conversion

◆ readFiltered_mV()

uint16_t SerialWombatAnalogInput::readFiltered_mV ( )

Retreive a filtered A/D measurement and convert it to mV.

Conversion is based on the most recent filtered A/D result taken by the Serial Wombat at the command time and the last reference measurement made on the Serial Wombat using the SerialWombat.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ readFilteredCounts()

uint16_t SerialWombatAnalogInput::readFilteredCounts ( )

Retreive a filtered A/D measurement.

Conversion is based on the most recent filtered A/D value taken by the Serial Wombat at the command time.

Returns
A 16 bit unsigned value indicating the filtered A/D result

◆ readVoltage_mV()

uint16_t SerialWombatAnalogInput::readVoltage_mV ( )

Retreive a raw A/D measurement and convert it to mV.

Conversion is based on the most recent A/D conversion taken by the Serial Wombat at the command time and the last reference measurement made on the Serial Wombat using the SerialWombat.readSupplyVoltage_mV() method.

Returns
A 16 bit unsigned value indicating measurement in mV

◆ updateSupplyVoltage_mV()

uint16_t SerialWombatAnalogInput::updateSupplyVoltage_mV ( )

Provides a wrapper around the readSupplyVoltage_mV() method for the SerialWombat hosting this pin mode.