Created
January 14, 2017 03:13
-
-
Save xtacocorex/124a34b6e8c15dd4312368cfcbda5c46 to your computer and use it in GitHub Desktop.
Script for Toggling a CHIP GPIO to turn on/of Relay that powers a Pinewood Derby Car
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Python Controlled, CHIP Powered Pinewood Derby Car | |
# Robert Wolterman, 2017 | |
# Module Imports | |
import CHIP_IO.GPIO as GPIO | |
# Global Variables | |
PIN_TO_USE = "XIO-P0" | |
# Functions | |
def Setup(pin): | |
# We setup the GPIO we want as an output | |
GPIO.setup(pin, GPIO.OUT) | |
def Cleanup(pin): | |
# We want to clean everything up | |
GPIO.cleanup(pin) | |
def ToggleRelay(pin, state): | |
# We are assuming that the relay needs to go | |
# high to enable and low to disable | |
if state: | |
print("\n* STARTING THE MOTOR *\n") | |
GPIO.output(pin, GPIO.HIGH) | |
else: | |
print("\n* STOPPING THE MOTOR *\n") | |
GPIO.output(pin, GPIO.LOW) | |
def GetUserOptionAndDoTask(pin): | |
# Here we're going to print our menu to start/stop the relay | |
print("** CHIPWOOD DERBY **") | |
print("What would you like to do: ") | |
print(" 1 - START!") | |
print(" 2 - STOP!") | |
print(" q - Quit") | |
myopt = raw_input("Enter Option: ") | |
while myopt not in ["1", "2", "q", "Q"]: | |
print("** INVALID OPTION **") | |
myopt = raw_input("Enter Option: ") | |
# We make it here and things are good | |
if myopt == "1": | |
# We want to start the motor | |
ToggleRelay(pin, True) | |
rtnval = False | |
elif myopt == "2": | |
# We want to stop the motor | |
ToggleRelay(pin, False) | |
rtnval = False | |
elif myopt in ["q", "Q"]: | |
rtnval = True | |
# Return | |
return rtnval | |
def Main(): | |
# Here is where we're going to run our main loop | |
# First we'll setup the pin | |
Setup(PIN_TO_USE) | |
# Now we start our run loop | |
# We run while the look returns False | |
# once the option functions returns True | |
# we exit the loop, cleanup the GPIO, and then exit | |
dead = False | |
while not dead: | |
dead = GetUserOptionAndDoTask(PIN_TO_USE) | |
# If we get here, we hit q | |
print("\n* EXITING *") | |
Cleanup(PIN_TO_USE) | |
# ============================ | |
if __name__ == "__main__": | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment