Advertisement
phoenix332

Lights With Music

Apr 4th, 2017
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 23.59 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 RPi.GPIO as GPIO
  7. import time
  8. import random
  9. from thread import start_new_thread
  10. from array import array
  11. from neopixel import *
  12.  
  13.  
  14.  
  15. # LED strip configuration:
  16. LED_COUNT      = 150      # Number of LED pixels.
  17. LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!).
  18. LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
  19. LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
  20. LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest
  21. LED_INVERT     = False   # True to invert the signal (when using NPN transistor level shift)
  22.  
  23.  
  24. # Define functions which animate LEDs in various ways.
  25. f = [0]*6
  26. f[0] = 26
  27. f[1] = 13
  28. f[2] = 6
  29. f[3] = 5
  30. f[4] = 16
  31. f[5] = 12
  32. GPIO.setmode(GPIO.BCM)
  33. GPIO.setwarnings(False)
  34. for i in range (0,6):
  35.         GPIO.setup(f[i],GPIO.IN)
  36.                
  37.        
  38. def fullStrip(strip,numColors,colorArray,ifMoving):
  39.         for i in range(0-1-ifMoving-numColors,strip.numPixels(),numColors):
  40.                 for j in range(0,numColors):
  41.                         color=colorArray[j]
  42.                         i= i+1
  43.                         strip.setPixelColor(i,color)                        
  44.         strip.show()
  45.        
  46.  
  47. def buildUp(strip, numColors,colorArray):
  48.         h = 0;
  49.         for i in range(0,strip.numPixels()):
  50.                 for k in range(0,numColors,1):
  51.                         for j in range(0,(strip.numPixels()-h)):
  52.                                 strip.setPixelColor(j,colorArray[k])
  53.                                 strip.setPixelColor((j-1),0)
  54.                                 strip.show()
  55.                         h += 1
  56.  
  57. def shootingStar(strip, numColors,colorArray, speed):
  58.         k = random.randint(0,numColors-1)
  59.         for i in range(0,(strip.numPixels()+1)):
  60.                 global running
  61.                 running = True
  62.                 strip.setPixelColor(i,colorArray[k])
  63.                 strip.setPixelColor((i-1),0)
  64.                 strip.show()
  65.                 time.sleep(speed)
  66.         running = False
  67.                
  68. def lightning(strip, numColors, colorArray, speed):
  69.         i = random.randint(0,(strip.numPixels()/3))
  70.         k = random.randint(0,numColors-1)
  71.         finalColor=colorArray[k]
  72.  
  73.         for j in range(0,3):
  74.                 strip.setPixelColor(i*3+j, finalColor)
  75.         strip.show()
  76.         time.sleep(speed)
  77.         for j in range(0,3):
  78.                 strip.setPixelColor(i*3+j, 0)
  79.  
  80. def every4(strip, color, speed):
  81.         for j in range (0,4):
  82.                 for i in range(j,strip.numPixels(),4):
  83.                         strip.setPixelColor(i-1,0)
  84.                         strip.setPixelColor(i,color)
  85.                         strip.setPixelColor(i+1,0)
  86.                         strip.setPixelColor(i+2,0)
  87.                         strip.setPixelColor(i+3,0)
  88.                 strip.show()
  89.                 time.sleep(speed)
  90.                        
  91. def colorWipe(strip, numColors, colorArray, speed):
  92.         for j in range(0,numColors):
  93.                 for i in range(strip.numPixels()):
  94.                         strip.setPixelColor(i, colorArray[j])
  95.                         strip.show()
  96.                         time.sleep(speed)
  97.  
  98. def strobe(strip, numColors, colorArray, speed):
  99.         for j in range(0,numColors):
  100.                 for i in range(0,strip.numPixels()):
  101.                         strip.setPixelColor(i,colorArray[j])
  102.                 strip.show()
  103.                 time.sleep(speed)
  104.                 for i in range(0,strip.numPixels()):
  105.                         strip.setPixelColor(i,0)
  106.                 strip.show()
  107.                 time.sleep(speed)
  108. def musicSingle(strip,start,red, green, blue):
  109.         for i in range (start, strip.numPixels(),6):
  110.                 strip.setPixelColor(i,Color(green,red,blue))
  111.         strip.show()
  112.         time.sleep(.1)
  113.         for i in range (start, strip.numPixels(),6):
  114.                 strip.setPixelColor(i,Color(0,0,0))
  115.         strip.show()
  116.                
  117. def musicEqual(strip,start,end,red, green, blue):
  118.         for i in range (start, end):
  119.                 strip.setPixelColor(i,Color(green,red,blue))
  120.         strip.show()
  121.         time.sleep(.1)
  122.         for i in range (start, end):
  123.                 strip.setPixelColor(i,Color(0,0,0))
  124.         strip.show()
  125.        
  126.  
  127. def askForInt(question,low,high):
  128.         entered = int(input (question))
  129.         while (entered>high or entered<low):
  130.                 print"Invalid Response. Please enter a number between ",low," and ",high
  131.                 entered = int(input (question))                        
  132.         return entered
  133.  
  134. def pickSpeed(pick):
  135.         if pick==1:
  136.                 return .15
  137.         elif pick==2:
  138.                 return .1
  139.         elif pick==3:
  140.                 return .075
  141.         elif pick==4:
  142.                 return .05
  143.         elif pick==5:
  144.                 return .025
  145.  
  146. def clean(strip):
  147.         for i in range(0,strip.numPixels()):
  148.                 strip.setPixelColor(i,0)
  149.         strip.show()
  150.  
  151. def end():
  152.         s=askForInt("Enter 1 to return to main menu. ",1,1)
  153.         global halt
  154.         halt = True
  155.         print("Please wait for program to finish" )
  156.  
  157. # Main program logic follows:
  158. while True:
  159.     # Create NeoPixel object with appropriate configuration.
  160.     strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
  161.     # Intialize the library (must be called once before other functions).
  162.     strip.begin()
  163.         while True:
  164.                 mode= askForInt("(1)Stationary Strip (2)Moving Strip (3)BuildUp (4)Lightning (5)Color Wipe (6)Every Four (7)Strobe (8)Shooting Star (9)Music ",1,9)
  165.                 halt=False
  166.                 clean(strip)
  167. ################################ Stationary Strip #################################
  168.                 if mode==1:
  169.                         presets=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  170.                         if presets == 1:
  171.                                 colorR=askForInt("What is R? ",0,255)
  172.                                 colorG=askForInt("What is G? ",0,255)
  173.                                 colorB=askForInt("What is B? ",0,255)
  174.                                 colorArray=[Color(colorG,colorR,colorB)]
  175.                                 fullStrip(strip,1,colorArray,0)
  176.                         elif presets==2:
  177.                                 colorAmount= askForInt("How many colors? ",2,strip.numPixels())
  178.                                 colorArray = [Color(0,0,0)]*colorAmount
  179.                                 for i in range(0,colorAmount):
  180.                                         print "Enter RGB code for color" ,i
  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 [i]=Color(colorG,colorR,colorB)
  185.                                 fullStrip(strip,colorAmount,colorArray,0)
  186.                         elif presets==3:
  187.                                 colorAmount=7
  188.                                 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)]
  189.                                 fullStrip(strip,colorAmount,colorArray,0)
  190. ################################# Moving Strip #######################################
  191.  
  192.                 if mode==2:
  193.                         presets=askForInt("(1)Custom (2)Rainbow ",1,2)
  194.                         if presets==1:
  195.                                 colorAmount= askForInt("How many colors? ",2,strip.numPixels())
  196.                                 colorArray = [Color(0,0,0)]*colorAmount
  197.                                 for i in range(0,colorAmount):
  198.                                         print "Enter RGB code for color" ,i
  199.                                         colorR=askForInt("What is R? ",0,255)
  200.                                         colorG=askForInt("What is G? ",0,255)
  201.                                         colorB=askForInt("What is B? ",0,255)
  202.                                         colorArray [i]=Color(colorG,colorR,colorB)
  203.                                 pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  204.                                 speed=pickSpeed(pickedSpeed)
  205.                                 start_new_thread(end,())
  206.                                 while not(halt):
  207.                                         for g in range(0,colorAmount):
  208.                                                 fullStrip(strip,colorAmount,colorArray,g)
  209.                                                 time.sleep(speed)
  210.                         if presets==2:
  211.                                 colorAmount=7
  212.                                 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)]
  213.                                 pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  214.                                 speed=pickSpeed(pickedSpeed)
  215.                                 start_new_thread(end,())
  216.                                 while not(halt):
  217.                                         for g in range(0,colorAmount):
  218.                                                 fullStrip(strip,colorAmount,colorArray,g)
  219.                                                 time.sleep(speed)
  220.  
  221. ################################# Build Up #######################################
  222.                                                
  223.                 if mode==3:
  224.                         buildUpMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  225.                         if buildUpMode==1:
  226.                                 colorR=askForInt("What is R? ",0,255)
  227.                                 colorG=askForInt("What is G? ",0,255)
  228.                                 colorB=askForInt("What is B? ",0,255)
  229.                                 colorArray= [Color(colorG,colorR,colorB)]
  230.                                 start_new_thread(end,())
  231.                                 while not(halt):
  232.                                         buildUp(strip, 1, colorArray)
  233.                         if buildUpMode==2:
  234.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  235.                                 colorArray = [Color(0,0,0)]*colorAmount
  236.                                 for i in range(0,colorAmount):
  237.                                         print "Enter RGB code for color" ,i
  238.                                         colorR=askForInt("What is R? ",0,255)
  239.                                         colorG=askForInt("What is G? ",0,255)
  240.                                         colorB=askForInt("What is B? ",0,255)
  241.                                         colorArray [i]=Color(colorG,colorR,colorB)
  242.                                 start_new_thread(end,())
  243.                                 while not(halt):
  244.                                         buildUp(strip,colorAmount, colorArray)
  245.  
  246.                         elif buildUpMode==3:
  247.                                 start_new_thread(end,())
  248.                                 while not(halt):
  249.                                         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)]
  250.                                         buildUp(strip, 7, colorArray)
  251.        
  252. ################################# Lightning White ##################################
  253.                 if mode==4:
  254.                         lightningMode=askForInt("(1)Single Color (2)Custom (3)White (4)Rainbow ",1,4)
  255.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  256.                         speed=pickSpeed(pickedSpeed)
  257.                         if lightningMode==1:
  258.                                 colorR=askForInt("What is R? ",0,255)
  259.                                 colorG=askForInt("What is G? ",0,255)
  260.                                 colorB=askForInt("What is B? ",0,255)
  261.                                 colorArray=[Color(colorG,colorR,colorB)]
  262.                                 start_new_thread(end,())
  263.                                 while not(halt):
  264.                                         lightning(strip, 1, colorArray, speed)
  265.                         elif lightningMode==2:
  266.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()/3))
  267.                                 colorArray = [Color(0,0,0)]*colorAmount
  268.                                 for i in range(0,colorAmount):
  269.                                         print "Enter RGB code for color" ,i
  270.                                         colorR=askForInt("What is R? ",0,255)
  271.                                         colorG=askForInt("What is G? ",0,255)
  272.                                         colorB=askForInt("What is B? ",0,255)
  273.                                         colorArray [i]=Color(colorG,colorR,colorB)
  274.                                 start_new_thread(end,())
  275.                                 while not(halt):
  276.                                         lightning(strip,colorAmount, colorArray, speed)
  277.                                
  278.                         elif lightningMode==3:
  279.                                 start_new_thread(end,())
  280.                                 while not(halt):
  281.                                         colorArray= [Color(127,127,127)]
  282.                                         lightning(strip,1,colorArray, speed)
  283.                         elif lightningMode==4:
  284.                                 start_new_thread(end,())
  285.                                 while not(halt):
  286.                                         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)]
  287.                                         lightning(strip,7,colorArray, speed)
  288.  
  289.        
  290. ############################### Color Wipe ################################
  291.                 if mode==5:
  292.                         buildUpMode=askForInt("(1)Custom (2)Rainbow ",1,2)
  293.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  294.                         speed=pickSpeed(pickedSpeed)
  295.                         if buildUpMode==1:
  296.                                 colorAmount = askForInt("How many colors? ",2,(strip.numPixels()))
  297.                                 colorArray = [Color(0,0,0)]*colorAmount
  298.                                 for i in range(0,colorAmount):
  299.                                         print "Enter RGB code for color" ,i
  300.                                         colorR=askForInt("What is R? ",0,255)
  301.                                         colorG=askForInt("What is G? ",0,255)
  302.                                         colorB=askForInt("What is B? ",0,255)
  303.                                         colorArray [i]=Color(colorG,colorR,colorB)
  304.                                 start_new_thread(end,())
  305.                                 while not(halt):
  306.                                         colorWipe(strip,colorAmount, colorArray, speed)
  307.  
  308.                         elif buildUpMode==2:
  309.                                 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)]
  310.                                 start_new_thread(end,())
  311.                                 while not(halt):
  312.                                           colorWipe(strip, 7, colorArray, speed)
  313.                
  314. ################################ Every Four #########################                
  315.                 if mode==6:
  316.                         pickedSpeed=askForInt("1-5 (1 being slowest) ",1,5)
  317.                         speed=pickSpeed(pickedSpeed)
  318.                         colorR=askForInt("What is R? ",0,255)
  319.                         colorG=askForInt("What is G? ",0,255)
  320.                         colorB=askForInt("What is B? ",0,255)
  321.                         color=Color(colorG,colorR,colorB)
  322.                         start_new_thread(end,())
  323.                         while not(halt):
  324.                                 every4(strip,color,speed)
  325. ############################### Strobe ###############################3
  326.                 if mode==7:
  327.                         strobeMode=askForInt("(1)One Color (2)Custom (3)White (4)Rainbow ",1,4)
  328.                         strobeSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  329.                         speed=pickSpeed(strobeSpeed)
  330.                         if strobeMode==1:
  331.                                 colorR=askForInt("What is R? ",0,255)
  332.                                 colorG=askForInt("What is G? ",0,255)
  333.                                 colorB=askForInt("What is B? ",0,255)
  334.                                 colorArray=[Color(colorG,colorR,colorB)]
  335.                                 start_new_thread(end,())
  336.                                 while not(halt):
  337.                                         strobe(strip, 1, colorArray,speed)
  338.                         elif strobeMode==2:
  339.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  340.                                 colorArray = [Color(0,0,0)]*colorAmount
  341.                                 for i in range(0,colorAmount):
  342.                                         print "Enter RGB code for color" ,i
  343.                                         colorR=askForInt("What is R? ",0,255)
  344.                                         colorG=askForInt("What is G? ",0,255)
  345.                                         colorB=askForInt("What is B? ",0,255)
  346.                                         colorArray [i]=Color(colorG,colorR,colorB)
  347.                                 start_new_thread(end,())
  348.                                 while not(halt):
  349.                                         strobe(strip,colorAmount, colorArray,speed)
  350.                                
  351.                         elif strobeMode==3:
  352.                                 start_new_thread(end,())
  353.                                 while not(halt):
  354.                                         colorArray= [Color(127,127,127)]
  355.                                         strobe(strip,1,colorArray,speed)
  356.                         elif strobeMode==4:
  357.                                 start_new_thread(end,())
  358.                                 while not(halt):
  359.                                         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)]
  360.                                         strobe(strip,7,colorArray,speed)
  361.  
  362.                                        
  363. ######################################## Shooting Star #######################3
  364.                        
  365.                 if mode==8:
  366.                         shootingMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
  367.                         pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
  368.                         speed=pickSpeed(pickedSpeed)
  369.                         if shootingMode==1:
  370.                                 colorR=askForInt("What is R? ",0,255)
  371.                                 colorG=askForInt("What is G? ",0,255)
  372.                                 colorB=askForInt("What is B? ",0,255)
  373.                                 colorArray= [Color(colorG,colorR,colorB)]
  374.                                 start_new_thread(end,())
  375.                                 while not(halt):
  376.                                         start_new_thread(shootingStar,(strip, 1, colorArray, speed))
  377.                                         time.sleep(1)
  378.                         if shootingMode==2:
  379.                                 colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
  380.                                 colorArray = [Color(0,0,0)]*colorAmount
  381.                                 for i in range(0,colorAmount):
  382.                                         print "Enter RGB code for color" ,i
  383.                                         colorR=askForInt("What is R? ",0,255)
  384.                                         colorG=askForInt("What is G? ",0,255)
  385.                                         colorB=askForInt("What is B? ",0,255)
  386.                                         colorArray [i]=Color(colorG,colorR,colorB)
  387.                                 start_new_thread(end,())
  388.                                 while not(halt):
  389.                                         start_new_thread(shootingStar,(strip, colorAmount, colorArray, speed))
  390.                                         time.sleep(1)
  391.  
  392.                         elif shootingMode==3:
  393.                                 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)]
  394.                                 start_new_thread(end,())
  395.                                 while not(halt):
  396.                                         start_new_thread(shootingStar,(strip, 7, colorArray, speed))
  397.                                         time.sleep(1)
  398.                                 while running:
  399.                                         time.sleep(.5)
  400. ######################################### Music ##################################
  401.                 if mode==9:
  402.                         mode=askForInt("(1)Even Strip (2)single Lights (3)Chunks ",1,3)
  403.                         if mode==1:
  404.                                 start_new_thread(end,())
  405.                                 while not(halt):
  406.                                         if GPIO.input(f[0]):    
  407.                                                 start_new_thread(musicEqual,(strip,0,25,255,0,0))
  408.                                         if GPIO.input(f[1]):
  409.                                                 start_new_thread(musicEqual,(strip,26,50,255,50,0))
  410.                                         if GPIO.input(f[2]):
  411.                                                 start_new_thread(musicEqual,(strip,51,75,255,130,0))
  412.                                         if GPIO.input(f[3]):
  413.                                                 start_new_thread(musicEqual,(strip,76,100,0,255,0))
  414.                                         if GPIO.input(f[4]):
  415.                                                 start_new_thread(musicEqual,(strip,101,125,0,0,255))
  416.                                         if GPIO.input(f[5]):
  417.                                                 start_new_thread(musicEqual,(strip,126,150,255,0,255))
  418.                         if mode==2:
  419.                                 start_new_thread(end,())
  420.                                 while not(halt):
  421.                                         if GPIO.input(f[0]):    
  422.                                                 start_new_thread(musicSingle,(strip,0,255,0,0))
  423.                                         if GPIO.input(f[1]):
  424.                                                 start_new_thread(musicSingle,(strip,1,255,50,0))
  425.                                         if GPIO.input(f[2]):
  426.                                                 start_new_thread(musicSingle,(strip,2,255,130,0))
  427.                                         if GPIO.input(f[3]):
  428.                                                 start_new_thread(musicSingle,(strip,3,0,255,0))
  429.                                         if GPIO.input(f[4]):
  430.                                                 start_new_thread(musicSingle,(strip,4,0,0,255))
  431.                                         if GPIO.input(f[5]):
  432.                                                 start_new_thread(musicSingle,(strip,5,255,0,255))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement