Python + Raspi:根据按下按钮来改变LED颜色

时间:2017-03-28 03:31:58

标签: python raspberry-pi gpio led

我编写了以下代码,将面包板上LED的颜色从蓝色变为紫色再变为红色,然后闪烁。当程序运行时,无论按下按钮,LED都会打开并始终保持蓝色。

import RPi.GPIO as GPIO
import time

limit = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Rpin = 11
Bpin = 13
flow = 0

try:
    while True:
        if GPIO.input(15) == False:
            flow += 1
            print flow
            time.sleep(0.1)
        if flow >= limit:
            False
        if flow < 6*limit//10:
            print("blue")
            GPIO.setup(Bpin, GPIO.OUT)
            GPIO.output(Bpin, GPIO.HIGH)

        elif flow in range(6*limit//10, 8*limit//10):
            print("violet")
            GPIO.setup(Rpin, GPIO.OUT)
            GPIO.output(Rpin, GPIO.HIGH)

        elif flow in range(8*limit//10, limit):
            print("red")
            GPIO.output(Bpin, GPIO.LOW)

        else:
            print("flashing red...")
            GPIO.output(Rpin, GPIO.LOW)
            time.sleep(.2)
            GPIO.output(Rpin, GPIO.HIGH)
            time.sleep(.2)
            GPIO.output(Rpin, GPIO.LOW)
            time.sleep(.2)
            GPIO.output(Rpin, GPIO.HIGH)
            time.sleep(.2)
            GPIO.output(Rpin, GPIO.LOW)
            break

except KeyboardInterrupt:   
        GPIO.output(Rpin, GPIO.LOW)
        GPIO.output(Bpin, GPIO.LOW)
        GPIO.cleanup()                  

我可以通过按键使LED正常工作,并可以按下按钮并打印该值,但两者不能同时工作。我非常确定这不是布线问题,因为其他代码可以使这些部件自行工作。我已经阅读了Stack和其他网站上的相关帖子,并且找不到与我类似的任何代码,其中增量会改变LED的颜色。作为一个相对较新的python用户,我怀疑代码中存在问题。非常感谢任何帮助!

import RPi.GPIO as GPIO
import time #added for sleep mode

GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Rpin = 11
Bpin = 13
flow = 0

GPIO.setup(Bpin, GPIO.OUT)
GPIO.setup(Rpin, GPIO.OUT)

GPIO.output(Rpin, GPIO.LOW)
GPIO.output(Bpin, GPIO.LOW)

limit = int(raw_input("How many gallons of water would you like to use                    for your shower?  "))
print("You may begin showering")
print limit

def flowmeter(f, l):
    if f == 0:
        print("off")
        GPIO.output(Rpin, GPIO.LOW)
        GPIO.output(Bpin, GPIO.LOW)
        GPIO.cleanup()
    if f < 6*l//10:
        print("blue")
        GPIO.output(Bpin, GPIO.HIGH)
    elif f in range(6*l//10, 8*l//10):
        print("violet")
        GPIO.output(Rpin, GPIO.HIGH)
    elif f in range(8*l//10, l):
        print("red")
        GPIO.output(Bpin, GPIO.LOW)
    else:
        print("flashing red... time's up!")
        GPIO.output(Rpin, GPIO.LOW)
        time.sleep(.5)
        GPIO.output(Rpin, GPIO.HIGH)
        time.sleep(.5)
        GPIO.output(Rpin, GPIO.LOW)
        time.sleep(.5)
        GPIO.output(Rpin, GPIO.HIGH)
        time.sleep(.5)
        GPIO.output(Rpin, GPIO.LOW)
        print("cleaning GPIO")
        GPIO.output(Rpin, GPIO.LOW)
        GPIO.output(Bpin, GPIO.LOW)
        GPIO.cleanup()
while True: 
    if GPIO.input(15) == False:
        flow += 1
        print("flow = %s" %flow)
        time.sleep(0.1) #prevents infinite pushes if held down for a few seconds
        flowmeter(flow,limit)

1 个答案:

答案 0 :(得分:0)

调试提示:如果您的代码永远不会在if语句中执行代码,那么因为if测试总是评估为false,那就是您应该调查的内容。

您需要阅读有关内置范围()函数的python文档 - 它不会执行您要执行的操作。

而不是:

package optimum.output;
import java.util.Scanner;
public class OptimumOutput {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    // First of all we have to get what the runs planned for next month. To get that there should be separate method

    plannedruns();

}
//plannedruns method is ment to get next month planned runs
 static String[] plannedruns(){

    String[] runs=new String[50];      
    //Writes instructions in console
    System.out.println("Instructions"+"\n"+"Please enter Job ID's that planned for this month (Mazimum 50 jobs)"+"\n"+"At the end of the list, enter 0");
    // Scanner to take inputs
    Scanner scanner = new Scanner(System.in);
    int i=0;
    //takes job ID inputs
    for(int position=0;position<50;position++){
        System.out.print("Enter Job ID for Job " + ++i +" : ");
        runs[position]=null;
        runs[position] = scanner.nextLine();
        try{
                if (Integer.parseInt(runs[position])==0){
                break;
            }
            for(int j=0;position>j;j++){
                System.out.print(runs[j].equalsIgnoreCase(runs[position]));
                if(runs[j].equals(runs[position])){
                    System.out.println("One job repeated. Please enter the run correctly");
                    --i;
                    --position;
                }
            }

        }
        catch(Exception e){                
        }
    }
    //Print before printing job details
    System.out.println("These are the entered jobs for this month");
    i=0;
    for(int position=0;position<50;){
        try{
            if (Integer.parseInt(runs[position])==0){
                break;
            }
        }
        catch(Exception e){            
        }
        System.out.println("Job No."+ ++i +" is "
                + ""+runs[position++]);

    }
    System.out.println("end");
    return runs;
  }
}

使用比较,例如

f in range(6*l//10, 8*l//10):