|
在淘宝找到一个双轴按键摇杆 PS2游戏摇杆控制杆传感器
查看说明:
十字摇杆为一个双向的10K电阻器,随着摇杆方向不同,抽头的阻值随着变化。本模块使用5V供电,原始状态下X,Y读出电压为2.5V左右,当随箭头方向按下,读出电压值随着增加,最大到5V;箭头相反方向按下,读出电压值减少,最小为0V。
8266上有一个ADC把其中的x轴接到ADC0 ,gnd 和5V接到8266的gnd 3.3V 这样就读取摇杆的模拟量,再接几个按钮,这样8266遥控器就完成硬件设计了.
程序上如下
- 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
复制代码
|
|