Advertisement
phoenix332

LED Light Program

Mar 8th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.36 KB | None | 0 0
  1. # NeoPixel library strandtest example
  2. # Author: Tony DiCola (tony@tonydicola.com)
  3. #
  4. # Direct port of the Arduino NeoPixel library strandtest example.  Showcases
  5. # various animations on a strip of NeoPixels.
  6. import time
  7. import random
  8. from array import array
  9.  
  10. from neopixel import *
  11.  
  12.  
  13. # LED strip configuration:
  14. LED_COUNT      = 150      # Number of LED pixels.
  15. LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
  16. LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
  17. LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
  18. LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
  19. LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
  20.  
  21.  
  22. # Define functions which animate LEDs in various ways.
  23.  
  24.                
  25.        
  26. def fullStrip(strip,numColors,colorArray,ifMoving):
  27.         for i in range(0-1-ifMoving-numColors,strip.numPixels(),numColors):
  28.                 for j in range(0,numColors):
  29.                         color=colorArray[j]
  30.                         i= i+1
  31.                         strip.setPixelColor(i,color)                        
  32.         strip.show()
  33.        
  34.  
  35. def buildUp(strip, numColors,colorArray):
  36.         h = 0;
  37.         for i in range(0,strip.numPixels()):
  38.                 for k in range(0,numColors,1):
  39.                         for j in range(0,(strip.numPixels()-h)):
  40.                                 strip.setPixelColor(j,colorArray[k])
  41.                                 strip.setPixelColor((j-1),0)
  42.                                 strip.show()
  43.                         h += 1
  44.  
  45. def shootingStar(strip, numColors,colorArray, speed):
  46.         k = random.randint(0,numColors-1)
  47.         for i in range(0,(strip.numPixels()+1)):
  48.                 strip.setPixelColor(i,colorArray[k])
  49.                 strip.setPixelColor((i-1),0)
  50.                 strip.show()
  51.                 time.sleep(speed)
  52.  
  53.                
  54. def lightning(strip, numColors, colorArray, speed):
  55.         i = random.randint(0,(strip.numPixels()/3))
  56.         k = random.randint(0,numColors-1)
  57.         finalColor=colorArray[k]
  58.  
  59.         for j in range(0,3):
  60.                 strip.setPixelColor(i*3+j, finalColor)
  61.         strip.show()
  62.         time.sleep(speed)
  63.         for j in range(0,3):
  64.                 strip.setPixelColor(i*3+j, 0)
  65.  
  66. def every4(strip, color, speed):
  67.         for j in range (0,4):
  68.                 for i in range(j,strip.numPixels(),4):
  69.                         strip.setPixelColor(i-1,0)
  70.                         strip.setPixelColor(i,color)
  71.                         strip.setPixelColor(i+1,0)
  72.                         strip.setPixelColor(i+2,0)
  73.                         strip.setPixelColor(i+3,0)
  74.                 strip.show()
  75.                 time.sleep(speed)
  76.                        
  77. def colorWipe(strip, numColors, colorArray, speed):
  78.         for j in range(0,numColors):
  79.                 for i in range(strip.numPixels()):
  80.                         strip.setPixelColor(i, colorArray[j])
  81.                         strip.show()
  82.                         time.sleep(speed)
  83.  
  84. def strobe(strip, numColors, colorArray, speed):
  85.         for j in range(0,numColors):
  86.                 for i in range(0,strip.numPixels()):
  87.                         strip.setPixelColor(i,colorArray[j])
  88.                 strip.show()
  89.                 time.sleep(speed)
  90.                 for i in range(0,strip.numPixels()):
  91.                         strip.setPixelColor(i,0)
  92.                 strip.show()
  93.                 time.sleep(speed)
  94.  
  95. def askForInt(question,low,high):
  96.         entered = int(input (question))
  97.         while (entered>high or entered<low):
  98.                 print"Invalid Response. Please enter a number between ",low," and ",high
  99.                 entered = int(input (question))                        
  100.         return entered
  101.  
  102. def pickSpeed(pick):
  103.         if pick==1:
  104.                 return .15
  105.         elif pick==2:
  106.                 return .1
  107.         elif pick==3:
  108.                 return .075
  109.         elif pick==4:
  110.                 return .05
  111.         elif pick==5:
  112.                 return .025
  113.        
  114.  
  115.  
  116. # Main program logic follows:
  117. if __name__ == '__main__':
  118.     # Create NeoPixel object with appropriate configuration.
  119.     strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  120.     # Intialize the library (must be called once before other functions).
  121.     strip.begin()
  122.         while True:
  123.                 mode= askForInt("(1)Stationary Strip (2)Moving Strip (3)BuildUp (4)Lightning (5)Color Wipe (6)Every Four (7)Strobe (8)Shooting Star ",1,8)
  124.  
  125. ################################ Stationary Strip #################################
  126.                 if mode==1:
  127.                         presets=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  128.                         if presets == 1:
  129.                                 colorR=askForInt("What is R? ",0,255)
  130.                                 colorG=askForInt("What is G? ",0,255)
  131.                                 colorB=askForInt("What is B? ",0,255)
  132.                                 colorArray=[Color(colorG,colorR,colorB)]
  133.                                 fullStrip(strip,1,colorArray,0)
  134.                         elif presets==2:
  135.                                 colorAmount= askForInt("How many colors? ",2,strip.numPixels())
  136.                                 colorArray = [Color(0,0,0)]*colorAmount
  137.                                 for i in range(0,colorAmount):
  138.                                         print "Enter RGB code for color" ,i
  139.                                         colorR=askForInt("What is R? ",0,255)
  140.                                         colorG=askForInt("What is G? ",0,255)
  141.                                         colorB=askForInt("What is B? ",0,255)
  142.                                         colorArray [i]=Color(colorG,colorR,colorB)
  143.                                 fullStrip(strip,colorAmount,colorArray,0)
  144.                         elif presets==3:
  145.                                 colorAmount=7
  146.                                 colorArray = [Color(0,255,0),Color(50,255,0),Color(130,255,0),Color(255,0,0),Color(255,0,255),Color(0,0,255),Color(0,255,255)]
  147.                                 fullStrip(strip,colorAmount,colorArray,0)
  148. ################################# Moving Strip #######################################
  149.  
  150.                 if mode==2:
  151.                         presets=askForInt("(1)Custom (2)Rainbow ",1,2)
  152.                         if presets==1:
  153.                                 colorAmount= askForInt("How many colors? ",2,strip.numPixels())
  154.                                 colorArray = [Color(0,0,0)]*colorAmount
  155.                                 for i in range(0,colorAmount):
  156.                                         print "Enter RGB code for color" ,i
  157.                                         colorR=askForInt("What is R? ",0,255)
  158.                                         colorG=askForInt("What is G? ",0,255)
  159.                                         colorB=askForInt("What is B? ",0,255)
  160.                                         colorArray [i]=Color(colorG,colorR,colorB)
  161.                                 pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  162.                                 speed=pickSpeed(pickedSpeed)
  163.                                 while True:
  164.                                         for g in range(0,colorAmount):
  165.                                                 fullStrip(strip,colorAmount,colorArray,g)
  166.                                                 time.sleep(speed)
  167.                         if presets==2:
  168.                                 colorAmount=7
  169.                                 colorArray = [Color(0,255,0),Color(50,255,0),Color(130,255,0),Color(255,0,0),Color(255,0,255),Color(0,0,255),Color(0,255,255)]
  170.                                 pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  171.                                 speed=pickSpeed(pickedSpeed)
  172.                                 while True:
  173.                                         for g in range(0,colorAmount):
  174.                                                 fullStrip(strip,colorAmount,colorArray,g)
  175.                                                 time.sleep(speed)
  176.  
  177. ################################# Build Up #######################################
  178.                                                
  179.                 if mode==3:
  180.                         buildUpMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  181.                         if buildUpMode==1:
  182.                                 colorR=askForInt("What is R? ",0,255)
  183.                                 colorG=askForInt("What is G? ",0,255)
  184.                                 colorB=askForInt("What is B? ",0,255)
  185.                                 colorArray= [Color(colorG,colorR,colorB)]
  186.                                 while True:
  187.                                         buildUp(strip, 1, colorArray)
  188.                         if buildUpMode==2:
  189.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  190.                                 colorArray = [Color(0,0,0)]*colorAmount
  191.                                 for i in range(0,colorAmount):
  192.                                         print "Enter RGB code for color" ,i
  193.                                         colorR=askForInt("What is R? ",0,255)
  194.                                         colorG=askForInt("What is G? ",0,255)
  195.                                         colorB=askForInt("What is B? ",0,255)
  196.                                         colorArray [i]=Color(colorG,colorR,colorB)
  197.                                 while True:
  198.                                         buildUp(strip,colorAmount, colorArray)
  199.  
  200.                         elif buildUpMode==3:
  201.                                 colorArray= [Color(0,255,0), Color(50,255,0), Color(130,255,0), Color(255,0,0), Color(255,0,255), Color(0,0,255), Color(0,255,255)]
  202.                                 while True:
  203.                                           buildUp(strip, 7, colorArray)
  204.        
  205. ################################# Lightning White ##################################
  206.                 if mode==4:
  207.                         lightningMode=askForInt("(1)Single Color (2)Custom (3)White (4)Rainbow ",1,4)
  208.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  209.                         speed=pickSpeed(pickedSpeed)
  210.                         if lightningMode==1:
  211.                                 colorR=askForInt("What is R? ",0,255)
  212.                                 colorG=askForInt("What is G? ",0,255)
  213.                                 colorB=askForInt("What is B? ",0,255)
  214.                                 colorArray=[Color(colorG,colorR,colorB)]
  215.                                 while True:
  216.                                         lightning(strip, 1, colorArray, speed)
  217.                         elif lightningMode==2:
  218.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()/3))
  219.                                 colorArray = [Color(0,0,0)]*colorAmount
  220.                                 for i in range(0,colorAmount):
  221.                                         print "Enter RGB code for color" ,i
  222.                                         colorR=askForInt("What is R? ",0,255)
  223.                                         colorG=askForInt("What is G? ",0,255)
  224.                                         colorB=askForInt("What is B? ",0,255)
  225.                                         colorArray [i]=Color(colorG,colorR,colorB)
  226.                                 while True:
  227.                                         lightning(strip,colorAmount, colorArray, speed)
  228.                                
  229.                         elif lightningMode==3:
  230.                                 while True:
  231.                                         colorArray= [Color(127,127,127)]
  232.                                         lightning(strip,1,colorArray, speed)
  233.                         elif lightningMode==4:
  234.                                 while True:
  235.                                         colorArray = [Color(0,255,0),Color(50,255,0),Color(130,255,0),Color(255,0,0),Color(255,0,255),Color(0,0,255),Color(0,255,255)]
  236.                                         lightning(strip,7,colorArray, speed)
  237.  
  238.        
  239. ############################### Color Wipe ################################
  240.                 if mode==5:
  241.                         buildUpMode=askForInt("(1)Custom (2)Rainbow ",1,2)
  242.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  243.                         speed=pickSpeed(pickedSpeed)
  244.                         if buildUpMode==1:
  245.                                 colorAmount = askForInt("How many colors? ",2,(strip.numPixels()))
  246.                                 colorArray = [Color(0,0,0)]*colorAmount
  247.                                 for i in range(0,colorAmount):
  248.                                         print "Enter RGB code for color" ,i
  249.                                         colorR=askForInt("What is R? ",0,255)
  250.                                         colorG=askForInt("What is G? ",0,255)
  251.                                         colorB=askForInt("What is B? ",0,255)
  252.                                         colorArray [i]=Color(colorG,colorR,colorB)
  253.                                 while True:
  254.                                         buildUp(strip,colorAmount, colorArray, speed)
  255.  
  256.                         elif buildUpMode==2:
  257.                                 colorArray= [Color(0,255,0), Color(50,255,0), Color(130,255,0), Color(255,0,0), Color(255,0,255), Color(0,0,255), Color(0,255,255)]
  258.                                 while True:
  259.                                           colorWipe(strip, 7, colorArray, speed)
  260.                
  261. ################################ Every Four #########################                
  262.                 if mode==6:
  263.                         pickedSpeed=askForInt("1-5 (1 being slowest) ",1,5)
  264.                         speed=pickSpeed(pickedSpeed)
  265.                         colorR=askForInt("What is R? ",0,255)
  266.                         colorG=askForInt("What is G? ",0,255)
  267.                         colorB=askForInt("What is B? ",0,255)
  268.                         color=Color(colorG,colorR,colorB)
  269.                         while True:
  270.                                 every4(strip,color,speed)
  271. ############################### Strobe ###############################3
  272.                 if mode==7:
  273.                         strobeMode=askForInt("(1)One Color (2)Custom (3)White (4)Rainbow ",1,4)
  274.                         strobeSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  275.                         speed=pickSpeed(strobeSpeed)
  276.                         if strobeMode==1:
  277.                                 colorR=askForInt("What is R? ",0,255)
  278.                                 colorG=askForInt("What is G? ",0,255)
  279.                                 colorB=askForInt("What is B? ",0,255)
  280.                                 colorArray=[Color(colorG,colorR,colorB)]
  281.                                 while True:
  282.                                         strobe(strip, 1, colorArray,speed)
  283.                         elif strobeMode==2:
  284.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  285.                                 colorArray = [Color(0,0,0)]*colorAmount
  286.                                 for i in range(0,colorAmount):
  287.                                         print "Enter RGB code for color" ,i
  288.                                         colorR=askForInt("What is R? ",0,255)
  289.                                         colorG=askForInt("What is G? ",0,255)
  290.                                         colorB=askForInt("What is B? ",0,255)
  291.                                         colorArray [i]=Color(colorG,colorR,colorB)
  292.                                 while True:
  293.                                         strobe(strip,colorAmount, colorArray,speed)
  294.                                
  295.                         elif strobeMode==3:
  296.                                 while True:
  297.                                         colorArray= [Color(127,127,127)]
  298.                                         strobe(strip,1,colorArray,speed)
  299.                         elif strobeMode==4:
  300.                                 while True:
  301.                                         colorArray = [Color(0,255,0),Color(50,255,0),Color(130,255,0),Color(255,0,0),Color(255,0,255),Color(0,0,255),Color(0,255,255)]
  302.                                         strobe(strip,7,colorArray,speed)
  303.  
  304.                                        
  305. ######################################## Shooting Star #######################3
  306.                        
  307.                 if mode==8:
  308.                         shootingMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  309.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  310.                         speed=pickSpeed(pickedSpeed)
  311.                         if shootingMode==1:
  312.                                 colorR=askForInt("What is R? ",0,255)
  313.                                 colorG=askForInt("What is G? ",0,255)
  314.                                 colorB=askForInt("What is B? ",0,255)
  315.                                 colorArray= [Color(colorG,colorR,colorB)]
  316.                                 while True:
  317.                                         shootingStar(strip, 1, colorArray, speed)
  318.                         if shootingMode==2:
  319.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  320.                                 colorArray = [Color(0,0,0)]*colorAmount
  321.                                 for i in range(0,colorAmount):
  322.                                         print "Enter RGB code for color" ,i
  323.                                         colorR=askForInt("What is R? ",0,255)
  324.                                         colorG=askForInt("What is G? ",0,255)
  325.                                         colorB=askForInt("What is B? ",0,255)
  326.                                         colorArray [i]=Color(colorG,colorR,colorB)
  327.                                 while True:
  328.                                         shootingStar(strip,colorAmount, colorArray, speed)
  329.  
  330.                         elif shootingMode==3:
  331.                                 colorArray= [Color(0,255,0), Color(50,255,0), Color(130,255,0), Color(255,0,0), Color(255,0,255), Color(0,0,255), Color(0,255,255)]
  332.                                 while True:
  333.                                           shootingStar(strip, 7, colorArray, speed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement