Advertisement
phoenix332

LED With MultiThreading

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