piJake
Posts: 12
Joined: Thu Feb 20, 2014 8:27 pm
Contact: Website

Help!

Sun Mar 02, 2014 2:11 pm

Hello
I have had my GPIO board for a while now and I can turn LEDS on and off again but is there a way that instead of typing:
GPIO.output(7,True)
To turn it on is there a way that I can type 'on' to run that section of code and also the same goes for the off command.

Can anyone help

Thanks

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Help!

Sun Mar 02, 2014 2:22 pm

You can use HIGH & LOW or true & false or 0 & 1.
You can equate on to 1 and off to 0

Code: Select all

import RPi.GPIO as GPIO
import time
import sys

on = 1
off = 0

pin = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)

for i in range(0,999):
    sys.stdout.write("\rBlinking ...")
    sys.stdout.flush()
    GPIO.output(pin,on)
    time.sleep(0.5)
    sys.stdout.write("\r         ...")
    sys.stdout.flush()
    GPIO.output(pin,off)
    time.sleep(0.5)

sys.stdout.write("\n")
sys.stdout.flush()
print "Done"
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

piJake
Posts: 12
Joined: Thu Feb 20, 2014 8:27 pm
Contact: Website

Re: Help!

Sun Mar 02, 2014 2:25 pm

Thank you very much for your help.

Return to “Python”