Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os #import the os library (used to clear the screen when necessary)
import RPi.GPIO as gpio #import library used to control the Raspnerry’s GPIO
import serial #import library responsible for the serial communication
import time #import library that makes it possible to use the delay function
import subprocess #import library responsible for play the songs
#start serial
ser = serial.Serial("/dev/ttyS0",9600) #define the device name and the baud rate
#clear screen
clear = lambda: os.system('clear')
#set pins for relay control
gpio.setmode(gpio.BOARD)
gpio.setup(11,gpio.OUT) #lamp 10
gpio.setup(12,gpio.OUT) #lamp 9
gpio.setup(13,gpio.OUT) #lamp 8
gpio.setup(15,gpio.OUT) #lamp 7
gpio.setup(16,gpio.OUT) #lamp 6
gpio.setup(18,gpio.OUT) #lamp 5
gpio.setup(19,gpio.OUT) #lamp 4
gpio.setup(21,gpio.OUT) #lamp 3
gpio.setup(22,gpio.OUT) #lamp 2
gpio.setup(23,gpio.OUT) #lamp 1
#start records
name=["None"]*10
voltage=[0.00]*10
#read records file
f = open('records','r')
for i in range(10): #the 10 best scores appear on the list
name[i]=f.readline()
name[i]=name[i][:len(name[i])-1]
voltage[i]=f.readline()
voltage[i]=float(voltage[i][:len(voltage[i])-1])
f.close()
clear()
#set the max voltage
max=50.00
#turn the lamps off
for i in range(11,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.HIGH) #set to HIGH, the relays are turned off
#start
while True:
#initial screen
print "Records:\n"
for i in range(10):
print name[i],":",voltage[i],"V"
current_name=raw_input("Write your name to start: ")
clear()
#Change max value
if current_name == "max":
max=input("Write the max voltage: (2 decimal places)")
clear()
else:
#start warning
for i in range(11,24,1): #the loop starts in the PIN 11 and stop in PIN 24
if i!=14 and i!=17 and i!=20: #PIN 14 and 20 are GND pins and 20 is a 3.3 V pin
gpio.output(i,gpio.LOW) #turn the lamps on
time.sleep(0.5)
k=10
for i in range (23,10,-1):
clear()
if i!=14 and i!=17 and i!=20:
subprocess.Popen(['aplay','Audios/'+str(k)+'.wav'])
time.sleep(0.03)
clear()
print "Prepare!\n",k
time.sleep(1)
k-=1
gpio.output(i,gpio.HIGH) #turn the lamps off (one by one)
subprocess.Popen(['aplay','Audios/go.wav']) #plays the start music
time.sleep(0.03)
clear()
print "GO!"
time.sleep(1)
clear()
#voltage read
current_voltage=0.00
voltage1=0.00
for i in range(200):
ser.flushInput()
previous=voltage1
voltage1=float(ser.readline()) #collects Arduino’s data transferred by RX-TX
clear()
print voltage1,"V"
if voltage1>current_voltage:
current_voltage=voltage1
# depending on the voltage generated, more lamps light up.
if voltage1<max/10:
for i in range(11,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=max/10:
gpio.output(11,gpio.LOW)
for i in range(12,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=2*max/10:
for i in range (11,13,1):
gpio.output(i,gpio.LOW)
for i in range(13,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=3*max/10:
for i in range (11,14,1):
gpio.output(i,gpio.LOW)
for i in range(15,24,1):
if i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=4*max/10:
for i in range (11,16,1):
if i!=14:
gpio.output(i,gpio.LOW)
for i in range(16,24,1):
if i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=5*max/10:
for i in range (11,17,1):
if i!=14:
gpio.output(i,gpio.LOW)
for i in range(18,24,1):
if i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=6*max/10:
for i in range (11,19,1):
if i!=14 and i!=17:
gpio.output(i,gpio.LOW)
for i in range(19,24,1):
if i!=20:
gpio.output(i,gpio.HIGH)
if voltage1>=7*max/10:
for i in range (11,20,1):
if i!=14 and i!=17:
gpio.output(i,gpio.LOW)
for i in range(21,24,1):
gpio.output(i,gpio.HIGH)
if voltage1>=8*max/10:
for i in range (11,22,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.LOW)
for i in range(22,24,1):
gpio.output(i,gpio.HIGH)
if voltage1>=9*max/10:
for i in range (11,23,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.LOW)
gpio.output(23,gpio.HIGH)
if voltage1>=max:
for i in range (11,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.LOW)
if voltage1<previous-2.0:
break
#turn the lamps off
for i in range (11,24,1):
if i!=14 and i!=17 and i!=20:
gpio.output(i,gpio.HIGH)
#victory music
if current_voltage>=max:
subprocess.Popen(['aplay','Audios/rocky.wav'])
time.sleep(0.03)
clear()
print "VERY GOOD, YOU WON!" % (u'\u00c9',u'\u00ca',u'\u00c2')
for i in range(10):
for j in range(11,24,1):
if j!=14 and j!=17 and j!=20:
gpio.output(j,gpio.LOW)
time.sleep(0.05)
for j in range(11,24,1):
if j!=14 and j!=17 and j!=20:
gpio.output(j,gpio.HIGH)
time.sleep(0.05)
time.sleep(0.5)
subprocess.Popen(['aplay','Audios/end.wav'])
time.sleep(0.03)
clear()
print "End game...\n",current_voltage,"V"
#records
time.sleep(1.2)
reached=0
for i in range(10):
if current_voltage > voltage[i]:
reached+=1
temp_voltage=voltage[i]
voltage[i]=current_voltage
current_voltage=temp_voltage
temp_name=name[i]
name[i]=current_name
current_name=temp_name
if reached > 0:
subprocess.Popen(['aplay','Audios/record.wav'])
time.sleep(0.03)
clear()
f = open('records','w')
for i in range(10):
f.write(name[i])
f.write("\n")
f.write(str(voltage[i]))
f.write("\n")
f.close()
clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.