Loading...
Searching...
No Matches
AnalogShield Class Reference

Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield. More...

#include <AnalogShield.h>

Public Types

using EncoderCallback = void (*)(int32_t count, int8_t step)
 Called on every encoder step.
using SwitchCallback = void (*)(bool pressed)
 Called when the encoder switch is pressed or released.

Public Member Functions

void begin ()
 Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
void end ()
 Stops tracking the encoder and its switch.
int32_t EncoderCount () const noexcept
 Encoder steps since begin() or the last SetEncoderCount().
int32_t EncoderDetents () const noexcept
 Encoder clicks (detents) since begin() or the last SetEncoderCount().
void SetEncoderCount (int32_t count=0) noexcept
 Sets the encoder count, for example to zero it at a home position.
void SetEncoderCallback (EncoderCallback callback) noexcept
 Sets the function called on every encoder step.
bool SwitchPressed () const noexcept
 Reads the encoder switch.
void SetSwitchCallback (SwitchCallback callback) noexcept
 Sets the function called when the encoder switch is pressed or released.
bool SetAdcResolution (uint8_t bits)
 Sets the ADC resolution used by analogRead() and by every reading below.
uint8_t AdcResolution () const noexcept
 ADC resolution set by SetAdcResolution().
void LedON (uint8_t index, bool on=true) noexcept
 Turns an LED on or off.
void LedToggle (uint8_t index) noexcept
 Toggles an LED.
Readings

Every reading takes an optional samples: the number of ADC reads to average (1 to 255, 0 counts as 1). More samples give a steadier value.

int TempRaw (TempSource source, uint8_t samples=1) const
 Reads a temperature signal with the ADC.
int32_t TempMillivolts (TempSource source, uint8_t samples=1) const
 Reads the voltage of a temperature signal.
float TempCelsius (TempSource source, uint8_t samples=1) const
 Reads a temperature signal and converts it to degrees Celsius.
float TempFahrenheit (TempSource source, uint8_t samples=1) const
 Reads a temperature signal and converts it to degrees Fahrenheit.
float TempKelvin (TempSource source, uint8_t samples=1) const
 Reads a temperature signal and converts it to kelvins.
int PotRaw (uint8_t samples=1) const
 Reads the potentiometer RV1 with the ADC.
int32_t PotMillivolts (uint8_t samples=1) const
 Reads the potentiometer wiper voltage.
float PotPercent (uint8_t samples=1) const
 Reads the potentiometer position.

Static Public Attributes

Encoder resolution (PEC11L-4220F-S0015: 15 pulses, 30 detents per turn)
static constexpr int32_t COUNTS_PER_DETENT = 2
 EncoderCount() steps per click.
static constexpr int32_t DETENTS_PER_REV = 30
 Clicks per full turn.

Detailed Description

Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.

Create one AnalogShield and call begin() in setup(). The encoder and its switch are tracked with pin interrupts from then on.

Examples
EncoderInterrupt.ino, EncoderPolling.ino, LedFade.ino, SerialPlotter.ino, and Temperature.ino.

Member Typedef Documentation

◆ EncoderCallback

using AnalogShield::EncoderCallback = void (*)(int32_t count, int8_t step)

Called on every encoder step.

Parameters
countNew encoder count, as returned by EncoderCount().
step+1 when A leads B, -1 when B leads A.
Warning
Runs inside the interrupt: keep it short, no Serial or delay().

◆ SwitchCallback

using AnalogShield::SwitchCallback = void (*)(bool pressed)

Called when the encoder switch is pressed or released.

Parameters
pressedtrue on press, false on release.
Warning
Runs inside the interrupt: keep it short, no Serial or delay().

Member Function Documentation

◆ AdcResolution()

uint8_t AnalogShield::AdcResolution ( ) const
nodiscardnoexcept

ADC resolution set by SetAdcResolution().

Returns
Bits, 10 until SetAdcResolution() is called.

◆ begin()

void AnalogShield::begin ( )

Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.

Resets the encoder count to 0. Callbacks already set are kept.

Examples
EncoderInterrupt.ino, EncoderPolling.ino, LedFade.ino, SerialPlotter.ino, and Temperature.ino.

◆ EncoderCount()

int32_t AnalogShield::EncoderCount ( ) const
nodiscardnoexcept

Encoder steps since begin() or the last SetEncoderCount().

Returns
Count, 4 per quadrature cycle, positive when A leads B.
Examples
EncoderInterrupt.ino, EncoderPolling.ino, and SerialPlotter.ino.

◆ EncoderDetents()

int32_t AnalogShield::EncoderDetents ( ) const
nodiscardnoexcept

Encoder clicks (detents) since begin() or the last SetEncoderCount().

Returns
EncoderCount() / COUNTS_PER_DETENT, rounded toward zero, so the value changes once per click.

◆ LedON()

void AnalogShield::LedON ( uint8_t index,
bool on = true )
noexcept

Turns an LED on or off.

Parameters
indexAnalogShieldPins::LED1 or AnalogShieldPins::LED2; other pins are ignored.
ontrue to turn the LED on, false to turn it off.
Examples
EncoderInterrupt.ino, EncoderPolling.ino, and SerialPlotter.ino.

◆ LedToggle()

void AnalogShield::LedToggle ( uint8_t index)
noexcept

Toggles an LED.

Parameters
indexAnalogShieldPins::LED1 or AnalogShieldPins::LED2; other pins are ignored.
Examples
EncoderInterrupt.ino, and EncoderPolling.ino.

◆ PotMillivolts()

int32_t AnalogShield::PotMillivolts ( uint8_t samples = 1) const
nodiscard

Reads the potentiometer wiper voltage.

Parameters
samplesADC reads to average.
Returns
Voltage in mV. On the Phyduino Pico this uses the pin's own full scale (3.3 V on A0-A2, 2.048 V on A3-A5); other boards assume 3.3 V (JP3 at 3.3 V).
Examples
SerialPlotter.ino.

◆ PotPercent()

float AnalogShield::PotPercent ( uint8_t samples = 1) const
nodiscard

Reads the potentiometer position.

Parameters
samplesADC reads to average.
Returns
0.0 at one end of travel to 100.0 at the other (0 V to VDD).

◆ PotRaw()

int AnalogShield::PotRaw ( uint8_t samples = 1) const
nodiscard

Reads the potentiometer RV1 with the ADC.

Parameters
samplesADC reads to average.
Returns
ADC reading, 0 to 2^AdcResolution() - 1.

◆ SetAdcResolution()

bool AnalogShield::SetAdcResolution ( uint8_t bits)

Sets the ADC resolution used by analogRead() and by every reading below.

Use this rather than calling analogReadResolution() directly, so the millivolt and temperature conversions use the same full scale.

Parameters
bits8 to 12. The RP2040/RP2350 ADC is 12-bit; the Arduino default is 10.
Returns
false, and no change, if bits is out of range or the board cannot change resolution (Uno R3 stays at 10 bits).
Examples
SerialPlotter.ino, and Temperature.ino.

◆ SetEncoderCallback()

void AnalogShield::SetEncoderCallback ( EncoderCallback callback)
noexcept

Sets the function called on every encoder step.

Parameters
callbackFunction to call, or nullptr to remove it.
Examples
EncoderInterrupt.ino.

◆ SetEncoderCount()

void AnalogShield::SetEncoderCount ( int32_t count = 0)
noexcept

Sets the encoder count, for example to zero it at a home position.

Parameters
countNew count, in EncoderCount() steps. Leave out to reset to 0.

◆ SetSwitchCallback()

void AnalogShield::SetSwitchCallback ( SwitchCallback callback)
noexcept

Sets the function called when the encoder switch is pressed or released.

Parameters
callbackFunction to call, or nullptr to remove it.
Examples
EncoderInterrupt.ino, and SerialPlotter.ino.

◆ SwitchPressed()

bool AnalogShield::SwitchPressed ( ) const
nodiscardnoexcept

Reads the encoder switch.

Returns
true while the switch is held down.
Examples
EncoderPolling.ino.

◆ TempCelsius()

float AnalogShield::TempCelsius ( TempSource source,
uint8_t samples = 1 ) const
nodiscard

Reads a temperature signal and converts it to degrees Celsius.

Undoes the shield's signal path for source (the R15 / 10K divider, with the Phyduino Pico's 100 kΩ A3 pull-down in parallel with R15 for EXT, then the conditioning stage Vout = 4.3 Vin - 0.638 V), then applies the LMT84 parabolic transfer function (datasheet Equation 2). Accurate over the sensor's -50 to 150 °C range; outside it the result is extrapolated.

Parameters
sourceSignal to read.
samplesADC reads to average.
Returns
Temperature in °C, with the ADC full scale as for TempMillivolts().
Note
Over the sensor range TempSource::MCU spans 61.7 mV to 2.039 V and TempSource::EXT, loaded by the 100 kΩ pull-down, 58.8 mV to 1.954 V, so both read the full range: EXT is within the MAX11635's 2.048 V on the Phyduino Pico. Above a 1.3 V sensor input (JP1 at External, J1) they clamp, MCU at about 2.05 V and EXT at about 1.97 V.
Examples
SerialPlotter.ino, and Temperature.ino.

◆ TempFahrenheit()

float AnalogShield::TempFahrenheit ( TempSource source,
uint8_t samples = 1 ) const
nodiscard

Reads a temperature signal and converts it to degrees Fahrenheit.

Parameters
sourceSignal to read.
samplesADC reads to average.
Returns
Temperature in °F; same assumptions and limits as TempCelsius().
Examples
Temperature.ino.

◆ TempKelvin()

float AnalogShield::TempKelvin ( TempSource source,
uint8_t samples = 1 ) const
nodiscard

Reads a temperature signal and converts it to kelvins.

Parameters
sourceSignal to read.
samplesADC reads to average.
Returns
Temperature in K; same assumptions and limits as TempCelsius().
Examples
Temperature.ino.

◆ TempMillivolts()

int32_t AnalogShield::TempMillivolts ( TempSource source,
uint8_t samples = 1 ) const
nodiscard

Reads the voltage of a temperature signal.

Parameters
sourceSignal to read.
samplesADC reads to average.
Returns
Voltage in mV. On the Phyduino Pico this uses the pin's own full scale (3.3 V on A0-A2, 2.048 V on A3-A5); other boards assume 3.3 V (JP3 at 3.3 V).
Examples
Temperature.ino.

◆ TempRaw()

int AnalogShield::TempRaw ( TempSource source,
uint8_t samples = 1 ) const
nodiscard

Reads a temperature signal with the ADC.

Parameters
sourceSignal to read.
samplesADC reads to average.
Returns
ADC reading, 0 to 2^AdcResolution() - 1 (1023 at the default 10 bits).
Examples
Temperature.ino.

The documentation for this class was generated from the following files: