54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
import serial
|
|
import pygame
|
|
import time
|
|
|
|
port = 'COM13' # port de com arduino
|
|
baud_rate = 115200 # baudrate
|
|
ser = serial.Serial(port, baud_rate) # connexion à l'arduino
|
|
|
|
pygame.mixer.init() # initialisation pygame
|
|
|
|
do = pygame.mixer.Sound("notes/4-c.wav")
|
|
re = pygame.mixer.Sound("notes/4-d.wav") # liste des fichiers audio pour chaque notes
|
|
mi = pygame.mixer.Sound("notes/4-e.wav")
|
|
fa = pygame.mixer.Sound("notes/4-f.wav")
|
|
sol = pygame.mixer.Sound("notes/4-g.wav")
|
|
la = pygame.mixer.Sound("notes/4-a.wav")
|
|
|
|
notes = [do, re, mi, fa, sol, la] # liste des notes pour jouer
|
|
|
|
keys_values_arduino = [0, 0, 0, 0, 0, 0] # liste ou seront stocker les valeurs des touches de 0 à 1023
|
|
keys_threshold = [150, ]
|
|
|
|
notes_current_state = [0, 0, 0, 0, 0, 0]
|
|
notes_last_state = [0, 0, 0, 0, 0, 0]
|
|
|
|
first_time_clicked = [time.time() for i in range(6)] # initialisation du temps pour chaque touche
|
|
|
|
for i in range(20): # output the 20 first lines to supress the errors
|
|
line = ser.readline()
|
|
|
|
try:
|
|
while True:
|
|
line = ser.readline().decode().strip()
|
|
keys_values_arduino = line.split(',')
|
|
print(keys_values_arduino)
|
|
for i in range(len(keys_values_arduino[:-1])):
|
|
if int(keys_values_arduino[i]) >= 850:
|
|
notes_current_state[i] = 1
|
|
else:
|
|
notes_current_state[i] = 0
|
|
if notes_current_state[i] != notes_last_state[i]:
|
|
if time.time() - first_time_clicked[i] >= 0.2:
|
|
pygame.mixer.Channel(i).play(pygame.mixer.Sound(notes[i]))
|
|
first_time_clicked[i] = time.time()
|
|
|
|
notes_last_state[i] = notes_current_state[i]
|
|
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
print("Arrêt du programme")
|
|
|