Code debugging, Raspberry pi relays and if statements
<p>I've a problem with Raspberry Pi relays. Earlier I wrote about LED's and the problem was similar, but this time these methods don't work. </p>

<pre><code>#!/usr/bin/env python
import sys
import time
import datetime
import RPi.GPIO as GPIO
import SDL_DS1307

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

Rele1 = 17
Rele2 = 27



GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, 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]&lt;=(data)&lt;=parts[1]:
                                GPIO.output(Rele1, True)
                                GPIO.output(Rele2, False)
                                break
                        else:
                                GPIO.output(Rele1, False)
                                GPIO.output(Rele2, True)
        sleep(0.10)
</code></pre>

<p>I have edited the code a little bit, now just when <code>if</code> is <code>True</code> one channel of relay module, GPIO27, is clicking very fast. I tried changing the GPIOs but the result is the same.</p>

<p>When <code>if</code> is <code>False</code>, then relay works as it should. If I put <code>break</code> after <code>else</code> , the code stops doing .txt file check and if there are more dates, the  program doesn't do anything</p>

<p>You need to do some work on your logic. Think about what happens when the the time is between the values on the third line of data.txt. As your code reads the first  line, the result is false so the outputs are set correspondingly, then on the second line the result is false and the outputs are set for false again, then the third line the result is true so the outputs are set for true. Then the file is immediately read again and the first line result is false, 2nd line false, third line true. So your relays will be set to false-false-true each time the file is read, again and again.</p>

<p>You need to make your code only set the outputs when ANY of the tests is true (a logical OR) and not for the result of each test.</p>

<p>You probably need to do something like work out the OR across all the lines of the file then after finishing the file, if the result is true or false set the outputs accordingly.</p>

<p>And as a tip for debugging, if you had put a simple print statement in each branch of the if statement you would have seen on the console how once one of the lines after the first line has a true result the outputs are being toggled around on every read through the file.</p>

<pre><code>while True:
    currenttime = datetime.datetime.utcnow()
    deltatime = currenttime - starttime
    data=time.strftime("%Y"+"%m"+"%d"+"%H"+"%M")
    with open('data.txt') as f:
        result = False
        for line in f:
            parts=line.split()
            # compare the current time with the part1/part2 from data.txt
            if parts[0]&lt;=(data)&lt;=parts[1]:
                # The current time is between these, so our overall result is going to be True
                result = True
    # depending on the result, set the relays appropriately
    if result:
        print "Result is True"
        GPIO.output(Rele1, True)
        GPIO.output(Rele2, False)
    else:
        print "Result is False"
        GPIO.output(Rele1, False)
        GPIO.output(Rele2, True)
    # delay for a second - no point reading faster than the clock changes
    time.sleep(1)
</code></pre>

<p>`</p>

python raspberry-pi