Un joystick si Raspberry Pi Pico

Impreuna cu Raspberry Pi Pico putem folosi si un joystick. Acesta se conecteaza destul de simplu, modul de conectare fiind precum mai jos:

Joystick      ---         Raspberry

VCC            -           3V3(OUT) (pin 36)

GND           -           GND  (pin 38)

VRx            -           GPIO26/ADC0 (pin31)

VRy            -           GPIO27/ADC1 (pin 32)

SW             -           GPIO16 (pin 21)

Pentru editarea si incarcarea codului sursa vom folosi editorul Thonny iar codul sursa folosit pentru testare este cel de mai jos:

from machine import Pin, ADC
import utime

axaX = ADC(Pin(27))
axaY = ADC(Pin(26))
buton = Pin(16, Pin.IN, Pin.PULL_UP)

while True:
    val_axaX = axaX.read_u16()
    val_axaY = axaY.read_u16()
    val_buton = buton.value()
    print(str(val_axaX) +", "+str(val_axaY) + " -- " +str(val_buton))
    utime.sleep(0.1)
    

Un cod putin mai avansat este cel de mai jos:

from machine import Pin, ADC
import utime

axaX = ADC(Pin(27))
axaY = ADC(Pin(26))
buton = Pin(16, Pin.IN, Pin.PULL_UP)
status_X = ""
status_Y = ""
status_buton = ""
while True:
    val_axaX = axaX.read_u16()
    val_axaY = axaY.read_u16()
    val_buton = buton.value()
    if val_axaX <= 600:
        status_X = "stanga"
    elif val_axaX >= 60000:
        status_X = "dreapta"
    if val_axaY <= 600:
        status_Y = "jos"
    elif val_axaY >= 60000:
        status_Y = "sus"
    if val_buton == 0:
        status_buton = "buton apasat"
    else:
        status_buton = ""
    print("Axa X: ", status_X, "Axa Y: ", status_Y, " ", status_buton)
    utime.sleep(0.1)
   

 

Nu sunt comentarii in “Un joystick si Raspberry Pi Pico”

Lasa un comentariu

Raspuns lui Some User

Autentificare