Skip to content

Instantly share code, notes, and snippets.

@marschhuynh
Last active May 27, 2017 10:07
Show Gist options
  • Save marschhuynh/eb5e1f420773e534143cf9dc7d756733 to your computer and use it in GitHub Desktop.
Save marschhuynh/eb5e1f420773e534143cf9dc7d756733 to your computer and use it in GitHub Desktop.
import pi2go, time
pi2go.init()
# Here we set the speed to 60 out of 100 - feel free to change!
speed = 65
count = 0
isLeft = 0
disError = 10000
speedSpin = 40
countToGet = 0
try:
while True:
countToGet += 1
if countToGet > 1000000:
distance = pi2go.getDistance()
print(distance)
if distance < 5:
spinCount = 100000
while spinCount > 0:
print("-----------------------", distance)
pi2go.spinLeft(speed)
spinCount -= 1
countToGet = 0
# Defining the sensors on the bottom of the Pi2Go
left = pi2go.irLeftLine()
right = pi2go.irRightLine()
if left == right: # If both sensors are the same (either on or off):
# Forward
if left:
count += 1
if count > disError:
if isLeft:
pi2go.spinRight(speed)
print("Left >")
else:
print("Right >")
pi2go.spinLeft(speed)
count -= 1
print("Thang", count, isLeft)
pi2go.forward(speed)
elif left == True: # If the left sensor is on
print("Trai")
count = 0
isLeft=1
pi2go.spinRight(speedSpin)
elif right == True: #If the right sensor is on
# Right
isLeft=0
count = 0
print("Phai")
pi2go.spinLeft(speedSpin)
finally: # Even if there was an error, cleanup
pi2go.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment