Fixed code again, now leds working as they have to, i don't know why all Trues and Falses are upside down, because, where should be True is False and vice versa.
But there is one problem: if leds were turned on by earlier and don't have to glow by condition, they won't turn off until starts other (new) condition when they have to turn on. Where is the problem?
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]=='100':
GPIO.putput(LED_R, False)
GPIO.putput(LED_G, True)
GPIO.putput(LED_B, True)
elif parts[2]=='010':
GPIO.putput(LED_R, True)
GPIO.putput(LED_G, False)
GPIO.putput(LED_B, True)
elif parts[2]=='001':
GPIO.putput(LED_R, True)
GPIO.putput(LED_G, True)
GPIO.putput(LED_B, False)
elif parts[2]=='110':
GPIO.putput(LED_R, False)
GPIO.putput(LED_G, True)
GPIO.putput(LED_B, False)
elif parts[2]=='101':
GPIO.putput(LED_R, True)
GPIO.putput(LED_G, False)
GPIO.putput(LED_B, False)
elif parts[2]=='101':
GPIO.putput(LED_R, False)
GPIO.putput(LED_G, False)
GPIO.putput(LED_B, True)
elif parts[2]=='111':
GPIO.putput(LED_R, False)
GPIO.putput(LED_G, False)
GPIO.putput(LED_B, False)
else:
GPIO.output(LED_R, True)
GPIO.output(LED_G, True)
GPIO.output(LED_B, True)
time.sleep(1.0)
-
You have defined LED_R G B as 17 27 22. Why do you still use 17 27 22 in your following lines? Who was your programming teacher?ott--– ott--2016年08月18日 20:46:11 +00:00Commented Aug 18, 2016 at 20:46
-
Google, was my teacherVettehra– Vettehra2016年08月19日 18:21:10 +00:00Commented Aug 19, 2016 at 18:21
-
If you replace all numbers by the LED_x equivalent, you should see the error.ott--– ott--2016年08月19日 18:23:10 +00:00Commented Aug 19, 2016 at 18:23
-
I have corrected the code, 17, 22, 27 was replaced by LER_R, LED_G, LED_B , but all the same as was, i don't know, how to edit the code, if time is ended leds turns off, because, when if ends leds still on, they have to turn off, but how?Vettehra– Vettehra2016年08月19日 18:38:43 +00:00Commented Aug 19, 2016 at 18:38
2 Answers 2
You are calling GPIO.cleanup immediately after writing to the LEDs. That switches all the GPIO you are using to be inputs rather than outputs.
I am surprised you don't get error messages when you run the code. Perhaps the GPIO.setwarnings call is hiding the errors.
The simplest fix is to delete the GPIO.cleanup line.
-
Deleting GPIO.cleanup() won't help After that i enabled error messages, raspi says: 'RuntimeWarning: This channel is already in use...'Vettehra– Vettehra2016年08月16日 15:30:16 +00:00Commented Aug 16, 2016 at 15:30
-
Also looks like a typo in the last line, it says
PIO.output(17, 21, 22, True)
and should probably beGPIO.output(...)
.Ron Beyer– Ron Beyer2016年08月16日 15:31:51 +00:00Commented Aug 16, 2016 at 15:31 -
-
But still not working :/Vettehra– Vettehra2016年08月16日 15:40:34 +00:00Commented Aug 16, 2016 at 15:40
-
1You need to edit your question and say what isn't working. No point in us guessing.joan– joan2016年08月16日 16:03:17 +00:00Commented Aug 16, 2016 at 16:03
You read through each line of a file, turning on various LEDs, but each loop happens so fast that you barely see anything happen. You need to move your sleep
statement.
you can put it here:
for line in f:
time.sleep(1.0)
...or better yet, put it at the very bottom of the for()
loop so it sleeps after the line is processed. It should be indented only once inside the loop.
-
Thats not working, mabie better use 'while' than 'if' in first 'if' line?Vettehra– Vettehra2016年08月18日 16:09:09 +00:00Commented Aug 18, 2016 at 16:09