Example #1
0
# Create a new instance of the GPIOGalileoGen2 class.
# Setting debug=True gives information about the interaction with sysfs.
gpio = GPIO(debug=False)
pin = 3
brightness = 0
fadeAmount = 5

# Set pin 3 to be used as a PWM pin.
print 'Setting up pin %d' % pin
gpio.pinMode(pin, gpio.PWM)

print 'Fading pin %d now...' % pin
try:
    while(True):
        # Write brightness to the pin. The value must be between 0 and 255.
        gpio.analogWrite(pin, brightness)

        # Increment or decrement the brightness.
        brightness = brightness + fadeAmount

        # If the brightness has reached its maximum or minimum value swap
        # fadeAmount sign so we can start fading the led on the other direction.
        if brightness == 0 or brightness == 255:
            fadeAmount = -fadeAmount

        # Sleep for a while.
        time.sleep(0.03)

# When you get tired of seeing the led fading kill the loop with Ctrl-C.
except KeyboardInterrupt:
    # Leave the led turned off.