I'm new to Python and the Raspberry Pi. I'm trying to shorten my code and use for
loops for repetitive parts in my code, like changing all pins to high and low.
I'm trying to use a for
loop to access pins. Is this even possible?
import RPi.GPIO as GPIO
from time import sleep
R1=22
R2=10
R3=9
R4=11
GPIO.setup(R1, GPIO.OUT)
GPIO.setup(R2, GPIO.OUT)
GPIO.setup(R3, GPIO.OUT)
GPIO.setup(R4, GPIO.OUT)
for x in range(1, 5):
print "We're on time %d" % (x)
GPIO.output(R + %d % (x), GPIO.HIGH)
sleep(1)
GPIO.output(R + %d % (x), GPIO.LOW)
sleep(1)
GPIO.cleanup()