Skip to content

3-Position Analog Switch Component

Path: src/Pos3Analog.h

Revision History: Initial documentation

The Pos3Analog component provides functionality for a 3-position analog switch interface, supporting both local (hardware) and remote (Modbus) control. It reads the state of a physical switch through analog inputs and exposes the switch state via Modbus. The component can be configured to operate in local mode (reading physical inputs) or remote mode (controlled via Modbus).

  • Hardware:

    • Two analog input pins (upPin and downPin)
    • ESP-32 microcontroller
  • Software:

    • Platform.io build environment
    • C++17 compatible compiler
  • Reads a 3-position switch (UP, MIDDLE, DOWN) using two analog inputs
  • Supports two control modes: LOCAL (hardware) and REMOTE (Modbus)
  • Provides Modbus register interface for reading the current position
  • Allows remote control of the switch position
  • Configurable Modbus address for integration with industrial systems
  • Debounced analog input reading with configurable interval

Pos3Analog

Component

xmath

ModbusTCP

ArduinoLog

If ControlMode==LOCAL

If ControlMode==REMOTE

If position changed

If position changed

ModbusRequest

Write to Mode Register

Write to RemoteValue Register

Read from Value Register

If Remote Mode & value changed

Setup

Idle

ReadLocal

ReadRemote

UpdateState

NotifyChange

ProcessModbus

ChangeMode

ChangeRemoteValue

ReturnCurrentValue

  • Consider implementing hysteresis for analog readings to prevent flickering between states
  • Optimize the loop interval for different application scenarios to balance responsiveness and CPU usage
  • Add bounds checking on Modbus register access to prevent potential overflows
  • Consider adding a validation mechanism for state changes to prevent rapid oscillations
  • Ensure compliance with industrial Modbus protocol standards for wider compatibility
  • Document conformance to RS-485 electrical standards if used in that context
  • Use pull-up/pull-down resistors on the analog input pins for more reliable readings
  • Configure appropriate thresholds in config.h for the specific analog sensors being used
  • Implement error handling for the case of both UP and DOWN inputs being active
  • Consider adding a debounce mechanism for the switch position to prevent oscillation

This example demonstrates how to initialize and add a Pos3Analog component to your application:

#ifdef PIN_POS3_ANALOG_0_UP
pos3Analog_0 = new Pos3Analog(
this, // owner
PIN_POS3_ANALOG_0_UP, // upPin
PIN_POS3_ANALOG_0_DOWN,// downPin
ID_POS3_ANALOG_0, // id
POS3_ANALOG_0_MB_ADDR // modbusAddress
);
if (pos3Analog_0)
{
components.push_back(pos3Analog_0);
Log.infoln(F("Pos3Analog_0 initialized. UpPin:%d, DownPin:%d, ID:%d, MB:%d"),
PIN_POS3_ANALOG_0_UP, PIN_POS3_ANALOG_0_DOWN,
ID_POS3_ANALOG_0, POS3_ANALOG_0_MB_ADDR);
}
else
{
Log.errorln(F("Pos3Analog_0 initialization failed."));
}
#endif