1

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-- Aug 18 '16 at 20:46
  • Google, was my teacher – Vettehra Aug 19 '16 at 18:21
  • If you replace all numbers by the LED_x equivalent, you should see the error. – ott-- Aug 19 '16 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 Aug 19 '16 at 18:38
1

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 Aug 16 '16 at 15:30
  • Also looks like a typo in the last line, it says PIO.output(17, 21, 22, True) and should probably be GPIO.output(...). – Ron Beyer Aug 16 '16 at 15:31
  • my fault :D Fixed – Vettehra Aug 16 '16 at 15:34
  • But still not working :/ – Vettehra Aug 16 '16 at 15:40
  • 1
    You need to edit your question and say what isn't working. No point in us guessing. – joan Aug 16 '16 at 16:03
0

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 Aug 18 '16 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.