Skip to content

Joystick 4-Position Component

Path: src/components/Joystick.cpp

Revision History: Initial documentation

The Joystick component provides an interface for a 4-position joystick (up, down, left, right) with support for both local physical operation and remote control via Modbus. It features position debouncing for reliable readings, position state tracking, and seamless switching between local and remote operation modes.

  • Four GPIO pins for joystick inputs (up, down, left, right)
  • Pull-up resistors (internal or external) for each input
  • Modbus support for remote operation
  • 5 distinct positions: UP, DOWN, LEFT, RIGHT, and CENTER
  • Configurable debouncing for reliable position readings
  • Position state tracking with timing information
  • Switchable operation modes:
    • LOCAL: reads physical joystick position
    • REMOTE: accepts position commands via Modbus
  • Complete Modbus integration
  • Position holding time tracking

Joystick

Component

ModbusTCP

ArduinoLog

setMode(REMOTE)

useDebouncing true

useDebouncing false

confirmCount >= threshold

position changed

setMode(LOCAL)

setOverridePosition()

Setup

LocalMode

RemoteMode

ReadPosition

Debouncing

UpdatePosition

NotifyChange

OverridePosition

  • Consider optimizing the debouncing algorithm for specific application needs
  • Evaluate if READ_INTERVAL_MS (25ms) can be adjusted based on application requirements
  • Implement input validation for Modbus commands
  • Consider adding authentication for remote mode switching
  • Ensure compliance with relevant industrial control standards
  • Review electrical safety requirements for the connected joystick hardware
  • Use hardware debouncing circuits when possible to reduce CPU load
  • Implement additional position combinations if needed (e.g., diagonal positions)
  • Consider adding filter capacitors on input pins for noisy environments
#ifdef PIN_JOYSTICK_UP
joystick = new Joystick(
this, // owner
PIN_JOYSTICK_UP, // pinUp
PIN_JOYSTICK_DOWN, // pinDown
PIN_JOYSTICK_LEFT, // pinLeft
PIN_JOYSTICK_RIGHT, // pinRight
JOYSTICK_MB_ADDR // modbusAddress
);
if (joystick) {
components.push_back(joystick);
Log.infoln(F("Joystick initialized. Pins - Up:%d Down:%d Left:%d Right:%d, MB:%d"),
PIN_JOYSTICK_UP, PIN_JOYSTICK_DOWN,
PIN_JOYSTICK_LEFT, PIN_JOYSTICK_RIGHT,
JOYSTICK_MB_ADDR);
} else {
Log.errorln(F("Joystick initialization failed."));
}
#endif