mercredi 30 novembre 2016

Problems playing audio and using functions in python/Raspberry

I am trying to code a Q/A script for my raspberry using buttons and audio. But for some reason I am having problems with 'defs' or if statements. When I run my code, my first def doesn't read my button, so it doens't access other functions, and keeps repeating the first def only. Ps: my two buttons are fine, I've tried it before, and works fine.

import pygame.mixer
from time import sleep
import RPi.GPIO as GPIO
from sys import exit
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)#YES
GPIO.setup(3, GPIO.IN)#NO

pygame.mixer.init()

sndA = pygame.mixer.Sound("audio.wav")#DO you want to eat?
sndB = pygame.mixer.Sound("audio2.wav")#Ok! lets GO!
sndC = pygame.mixer.Sound("audio3.wav")#Did you finish?
sndD = pygame.mixer.Sound("audio4.wav")#Great! 
sndE = pygame.mixer.Sound("audio5.wav")#No problems!
sndF = pygame.mixer.Sound("audio6.wav")#Oh dear! ok :(
soundChannelA = pygame.mixer.Channel(1)
soundChannelB = pygame.mixer.Channel(2)
soundChannelC = pygame.mixer.Channel(3)
soundChannelD = pygame.mixer.Channel(4)

def havealunch():
    time.sleep(5)
    soundChannelA.play(sndA)#Do you want to eat?
    if (GPIO.input(2) == False):#Yes
        wants()
    elif (GPIO.input(3) == False):#No
        doesntwants()   
    time.sleep(5)
def wants():
    soundChannelB.play(sndB)#Ok! Lets go!
    time.sleep(10)
    didyoufinish()

def doesntwants():
    soundChannelD.play(sndF)#Oh dear! Ok :(

def didyoufinish():
    soundChannelD.play(sndC)#Did you finish?
    if (GPIO.input(2) == False):#Yes!
        finished()
    if (GPIO.input(3) == False):#No
        didnt()

def finished(): 
    soundChannelD.play(sndD)#Great!


def didnt():
    soundChannelD.play(sndE)#No problems!   
    didyoufinish()  

print "Ready!!!"

while True: 
    havealunch()

I've tried to use only IF statements for this Q/A, but I had many problems, so this is my second plan, and now I have no idea about what to do. Any ideia?

Aucun commentaire:

Enregistrer un commentaire