Last active
May 27, 2017 10:07
-
-
Save marschhuynh/eb5e1f420773e534143cf9dc7d756733 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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