1

I am running a python script on a Raspberry Pi, I am using 4 of the gpio pins, 3 as outputs and 1 as an input. When the input is set low it activates a procedure were the outputs are all turned on and off at different times and a sound effect is played using pygame.mixer. I started to have some problems were the input would not activate the procedures anymore. When I looked into it further I discovered that it would only work for 120 times then the input would not activate the procedures anymore and i would get no error message. I am in no way an expert in coding in python so my code is probably messy looking. Here is the code.

    import RPi.GPIO as GPIO
    import sys
    import time
    import pygame
    import os
    from array import *

    print "loading program"
    GPIO.setmode(GPIO.BCM)
    GpioPinsOut=[4,17,27]
    for a in range (0, len(GpioPinsOut)):
        GPIO.setup(GpioPinsOut[a], GPIO.OUT)
    GpioPinsIn=[7,]
    for y in range (0, len(GpioPinsIn)):
        GPIO.setup(GpioPinsIn[y], GPIO.IN)
    start=0
    prevTriggered=0
    triggered=0
    elapsed=0
    vars=[0,]
    inVars=[0]
    prevInVars=[5,5]
    sceneNames=["Night"]
    onoffVar=  [1, 0]
    onoffVar2= ["On", "Off"]
    outputNamesFinal=['output1','output2','output3']
    outputNames= [[['output1','output2','output3'],['output1','output2','output3','reset']]]
    outputs=     [[[4        ,17       ,27       ],[4        ,17       ,27       ,'reset']]]
    times=       [[[0.0      ,0.0      ,0.0      ],[11.7     ,18.0     ,18.0     ,18.0   ]]]
    printGpioVar=[[[0        ,0        ,0        ],[0        ,0        ,0        ,0      ]]]
    outputsAlwaysOn=[[]]
    for va in range (0, len(GpioPinsOut)):
        GPIO.output(GpioPinsOut[va], False)
    for za in range (0, len(outputsAlwaysOn[0])):
        GPIO.output(outputsAlwaysOn[0][za], True)               
    effects=[[],[]]
    effectslocations=[['/root/audio/soundeffect.wav']]
    pygame.mixer.init(44100, -16, 2, 2048)
    for ba in range (0, len(effectslocations)):
        for bb in range (0, len(effectslocations[ba])):
            effectsVar = pygame.mixer.Sound(effectslocations[ba][bb])
            effects[ba].append(effectsVar)
    effectsNumbers=[[0  ]]
    effectsTimes=  [[0.0]]
    sfxVAR = 0
    numberOfTimes=0
    print "program ready"
    try:
        while True:
            for z in range (0, len(GpioPinsIn)):
                if GPIO.input(GpioPinsIn[z]) == False:
                    inVars[z]=0
                else:
                    inVars[z]=1
            if inVars[0] == 0:
                triggered = 1

            if triggered == 1:
                if prevTriggered != triggered:
                    print "Trigger Activated"
                    if start == 0:
                        print "Start Time Activated"
                        numberOfTimes+=1
                        print numberOfTimes
                        start = time.clock()
                elapsed = time.clock() - start
                for g in range(0, len(effectsTimes[0])):
                    if elapsed >= effectsTimes[0][g]:
                        if sfxVAR == 0:
                            effects[0][effectsNumbers[0][g]].play()
                            sfxVAR = 1

                for c in range (0, len(times[0])):
                    for d in range (0, len(times[0][c])):
                        if elapsed >= times[0][c][d]:
                            if outputs[0][c][d] == "reset":
                                print "done with %s procedures"%sceneNames[0]
                                for e in range (0, len(printGpioVar[0])):
                                    for f in range (0, len(printGpioVar[0][e])):
                                        printGpioVar[0][e][f]=0 
                                start=0
                                prevTriggered=0
                                sfxVAR=0
                                triggered=0

                            else:
                                if printGpioVar[0][c][d]==0:
                                    GPIO.output(outputs[0][c][d], onoffVar[c])
                                    print "Turned %s %s"%(onoffVar2[c], outputNames[0][c][d])
                                    printGpioVar[0][c][d]=1

                prevTriggered = triggered
            for g in range (0, len(inVars)):
                prevInVars[g]=inVars[g]
    except KeyboardInterrupt:
        GPIO.cleanup()
        sys.exit()
farmtech
  • 11
  • 1
  • The sintaxis is not very pythonic. For example, when you set the pins as outputs or inputs, it can be like this, which is much simpler and easier to read. for pin in GpioPinsOut: GPIO.setup(pin, GPIO.OUT) – Diego Herranz Sep 23 '13 at 12:24
  • I think I figured it out. On a Raspberry Pi when using time.clock() it changes from 2147.48 seconds to -2147.477296 which happens to be the same time my code went through 120 cycles. I am trying to see if time.time() works for me. – farmtech Sep 24 '13 at 02:27

0 Answers0