Advertisement
Guest User

Flowing LED Lights

a guest
Mar 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5.  
  6. # Set 8 Pins for 8 LEDs.
  7. LedPins = [17, 18, 27, 22, 23, 24, 25, 4]
  8.  
  9. # Define a function to print message at the beginning
  10. def print_message():
  11.         print ("========================================")
  12.         print ("|                8 LEDs                |")
  13.         print ("|    ------------------------------    |")
  14.         print ("|         LED0 connect to GPIO0        |")
  15.         print ("|         LED1 connect to GPIO1        |")
  16.         print ("|         LED2 connect to GPIO2        |")
  17.         print ("|         LED3 connect to GPIO3        |")
  18.         print ("|         LED4 connect to GPIO4        |")
  19.         print ("|         LED5 connect to GPIO5        |")
  20.         print ("|         LED6 connect to GPIO6        |")
  21.         print ("|         LED7 connect to GPIO7        |")
  22.         print ("|                                      |")
  23.         print ("|            Flow LED effect           |")
  24.         print ("|                                      |")
  25.         print ("|                            SunFounder|")
  26.         print ("========================================\n")
  27.         print 'Program is running...'
  28.         print 'Please press Ctrl+C to end the program...'
  29.         raw_input ("Press Enter to begin\n")
  30.  
  31. # Define a setup function for some setup
  32. def setup():
  33.         # Set the GPIO modes to BCM Numbering
  34.         GPIO.setmode(GPIO.BCM)
  35.         # Set all LedPin's mode to output,
  36.         # and initial level to High(3.3v)
  37.         GPIO.setup(LedPins, GPIO.OUT, initial=GPIO.HIGH)
  38.  
  39. # Define a main function for main process
  40. def main():
  41.         # Print messages
  42.         print_message()
  43.         leds = ['-', '-', '-', '-', '-', '-', '-', '-']
  44.  
  45.         while True:
  46.                 # Turn LED on from left to right
  47.                 print "From left to right."
  48.                 for pin in LedPins:
  49.                         #print pin
  50.                         GPIO.output(pin, GPIO.LOW)
  51.                         leds[LedPins.index(pin)] = 0    # Show which led is on
  52.                         print leds
  53.                         time.sleep(0.5)
  54.                         GPIO.output(pin, GPIO.HIGH)
  55.                         leds[LedPins.index(pin)] = '-'  # Show the led is off
  56.  
  57.                 # Turn LED off from right to left
  58.                 print "From right to left."
  59.                 for pin in reversed(LedPins):
  60.                         #print pin
  61.                         GPIO.output(pin, GPIO.LOW)
  62.                         leds[LedPins.index(pin)] = 0    # Show which led is on
  63.                         print leds
  64.                         time.sleep(0.5)
  65.                         GPIO.output(pin, GPIO.HIGH)
  66.                         leds[LedPins.index(pin)] = '-'  # Show the led is off
  67.  
  68. # Define a destroy function for clean up everything after
  69. # the script finished
  70. def destroy():
  71.         # Turn off all LEDs
  72.         GPIO.output(LedPins, GPIO.HIGH)
  73.         # Release resource
  74.         GPIO.cleanup()
  75.  
  76. # If run this script directly, do:
  77. if __name__ == '__main__':
  78.         setup()
  79.         try:
  80.                 main()
  81.         # When 'Ctrl+C' is pressed, the child program
  82.         # destroy() will be  executed.
  83.         except KeyboardInterrupt:
  84.                 destroy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement