Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nghiadt1098/bf21ab740db37a67433d6282191c0229 to your computer and use it in GitHub Desktop.
Save nghiadt1098/bf21ab740db37a67433d6282191c0229 to your computer and use it in GitHub Desktop.
from gpiozero import *
from time import sleep
import xbox
import code
A=14
B=2
D4=3
D3=4
D2=17
D1=15
error1=23
error2=24
speedA=PWMLED(A)
speedB=PWMLED(B)
status1A=LED(D1)
status2A=LED(D2)
status1B=LED(D4)
status2B=LED(D3)
error1=LED(23)
error2=LED(24)
error1.on()
error2.on()
speedA.on()
speedB.on()
print("Start")
def refresh():
speedA.on()
speedB.on()
status1B.value=0
status2B.value=0
status1A.value=0
status2A.value=0
def stop():
speedA.off()
speedB.off()
def up():
speedA.value=1
speedB.value=1
status2A.on()
status2B.on()
status1A.off()
status1B.off()
def down():
speedA.value=1
speedB.value=1
status2A.off()
status2B.off()
status1A.on()
status1B.on()
def left():
speedB.value=0.0
status1B.value=0
status2B.value=0
def right():
speedA.value=0.0
status1A.value=0
status2A.value=0
def setSpeed(value):
speedA.value=value
speedB.value=value
while(True):
try:
joy=xbox.Joystick()
error1.on()
error2.on()
break
except Exception as e:
if("Unable" in str(e)):
error1.off()
if("controller" in str(e)):
error2.off()
print(e)
while(True):
if(joy.Start()):
start()
print("%d %d %d %d" % (status1A.value,status2A.value,status1B.value,status2B.value))
if(joy.dpadLeft()):
left()
speedA.value=joy.rightTrigger()
continue
if(joy.dpadRight()):
right()
speedB.value=joy.rightTrigger()
continue
if(joy.leftTrigger()>0.4):
setSpeed(1)
status2A.off()
status2B.off()
status1A.on()
status1B.on()
continue
if(joy.rightTrigger()>0.4):
setSpeed(1)
status2A.on()
status2B.on()
status1A.off()
status1B.off()
continue
if(joy.Back()):
stop()
continue
setSpeed(joy.rightTrigger())
refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment