mardi 9 juin 2015

Use same Python variable in multiple if-statements

I am having issues with my current Python script. The purpose of the 'progress' variable is take a specific value when it goes through one of the if-loops. But, the program never goes further then the first if-statement. It looks as if every if-statement makes his own variable called 'progress'. Can someone please help me? See code below.

from bottle import run, route, template, error, static_file     
import RPi.GPIO as GPIO                         
import time                             

switch1 = 21        
switch2 = 20        
switch3 = 26        
switch4 = 16        
switch5 = 19        

led1 = 13       
led2 = 12       
led3 = 6        
led4 = 5        
led5 = 25       

GPIO.setmode(GPIO.BCM)                          

GPIO.setup(switch1, GPIO.IN, pull_up_down=GPIO.PUD_UP)          
GPIO.setup(switch2, GPIO.IN, pull_up_down=GPIO.PUD_UP)          
GPIO.setup(switch3, GPIO.IN, pull_up_down=GPIO.PUD_UP)          
GPIO.setup(switch4, GPIO.IN, pull_up_down=GPIO.PUD_UP)          
GPIO.setup(switch5, GPIO.IN, pull_up_down=GPIO.PUD_UP)          

GPIO.setup(led1, GPIO.OUT)                      
GPIO.setup(led2, GPIO.OUT)                      
GPIO.setup(led3, GPIO.OUT)                      
GPIO.setup(led4, GPIO.OUT)                      
GPIO.setup(led5, GPIO.OUT)                      


@route("/")
def hello():
    progress = 0
    while True:
        if progress == 0:
            GPIO.output(led1, False)
            GPIO.output(led2, False)
            GPIO.output(led3, False)
            GPIO.output(led4, False)
            GPIO.output(led5, False)

            progress = 1    
            return template('index.html')

        if (not GPIO.input(switch1)) and progress == 1:     
            GPIO.output(led1, True)
            progress = 2
            return template('video1.html')

        elif (not GPIO.input(switch2)) and progress == 2:   
            GPIO.output(led1, False)
            GPIO.output(led2, True)
            progress = 3
            return template('video2.html')

        elif (not GPIO.input(switch3)) and progress == 3:   
            GPIO.output(led2, False)
            GPIO.output(led3, True)
            progress = 4
            return template('video3.html')

        elif (not GPIO.input(switch4)) and progress == 4:   
            GPIO.output(led3, False)
            GPIO.output(led4, True)
            progress = 5
            return template('video4.html')

        elif (not GPIO.input(switch5)) and progress == 5:   
            GPIO.output(led4, False)
            GPIO.output(led5, True)
            progress = 6
            return template('video5.html')

        elif progress == 6:
            while True:                     
                GPIO.output(led1, True)
                GPIO.output(led2, True)
                GPIO.output(led3, True)
                GPIO.output(led4, True)
                GPIO.output(led5, True)
                time.sleep(0.5)
                GPIO.output(led1, False)
                GPIO.output(led2, False)
                GPIO.output(led3, False)
                GPIO.output(led4, False)
                GPIO.output(led5, False)
                time.sleep(0.5)

                return template('succes.html')

        elif GPIO.input(switch1) and GPIO.input(switch2) and GPIO.input(switch3) and GPIO.input(switch4) and GPIO.input(switch5):
             time.sleep(0.15)  


        else:                           
            GPIO.output(led1, False)
            GPIO.output(led2, False)
            GPIO.output(led3, False)
            GPIO.output(led4, False)
            GPIO.output(led5, False)

            return template('false.html')

        time.sleep(0.05)                    

@route('/<filename>')                           
def server_static(filename):
    return static_file(filename, root='static')

@error(404)                             
def error404(error):
    return("Nothing here, keep searching!")

run(host='0.0.0.0', port=80)                        

Aucun commentaire:

Enregistrer un commentaire