Skip to content

Instantly share code, notes, and snippets.

@shemnon
Created March 12, 2017 02:49
Show Gist options
  • Save shemnon/707e02f98524005b5150005aed3cd262 to your computer and use it in GitHub Desktop.
Save shemnon/707e02f98524005b5150005aed3cd262 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
# set MAXM race time, times out after this if all cars don't finish
max_race_time = 20
#set input gpio pin numbers
LaneOne = 16 #GPIO23
LaneTwo = 18 #GPIO24
#LaneThree = 22 #GPIO25
#LaneFour = 24 #GPIO8 CE0
Start_Button = 26 #GPIO7 CE1
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(LaneOne,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(LaneTwo,GPIO.IN,pull_up_down = GPIO.PUD_UP)
#GPIO.setup(LaneThree,GPIO.IN,pull_up_down = GPIO.PUD_UP)
#GPIO.setup(LaneFour,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(Start_Button,GPIO.IN,pull_up_down = GPIO.PUD_UP)
# set output led gpio pin numbers
#leds = [
#3, #GPIO2 SDA
#5, #GPIO3 SCL
#7, #GPIO4 GPKLK0
#11, #GPIO17
#13, #GPIO27
#15, #GPIO22
#19, #GPIO10 MOSI
#21, #GPIO9 MISO
#23, #GPIO11 SCLK
#8, #GPIO14 TXD
#10, #GPIO15 RXD
#12 #GPIO18 PWMO
#]
# test leds
x = 0
#while x < len(leds):
# GPIO.setup(leds[x],GPIO.OUT)
# GPIO.output(leds[x],GPIO.HIGH)
# time.sleep(0.25)
# GPIO.output(leds[x],GPIO.LOW)
# x +=1
winner = 0
while True:
# check DETECTORS receiving
One_fault = 0
Two_fault = 0
# Three_fault = 0
# Four_fault = 0
Total_faults = 0
if GPIO.input(LaneOne) == 1 :
print "DETECTOR 1 NOT OK !!"
# GPIO.output(leds[2],GPIO.HIGH)
One_fault = 1
Total_faults = Total_faults + 1
if GPIO.input(LaneTwo) == 1 :
print "DETECTOR 2 NOT OK !!"
# GPIO.output(leds[5],GPIO.HIGH)
Two_fault = 1
Total_faults = Total_faults + 1
# if GPIO.input(LaneThree) == 1 :
# print "DETECTOR 3 NOT OK !!"
# GPIO.output(leds[8],GPIO.HIGH)
# Three_fault = 1
# Total_faults = Total_faults + 1
# if GPIO.input(LaneFour) == 1 :
# print "DETECTOR 4 NOT OK !!"
# GPIO.output(leds[11],GPIO.HIGH)
# Four_fault = 1
# Total_faults = Total_faults + 1
# wait for START
print "PRESS the START BUTTON...(Ctrl&C to exit)"
while GPIO.input(Start_Button) == 1:
# GPIO.output(leds[winner],GPIO.HIGH)
time.sleep(.1)
# GPIO.output(leds[winner],GPIO.LOW)
time.sleep(.1)
print "READY..."
# GPIO.output(leds[0],GPIO.LOW)
x = 0
# while x < len(leds):
# GPIO.output(leds[x],GPIO.LOW)
# x +=1
# check DETECTORS receiving
One_fault = 0
Two_fault = 0
# Three_fault = 0
# Four_fault = 0
Total_faults = 0
while GPIO.input(Start_Button) == 0:
time.sleep(0.0005)
print "RACE!"
if GPIO.input(LaneOne) == 1 :
print "DETECTOR 1 NOT OK !!"
# GPIO.output(leds[2],GPIO.HIGH)
One_fault = 1
Total_faults = Total_faults + 1
if GPIO.input(LaneTwo) == 1 :
print "DETECTOR 2 NOT OK !!"
# GPIO.output(leds[5],GPIO.HIGH)
Two_fault = 1
Total_faults = Total_faults + 1
# if GPIO.input(LaneThree) == 1 :
# print "DETECTOR 3 NOT OK !!"
# GPIO.output(leds[8],GPIO.HIGH)
# Three_fault = 1
# Total_faults = Total_faults + 1
# if GPIO.input(LaneFour) == 1 :
# print "DETECTOR 4 NOT OK !!"
# GPIO.output(leds[11],GPIO.HIGH)
# Four_fault = 1
# Total_faults = Total_faults + 1
# if One_fault == 1 :
# GPIO.output(leds[2],GPIO.HIGH)
# if Two_fault == 1 :
# GPIO.output(leds[5],GPIO.HIGH)
# if Three_fault == 1 :
# GPIO.output(leds[8],GPIO.HIGH)
# if Four_fault == 1 :
# GPIO.output(leds[11],GPIO.HIGH)
# start the race
start_time = time.time()
timer = time.time()
finish_time = 0
diff_time = 0
Finish = ""
One_finish = 0
Two_finish = 0
# Three_finish = 0
# Four_finish = 0
ident = " + "
place = 0
One_place = 0
Two_place = 0
# Three_place = 0
# Four_place = 0
winner = 0
while (time.time() - timer) < max_race_time and place < (2 - Total_faults):
if GPIO.input(Start_Button) == 0:
print "ABORT!"
Total_faults = 100
#check Lane One
if GPIO.input(LaneOne) == 1 and One_finish == 0 and One_fault == 0:
One_finish = 1
place = place + 1
One_place = place
if One_place == 2 or One_place == 3:
pass
#GPIO.output(leds[0],GPIO.HIGH)
if One_place == 1 or One_place == 3:
#GPIO.output(leds[1],GPIO.HIGH)
if One_place == 1:
winner = 1
if One_place == 2 or One_place == 3:
pass
# GPIO.output(leds[2],GPIO.HIGH)
my_time = time.time()- start_time
if Finish == "":
finish_time = time.time()
ident = " "
diff_time = " "
else:
diff_time = (time.time() - finish_time)
Finish = Finish + "1 " + str(my_time)[0:5] + ident + str(diff_time)[0:5] + chr(10)
#check Lane Two
elif GPIO.input(LaneTwo) == 1 and Two_finish == 0 and Two_fault == 0:
Two_finish = 1
place = place + 1
Two_place = place
if Two_place == 2 or Two_place == 3:
pass
# GPIO.output(leds[3],GPIO.HIGH)
if Two_place == 1 or Two_place == 3:
# GPIO.output(leds[4],GPIO.HIGH)
if Two_place == 1:
winner = 4
if Two_place == 2 or Two_place == 3:
pass
# GPIO.output(leds[5],GPIO.HIGH)
my_time = time.time()- start_time
if Finish == "":
finish_time = time.time()
ident = " "
diff_time = " "
else:
diff_time = (time.time() - finish_time)
Finish = Finish + "2 " + str(my_time)[0:5] + ident + str(diff_time)[0:5] + chr(10)
#check Lane Three
# elif GPIO.input(LaneThree) == 1 and Three_finish == 0 and Three_fault == 0:
# Three_finish = 1
# place = place + 1
# Three_place = place
# if Three_place == 2 or Three_place == 3:
# GPIO.output(leds[6],GPIO.HIGH)
# if Three_place == 1 or Three_place == 3:
# GPIO.output(leds[7],GPIO.HIGH)
# if Three_place == 1:
# winner = 7
# if Three_place == 2 or Three_place == 3:
# GPIO.output(leds[8],GPIO.HIGH)
# diff_time = time.time()- start_time
# if Finish == "":
# finish_time = time.time()
# ident = " "
# else:
# diff_time = (time.time() - finish_time)
# Finish = Finish + "3 " + ident + str(diff_time)[0:7] + chr(10)
# #check Lane Four
# elif GPIO.input(LaneFour) == 1 and Four_finish == 0 and Four_fault == 0:
# Four_finish = 1
# place = place + 1
# Four_place = place
# if Four_place == 2 or Four_place == 3:
# GPIO.output(leds[9],GPIO.HIGH)
# if Four_place == 1 or Four_place == 3:
# GPIO.output(leds[10],GPIO.HIGH)
# if Four_place == 1:
# winner = 10
# if Four_place == 2 or Four_place == 3:
# GPIO.output(leds[11],GPIO.HIGH)
# diff_time = time.time()- start_time
# if Finish == "":
# finish_time = time.time()
# ident = " "
# else:
# diff_time = (time.time() - finish_time)
# Finish = Finish + "4 " + ident + str(diff_time)[0:7] + chr(10)
ident = " + "
# identify NOT FINISHERS
if One_finish == 0:
Finish = Finish + "1 DNF" + chr(10)
if Two_finish == 0:
Finish = Finish + "2 DNF" + chr(10)
# if Three_finish == 0:
# Finish = Finish + "3 DNF" + chr(10)
# if Four_finish == 0:
# Finish = Finish + "4 DNF" + chr(10)
# print the finishing sequence and times relative to the winner
if (Total_faults < 99):
if len(Finish) > 0:
print chr(10) + "Finishing Order: " + chr(10) + chr(10), Finish
# wait to ensure lanes are clear
time.sleep(3)
# else:
# while GPIO.input(Start_Button) == 0:
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment