简体   繁体   English

代码调试,为什么操作后LED灯不会熄灭?

[英]Code debugging, why leds won't turn off after operation?

I wroted a code, whitch turns on leds by the date and color from the txt file. 我写了一个代码,根据txt文件中的日期和颜色打开了led。 If the date is correct leds turns on, but when correct time passes leds wont turn off, they still glowing until next date. 如果日期正确,LED会打开,但经过正确的时间后LED不会关闭,它们仍会发光直到下一个日期。 So, why leds won't turn off, where is the problem? 那么,为什么LED灯不会熄灭,问题出在哪里呢? Please help, I have tried almost everything. 请帮助,我已经尝试了几乎所有东西。

import sys
import time
import datetime
import RPi.GPIO as GPIO
import SDL_DS1307

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)



LED_R = 17
LED_G = 27
LED_B = 22


GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)


filename = time.strftime("%Y-%m-%d%H:%M:%SRTCTest") + ".txt"
starttime = datetime.datetime.utcnow()

ds1307 = SDL_DS1307.SDL_DS1307(1, 0x68)
ds1307.write_now()

while True:
    currenttime = datetime.datetime.utcnow()
    deltatime = currenttime - starttime
    data=time.strftime("%Y"+"%m"+"%d"+"%H"+"%M")
    with open('data.txt') as f:
            for line in f:
                    parts=line.split()                               
                    if parts[0]<=(data)<=parts[1]:
                            if parts[2]=='raudona':
                                    GPIO.putput(LED_R, False)
                                    GPIO.putput(LED_G, True)
                                    GPIO.putput(LED_B, True)
                            elif parts[2]=='zalia':
                                    GPIO.putput(LED_R, True)
                                    GPIO.putput(LED_G, False)
                                    GPIO.putput(LED_B, True)
                            elif parts[2]=='melyna':
                                    GPIO.putput(LED_R, True)
                                    GPIO.putput(LED_G, True)
                                    GPIO.putput(LED_B, False)
                            elif parts[2]=='geltona':
                                    GPIO.putput(LED_R, False)
                                    GPIO.putput(LED_G, True)
                                    GPIO.putput(LED_B, False) 
                            elif parts[2]=='zydra':
                                    GPIO.putput(LED_R, True)
                                    GPIO.putput(LED_G, False)
                                    GPIO.putput(LED_B, False)
                            elif parts[2]=='violetine':
                                    GPIO.putput(LED_R, False)
                                    GPIO.putput(LED_G, False)
                                    GPIO.putput(LED_B, True)
                            elif parts[2]=='balta':
                                    GPIO.putput(LED_R, False)
                                    GPIO.putput(LED_G, False)
                                    GPIO.putput(LED_B, False)

            time.sleep(10.0)

What a nice opportunity to use the for-else construct . 使用for-else构造的绝佳机会。

If an instruction to turn LED lights on is found, turn the LEDs on and then break from the loop, because the task is completed. 如果找到打开LED灯的指令,请打开LED,然后从循环中断开,因为该任务已完成。

For the case, no instruction was found, ie no break was executed, add the else suite to the loop and turn off all LEDs there. 对于这种情况,未找到指令,即未执行break ,将else套件添加到循环中并关闭那里的所有LED。

            for line in f:
                parts=line.split()
                if parts[0]<=(data)<=parts[1]:
                    if parts[2]=='raudona':
                        GPIO.putput(LED_R, False)
                        GPIO.putput(LED_G, True)
                        GPIO.putput(LED_B, True)
                        break
                    elif parts[2]=='zalia':
                        GPIO.putput(LED_R, True)
                        GPIO.putput(LED_G, False)
                        GPIO.putput(LED_B, True)
                        break
                    # elif .....
            else:
                 GPIO.putput(LED_R, False)
                 GPIO.putput(LED_G, False)
                 GPIO.putput(LED_B, False)

(code not tested, all my raspberries are in the garden) (代码未经测试,我所有的覆盆子都在花园里)

Hey brother I believe I have found the answer to your problem. 嘿兄弟,我相信我已经找到您问题的答案。 Okay here is what you will do. 好的,这就是您要做的。 THIS IS A THOROUGH GUIDE ON DEBUGGING A BLINKING LED 这是调试LED闪烁的彻底指南

Endeavour to go through the guide as it contains useful information on solving the issue that you are experiencing. 请努力阅读该指南,因为该指南包含有关解决所遇到问题的有用信息。

You see Raspberry Pi is a very interesting and unique piece of device 您会看到Raspberry Pi是非常有趣且独特的设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM