Advertisement
phoenix332

Untitled

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