Menu

JoyStick

Perrotti

Class JoyStick

Class to drive a joystick module.

Public declarations:

class JoyStick{
public:  
  JoyStick();

  void begin(byte xAxisPin, byte yAxisPin, byte zButtonPin);
  void axisConfig(int normalizeTo, byte tolerance, boolean zIgnoreXY=false);
  void run();
  int readXAxis();
  int readYAxis();
  boolean readZButton();

protected:
  virtual void onZButtonPressed(){}
  virtual void onZButtonReleased(){}
  virtual void onXAxisChange(int xVal){}
  virtual void onYAxisChange(int yVal){}
};
How to use

Call begin() before any other method informing arduino's pins.
You can configurate the readings by calling axisConfig() method.
Use readXAxis() and readYAxis() to read axis values and readZButton() to read button Z status.

If you prefer to work with events, derive JoyStick class and rewrite them. In this case, dont forget to call run() method at least once per loop.


void begin(byte xAxisPin, byte yAxisPin, byte zButtonPin)

Call before any other method. Read axes values to use as reference. This references are considered the origin of axis (zero). Lower readings return negative values.

xAxisPin: x axis pin (analog).
yAxisPin: y axis pin (analog).
zButtonPin: button z pin (digital).


void axisConfig(int normalizeTo, byte tolerance, boolean zIgnoreXY=false)

Configure the axes readings. By default, readings of XY axes return values beetwen -512 and 512 (approximately). The normalizeTo parameter make readings to be mapped to informed value. For example, if it is 100, the readings will return values between -100 and 100.

normalizeTo: value to normalize readings of axes.
tolerance: minimum variation of the axes to trigger events (after normalized).
zIgnoreXY: ignore XY axes events when button Z is pressed.


void run()

Reads axes and button status and triggers events if needed. If you use events, this method must be called at least once per loop.


int readXAxis()

Read and return the x axis value. The returned value can be normalized if axisConfig() method was called.

return: x axis value normalized.


int readYAxis()

Read and return the y axis value. The returned value can be normalized if axisConfig() method was called.

return: y axis value normalized.


boolean readZButton()

Read Z button status.

return: true if Z button is pressed , false if not.


virtual void onZButtonPressed()

Event triggered when Z button is pressed.
Events are triggered by run() method, so to work with events you must call run() at least once per loop.


virtual void onZButtonReleased()

Event triggered when Z button is released.
Events are triggered by run() method, so to work with events you must call run() at least once per loop.


virtual void onXAxisChange(int xVal)

Event triggered when X axis value change at least the tolerance informed in axisConfig() method (default=1).
Events are triggered by run() method, so to work with events you must call run() at least once per loop.

  • xVal: X axis value.

virtual void onYAxisChange(int xVal)

Event triggered when Y axis value change at least the tolerance informed in axisConfig() method (default=1).
Events are triggered by run() method, so to work with events you must call run() at least once per loop.

  • yVal: Y axis value.

Related

Wiki: Home
Wiki: JoyStickShield
Wiki: JsRadioMaster
Wiki: JsRadioSlave
Wiki: PushButton

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.