Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # NeoPixel library strandtest example
- # Author: Tony DiCola (tony@tonydicola.com)
- #
- # Direct port of the Arduino NeoPixel library strandtest example. Showcases
- # various animations on a strip of NeoPixels.
- import time
- import random
- from thread import start_new_thread
- from array import array
- from neopixel import *
- # LED strip configuration:
- LED_COUNT = 150 # Number of LED pixels.
- LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
- LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
- LED_DMA = 5 # DMA channel to use for generating signal (try 5)
- LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
- LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
- # Define functions which animate LEDs in various ways.
- def fullStrip(strip,numColors,colorArray,ifMoving):
- for i in range(0-1-ifMoving-numColors,strip.numPixels(),numColors):
- for j in range(0,numColors):
- color=colorArray[j]
- i= i+1
- strip.setPixelColor(i,color)
- strip.show()
- def buildUp(strip, numColors,colorArray):
- h = 0;
- for i in range(0,strip.numPixels()):
- for k in range(0,numColors,1):
- for j in range(0,(strip.numPixels()-h)):
- strip.setPixelColor(j,colorArray[k])
- strip.setPixelColor((j-1),0)
- strip.show()
- h += 1
- def shootingStar(strip, numColors,colorArray, speed):
- k = random.randint(0,numColors-1)
- for i in range(0,(strip.numPixels()+1)):
- global running
- running = True
- strip.setPixelColor(i,colorArray[k])
- strip.setPixelColor((i-1),0)
- strip.show()
- time.sleep(speed)
- running = False
- def lightning(strip, numColors, colorArray, speed):
- i = random.randint(0,(strip.numPixels()/3))
- k = random.randint(0,numColors-1)
- finalColor=colorArray[k]
- for j in range(0,3):
- strip.setPixelColor(i*3+j, finalColor)
- strip.show()
- time.sleep(speed)
- for j in range(0,3):
- strip.setPixelColor(i*3+j, 0)
- def every4(strip, color, speed):
- for j in range (0,4):
- for i in range(j,strip.numPixels(),4):
- strip.setPixelColor(i-1,0)
- strip.setPixelColor(i,color)
- strip.setPixelColor(i+1,0)
- strip.setPixelColor(i+2,0)
- strip.setPixelColor(i+3,0)
- strip.show()
- time.sleep(speed)
- def colorWipe(strip, numColors, colorArray, speed):
- for j in range(0,numColors):
- for i in range(strip.numPixels()):
- strip.setPixelColor(i, colorArray[j])
- strip.show()
- time.sleep(speed)
- def strobe(strip, numColors, colorArray, speed):
- for j in range(0,numColors):
- for i in range(0,strip.numPixels()):
- strip.setPixelColor(i,colorArray[j])
- strip.show()
- time.sleep(speed)
- for i in range(0,strip.numPixels()):
- strip.setPixelColor(i,0)
- strip.show()
- time.sleep(speed)
- def askForInt(question,low,high):
- entered = int(input (question))
- while (entered>high or entered<low):
- print"Invalid Response. Please enter a number between ",low," and ",high
- entered = int(input (question))
- return entered
- def pickSpeed(pick):
- if pick==1:
- return .15
- elif pick==2:
- return .1
- elif pick==3:
- return .075
- elif pick==4:
- return .05
- elif pick==5:
- return .025
- def clean(strip):
- for i in range(0,strip.numPixels()):
- strip.setPixelColor(i,0)
- strip.show()
- def end():
- s=askForInt("Enter 1 to return to main menu. ",1,1)
- global halt
- halt = True
- print("Please wait for program to finish" )
- # Main program logic follows:
- while True:
- # Create NeoPixel object with appropriate configuration.
- strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
- # Intialize the library (must be called once before other functions).
- strip.begin()
- while True:
- 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)
- halt=False
- clean(strip)
- ################################ Stationary Strip #################################
- if mode==1:
- presets=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
- if presets == 1:
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray=[Color(colorG,colorR,colorB)]
- fullStrip(strip,1,colorArray,0)
- elif presets==2:
- colorAmount= askForInt("How many colors? ",2,strip.numPixels())
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- fullStrip(strip,colorAmount,colorArray,0)
- elif presets==3:
- colorAmount=7
- 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)]
- fullStrip(strip,colorAmount,colorArray,0)
- ################################# Moving Strip #######################################
- if mode==2:
- presets=askForInt("(1)Custom (2)Rainbow ",1,2)
- if presets==1:
- colorAmount= askForInt("How many colors? ",2,strip.numPixels())
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- start_new_thread(end,())
- while not(halt):
- for g in range(0,colorAmount):
- fullStrip(strip,colorAmount,colorArray,g)
- time.sleep(speed)
- if presets==2:
- colorAmount=7
- 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)]
- pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- start_new_thread(end,())
- while not(halt):
- for g in range(0,colorAmount):
- fullStrip(strip,colorAmount,colorArray,g)
- time.sleep(speed)
- ################################# Build Up #######################################
- if mode==3:
- buildUpMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
- if buildUpMode==1:
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray= [Color(colorG,colorR,colorB)]
- start_new_thread(end,())
- while not(halt):
- buildUp(strip, 1, colorArray)
- if buildUpMode==2:
- colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- buildUp(strip,colorAmount, colorArray)
- elif buildUpMode==3:
- start_new_thread(end,())
- while not(halt):
- 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)]
- buildUp(strip, 7, colorArray)
- ################################# Lightning White ##################################
- if mode==4:
- lightningMode=askForInt("(1)Single Color (2)Custom (3)White (4)Rainbow ",1,4)
- pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- if lightningMode==1:
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray=[Color(colorG,colorR,colorB)]
- start_new_thread(end,())
- while not(halt):
- lightning(strip, 1, colorArray, speed)
- elif lightningMode==2:
- colorAmount= askForInt("How many colors? ",2,(strip.numPixels()/3))
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- lightning(strip,colorAmount, colorArray, speed)
- elif lightningMode==3:
- start_new_thread(end,())
- while not(halt):
- colorArray= [Color(127,127,127)]
- lightning(strip,1,colorArray, speed)
- elif lightningMode==4:
- start_new_thread(end,())
- while not(halt):
- 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)]
- lightning(strip,7,colorArray, speed)
- ############################### Color Wipe ################################
- if mode==5:
- buildUpMode=askForInt("(1)Custom (2)Rainbow ",1,2)
- pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- if buildUpMode==1:
- colorAmount = askForInt("How many colors? ",2,(strip.numPixels()))
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- colorWipe(strip,colorAmount, colorArray, speed)
- elif buildUpMode==2:
- 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)]
- start_new_thread(end,())
- while not(halt):
- colorWipe(strip, 7, colorArray, speed)
- ################################ Every Four #########################
- if mode==6:
- pickedSpeed=askForInt("1-5 (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- color=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- every4(strip,color,speed)
- ############################### Strobe ###############################3
- if mode==7:
- strobeMode=askForInt("(1)One Color (2)Custom (3)White (4)Rainbow ",1,4)
- strobeSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(strobeSpeed)
- if strobeMode==1:
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray=[Color(colorG,colorR,colorB)]
- start_new_thread(end,())
- while not(halt):
- strobe(strip, 1, colorArray,speed)
- elif strobeMode==2:
- colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- strobe(strip,colorAmount, colorArray,speed)
- elif strobeMode==3:
- start_new_thread(end,())
- while not(halt):
- colorArray= [Color(127,127,127)]
- strobe(strip,1,colorArray,speed)
- elif strobeMode==4:
- start_new_thread(end,())
- while not(halt):
- 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)]
- strobe(strip,7,colorArray,speed)
- ######################################## Shooting Star #######################3
- if mode==8:
- shootingMode=askForInt("(1)Single Color (2)Custom (3)Rainbow ",1,3)
- pickedSpeed=askForInt("Select a speed of 1-5. (1 being slowest) ",1,5)
- speed=pickSpeed(pickedSpeed)
- if shootingMode==1:
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray= [Color(colorG,colorR,colorB)]
- start_new_thread(end,())
- while not(halt):
- start_new_thread(shootingStar,(strip, 1, colorArray, speed))
- time.sleep(1)
- if shootingMode==2:
- colorAmount= askForInt("How many colors? ",2,(strip.numPixels()))
- colorArray = [Color(0,0,0)]*colorAmount
- for i in range(0,colorAmount):
- print "Enter RGB code for color" ,i
- colorR=askForInt("What is R? ",0,255)
- colorG=askForInt("What is G? ",0,255)
- colorB=askForInt("What is B? ",0,255)
- colorArray [i]=Color(colorG,colorR,colorB)
- start_new_thread(end,())
- while not(halt):
- start_new_thread(shootingStar,(strip, colorAmount, colorArray, speed))
- time.sleep(1)
- elif shootingMode==3:
- 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)]
- start_new_thread(end,())
- while not(halt):
- start_new_thread(shootingStar,(strip, 7, colorArray, speed))
- time.sleep(1)
- while running:
- time.sleep(.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement