Program to raise blinds - up.py

# up.py  python3 program to raise blinds

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

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

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

# if on vacation or windy a stopfile prevents program running
if ( stopfile_exists):
    sys.exit('Warning cannot raise blind because of stopfile at: /home/pi/shade/stop.status')

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

shutil.move(downfile, raisefile)
sys.stdout.write ('raise ')

#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)

# set relays ready to raise
motor_off(channel1)
motor_off(channel2)

# wait saftey time of 1/10 second for relays
waitfor (.1)

# turn on power switch 
motor_on(channel3)
waitfor (traveltime)

# turn off power switch
motor_off(channel3)
waitfor (.1)


GPIO.cleanup()
shutil.move(raisefile, upfile)