Program to lower Blind - Down.py



# Program down.py
# python3 down.py
import time
import RPi.GPIO as GPIO
import sys
import shutil
from os.path import exists

channel1 = 17   # motor left 
channel2 = 27   # motor right 
channel3 = 22   # power switch 
traveltime = 86 # number of seconds to move blind

upfile = '/home/pi/shade/up.status'        # existance means blinds are currently up 
downfile = '/home/pi/shade/down.status'    # existance means blinds are currently down 
raisefile = '/home/pi/shade/raise.status'  # existance means blinds were partly raised 
lowerfile = '/home/pi/shade/lower.status'  # existance means blinds were partly lowered 
stopfile = '/home/pi/shade/stop.status'    # existance means disable movement of blinds (windy)

stopfile_exists = exists(stopfile)
upfile_exists = exists(upfile)
downfile_exists = exists(downfile)

if ( stopfile_exists):
        sys.exit('Warning cannot lower blind because of stopfile at: /home/pi/shade/stop.status')

if (not upfile_exists):
    sys.exit('Error cannot lower blind that is not up: /home/pi/shade/up.status')

sys.stdout.write ('lower ')
shutil.move(upfile, lowerfile )

#GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel1, GPIO.OUT)
GPIO.setup(channel2, GPIO.OUT)
GPIO.setup(channel3, GPIO.OUT)

def motor_off(pin):
    GPIO.output(pin, GPIO.HIGH)  # Turn on
    print ("off")

def motor_on(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn off
    print ("on")

def waitfor(seconds):
    if (seconds >= 1):
        for x in range(seconds):
            if (x % 10 == 0):
                sys.stdout.write (':')
            else:
                sys.stdout.write ('.')
            sys.stdout.flush()
            time.sleep (1)
    else: 
        time.sleep (seconds)

# reverse
motor_on(channel1)
motor_on(channel2)

# turn on
waitfor (.1)
motor_on(channel3)
waitfor (traveltime)
# turn off
motor_off(channel3)
waitfor (.1)

shutil.move(lowerfile, downfile )
GPIO.cleanup()