Found a Dual-axis joystick PS2 game joystick control stick sensor on Taobao View description: The cross joystick is a bidirectional 10K resistor. The resistance of the tap changes with the direction of the joystick. This module uses 5V power supply. In the original state, the X, Y read voltage is about 2.5V. When pressed in the direction of the arrow , the read voltage value increases, the maximum is 5V; press the arrow in the opposite direction , the read voltage value decreases, the minimum is 0V. there is an ADC on 8266, connect the x-axis to ADC0, gnd and 5V to gnd 3.3V of 8266, so as to read the analog value of the joystick, and then connect a few buttons, so that the hardware design of 8266 remote control is completed. The program is as follows
from machine import ADC,Pin import time ,network,socket def setwifi(ssid,pwd): global result wlan = network.WLAN(network.AP_IF) # create access-point interface wlan.active(True) # activate the interface wlan.config(essid=ssid, authmode=network.AUTH_WPA_WPA2_PSK, password=pwd) while (wlan.ifconfig()[0] == '0.0.0.0'): time.sleep(1) result = wlan.ifconfig()[0] return result def http_get(cmd): s = socket.socket() s.setblocking(True) s.connect((ip,80)) s.send(b"?cmd="+cmd+" END") print(s.recv(4096)) s.close() def numtoduty(num): return 30+int(num*(70-30)/1024) setwifi('test','testtest') isforward=0 servo=ADC(0) lastnum=servo.read() stop=Pin(2,Pin.IN) dir=Pin(0,Pin.IN) back=Pin(13,Pin.IN) f2b=0 one =0 while(1): currnum=servo.read() if(abs(lastnum-currnum)>10): print(currnum) # print(numtoduty(currnum)) http_get(str(numtoduty(currnum))) pass time.sleep_ms(50) lastnum=currnum if (stop.value()==0 ): print('stop') http_get('stop') time.sleep_ms(20) while(stop.value()==0): pass if(dir.value()==0 ): print('forward') http_get('forward') while(dir.value()==0): pass #print('l') continue if (back.value()==0 ): time.sleep_ms(50) while(back.value()==0): pass http_get('back') continue