Skip to content

ANALOG JOYSTICK SENSOR

Thomas E. Horner edited this page Mar 12, 2019 · 11 revisions

Specification

What Comments
Identifier ANALOG_JOYSTICK
Interface 2 ADC
1 GPIO
ADC 1: X signal
ADC 2: Y signal
GPIO : SW signal (switch)
Provides x -1: left movement
0: no movement
1: right movement
y -1: up movement
0: no movement
1: down movement
sw 0: switch released
1: switch pressed
Properties none
Callbacks? yes

Notes

  • Hardware pull-ups are not required for switch.

Code

-- Attach a joystick connected as
-- x external ADC (ADC1115) / channel 0
-- y external ADC (ADC1115) / channel 1
-- sw (switch) GPIO26
s = sensor.attach("ANALOG_JOYSTICK", adc.ADS1115, 0, adc.ADS1115, 1, pio.GPIO26)

-- Register a callback. Callback is executed when some sensor property changes.
s:callback(function(data)
  if (data.x == -1) then
    print("left")
  elseif (data.x == 1) then
    print("right")
  end

  if (data.y == -1) then
    print("up")
  elseif (data.y == 1) then
    print("down")
  end
  
  if (data.sw == 1) then
    print("sw on")
  elseif (data.sw == 0) then
    print("sw off")
  end
end)

Back to sensor list

Clone this wiki locally