#!/usr/bin/env python

import time

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

# These are the pins in the order they appear on the relay
PINS = [
        26, # 1
        19, # 2
        6,  # 3
        13, # 4
        5,  # 5
        20, # 6
        16, # 7
        21, # 8
        ]

def init():
    # This sets the pin mode to OUTPUT and HIGH on all pins. This will close the relay. We call this again at the end
    # end to make sure our relays are all closed if there's a failure
    for pin in PINS:
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.HIGH)

def switch():
    pos = 0
    while True:
        for pin in PINS:
            pos += 1
            print("Attempting relay %s (pin %s)" % (pos, pin))
            GPIO.output(pin, GPIO.LOW)
            time.sleep(1)
            GPIO.output(pin, GPIO.HIGH)

try:
    init()
    switch()
finally:
    # Make sure all of the pins are properly closed and the relays off
    init()
    GPIO.cleanup()
