Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from rpi_ws281x import PixelStrip, Color
- import argparse
- import RPi.GPIO as GPIO
- import board
- import neopixel
- import colorsys
- from numpy import mean, arange
- import random
- from datetime import datetime
- import os
- # LED strip configuration:
- LED_COUNT = 150 # Number of LED pixels.
- LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
- # LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
- LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
- LED_DMA = 10 # DMA channel to use for generating signal (try 10)
- 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)
- LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
- # Create NeoPixel object with appropriate configuration.
- strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
- # Intialize the library (must be called once before other functions).
- strip.begin()
- SENSOR_PIN_1 = 23
- SENSOR_PIN_2 = 24
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(SENSOR_PIN_1, GPIO.IN)
- GPIO.setup(SENSOR_PIN_2, GPIO.IN)
- def rainbow(hue):
- (r,g,b) = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
- R, G, B = int(255 * r), int(255 * g), int(255 * b)
- return (R,G,B)
- def my_callback1(arg):
- print("MOTION1!!!")
- wait_ms = 50
- color = Color(255, 0, 0)
- for i in range(strip.numPixels()):
- strip.setPixelColor(i, color)
- strip.show()
- time.sleep(wait_ms / 1000.0)
- def my_callback2(arg):
- print("MOTION2!!!")
- wait_ms = 50
- color = Color(0, 255, 0)
- for i in range(strip.numPixels()):
- strip.setPixelColor(i, color)
- strip.show()
- time.sleep(wait_ms / 1000.0)
- def RGB_LinearTransition(FromColor,ToColor,FromIndex,ToIndex,i):
- if FromIndex >= ToIndex:
- raise ValueError("FromIndex must be smaller than ToIndex.")
- if i < FromIndex or i > ToIndex:
- raise ValueError(f"The index i={i} must lie between FromIndex={FromIndex} and ToIndex={ToIndex}.")
- if (type(FromColor), type(ToColor),
- len(FromColor), len(ToColor)) != (tuple, tuple, 3, 3):
- raise TypeError("FromColor and ToColor need to be tuples with (each) 3 entries.")
- R1 = FromColor[0]
- G1 = FromColor[1]
- B1 = FromColor[2]
- R2 = ToColor[0]
- G2 = ToColor[1]
- B2 = ToColor[2]
- length = ToIndex - FromIndex
- distance = i - ToIndex
- progress_percentage = -distance/length
- R = round(R2 - (R2-R1)*progress_percentage)
- G = round(G2 - (G2-G1)*progress_percentage)
- B = round(B2 - (B2-B1)*progress_percentage)
- return R, G, B
- def FADE_OUT(t, color_int, FADE_ms=2000):
- if t > FADE_ms:
- raise ValueError("t must not be larger than FADE_ms.")
- pct = (FADE_ms-t)/FADE_ms # progress percentage
- color = round(color_int*pct)
- return color
- SensorDict = {
- "Sensor 1 Event RISING": False,
- "Sensor 2 Event RISING": False,
- "Sensor 1 Event FALLING": False,
- "Sensor 2 Event FALLING": False,
- "Sensor 1 Event Counter": 0,
- "Sensor 2 Event Counter": 0,
- "Sensor 1 Time": 0,
- "Sensor 2 Time": 0,
- 0: 0,
- 1: 0,
- 2: 0,
- 3: 0,
- 4: 0,
- "Loop": 0
- }
- def SarahsAnimation():
- if SensorDict["Sensor 1 Event RISING"]:
- purple = (128,49,167)
- petrol = (0,98,105)
- yellow = (255,253,0)
- width = 5
- trans = 2
- time_list = [SensorDict[0],
- SensorDict[1],
- SensorDict[2],
- SensorDict[3],
- SensorDict[4]]
- avg_time = mean(time_list)
- wait_ms = avg_time/strip.numPixels()*1000
- forw_range = strip.numPixels()-(3*width+2*trans)+1
- for forw in range(-(3*width+2*trans),forw_range):
- for i in range(forw,forw+width):
- strip.setPixelColor(i, Color(255,253,0)) # yellow
- for i in range(forw+width,forw+width+trans):
- R, G, B = RGB_LinearTransition(yellow,petrol,forw+width,forw+width+trans-1,i)
- strip.setPixelColor(i, Color(R,G,B))
- for i in range(forw+width+trans,forw+2*width+trans):
- strip.setPixelColor(i, Color(0,98,105)) # petrol
- for i in range(forw+2*width+trans,forw+2*width+2*trans):
- R, G, B = RGB_LinearTransition(petrol,purple,forw+2*width+trans,forw+2*width+2*trans-1,i)
- strip.setPixelColor(i, Color(R,G,B))
- for i in range(forw+2*width+2*trans,forw+3*width+2*trans):
- strip.setPixelColor(i, Color(128,49,167)) # purple
- for i in range(forw):
- strip.setPixelColor(i, Color(0,0,0)) # dark
- strip.show()
- time.sleep(wait_ms / 1000.0)
- # FADE OUT
- FADE_ms = 2000
- smoothness = 10
- for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
- for i in range(forw_range-1,forw_range+3*width+2*trans):
- R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
- R = FADE_OUT(t, R, FADE_ms=FADE_ms)
- G = FADE_OUT(t, G, FADE_ms=FADE_ms)
- B = FADE_OUT(t, B, FADE_ms=FADE_ms)
- strip.setPixelColor(i, Color(R,G,B))
- strip.show()
- time.sleep(FADE_ms/1000.0/smoothness)
- '''while True:
- FADE_ms = 2000
- smoothness = 10
- width=5
- trans=2
- forw_range = strip.numPixels()-(3*width+2*trans)+1
- for i in range(forw_range+2*width+2*trans,forw_range+3*width+2*trans):
- strip.setPixelColor(i, Color(128,49,167)) # purple
- for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
- for i in range(forw_range-1,forw_range+3*width+2*trans):
- R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
- R = FADE_OUT(t, R, FADE_ms=FADE_ms)
- G = FADE_OUT(t, G, FADE_ms=FADE_ms)
- B = FADE_OUT(t, B, FADE_ms=FADE_ms)
- strip.setPixelColor(i, Color(R,G,B))
- strip.show()
- time.sleep(FADE_ms/1000.0/smoothness)'''
- '''for t in range(2,51):
- for w in range(2,51):
- SensorDict["Sensor 1 Event RISING"] = True
- SensorDict[0] = 5
- SensorDict[1] = 4
- SensorDict[2] = 2
- SensorDict[3] = 4
- SensorDict[4] = 1
- SarahsAnimation(width=w, trans=t)'''
- def SarahsAnimationBackwards():
- if SensorDict["Sensor 2 Event RISING"]:
- purple = (128,49,167)
- petrol = (0,98,105)
- yellow = (255,253,0)
- width = 5
- trans = 2
- time_list = [SensorDict[0],
- SensorDict[1],
- SensorDict[2],
- SensorDict[3],
- SensorDict[4]]
- avg_time = mean(time_list)
- wait_ms = avg_time/strip.numPixels()*1000
- for backw in range(-(3*width+2*trans), strip.numPixels()-(3*width+2*trans)+1):
- backw = strip.numPixels() - (3*width+2*trans)+1 - backw # reverse
- for i in range(backw,backw+width):
- strip.setPixelColor(i, Color(255,253,0)) # yellow
- for i in range(backw+width,backw+width+trans):
- R, G, B = RGB_LinearTransition(yellow,petrol,backw+width,backw+width+trans-1,i)
- strip.setPixelColor(i, Color(R,G,B))
- for i in range(backw+width+trans,backw+2*width+trans):
- strip.setPixelColor(i, Color(0,98,105)) # petrol
- for i in range(backw+2*width+trans,backw+2*width+2*trans):
- R, G, B = RGB_LinearTransition(petrol,purple,backw+2*width+trans,backw+2*width+2*trans-1,i)
- strip.setPixelColor(i, Color(R,G,B))
- for i in range(backw+2*width+2*trans,backw+3*width+2*trans):
- strip.setPixelColor(i, Color(128,49,167)) # purple
- for i in range(backw+3*width+trans, strip.numPixels()+1):
- strip.setPixelColor(i, Color(0,0,0)) # dark
- strip.show()
- time.sleep(wait_ms / 1000.0)
- # FADE OUT
- FADE_ms = 2000
- smoothness = 10
- for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
- for i in range(3*width+2*trans):
- R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
- R = FADE_OUT(t, R, FADE_ms=FADE_ms)
- G = FADE_OUT(t, G, FADE_ms=FADE_ms)
- B = FADE_OUT(t, B, FADE_ms=FADE_ms)
- strip.setPixelColor(i, Color(R,G,B))
- strip.show()
- time.sleep(FADE_ms/1000.0/smoothness)
- def Sensor1EventRISING(arg):
- dt_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
- print(f"Sensor 1 RISING ({dt_string})")
- with open("SensorLog.txt", "a") as f:
- os.chdir("/home/pi/Documents")
- f.write(f"\nSensor 1: {dt_string}")
- SensorDict["Sensor 1 Event RISING"] = True
- SensorDict["Sensor 1 Event FALLING"] = False
- SensorDict["Sensor 1 Event Counter"] += 1
- # save Sensor 1 time
- Time = time.time()
- SensorDict["Sensor 1 Time"] = Time
- time.sleep(1)
- SensorDict["Sensor 1 Event RISING"] = False
- def Sensor2EventRISING(arg):
- dt_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
- print(f"Sensor 2 RISING ({dt_string})")
- with open("SensorLog.txt", "a") as f:
- os.chdir("/home/pi/Documents")
- f.write(f"\nSensor 2: {dt_string}")
- SensorDict["Sensor 2 Event RISING"] = True
- SensorDict["Sensor 2 Event FALLING"] = False
- SensorDict["Sensor 2 Event Counter"] += 1
- # measure time difference
- Time = time.time()
- Sensor1Time = SensorDict["Sensor 1 Time"]
- if SensorDict["Sensor 1 Time"] != 0 and Time-Sensor1Time < 30:
- Loop = SensorDict["Loop"]
- SensorDict[Loop%5] = round(Time-Sensor1Time, 2)
- SensorDict["Loop"] += 1
- print(f"Time 1: {SensorDict[0]} s\nTime 2: {SensorDict[1]} s\nTime 3: {SensorDict[2]} s\nTime 4: {SensorDict[3]} s\nTime 5: {SensorDict[4]} s\n")
- time.sleep(1)
- SensorDict["Sensor 2 Event RISING"] = False
- def Sensor1EventFALLING(arg):
- SensorDict["Sensor 1 Event FALLING"] = True
- SensorDict["Sensor 1 Event RISING"] = False
- print("Sensor 1 FALLING")
- def Sensor2EventFALLING(arg):
- SensorDict["Sensor 2 Event FALLING"] = True
- SensorDict["Sensor 2 Event RISING"] = False
- print("Sensor 2 FALLING")
- def KeyboardInterruptAnimation():
- pixels.fill((255,255,255))
- time.sleep(1)
- pixels.fill((0,0,0))
- time.sleep(1)
- pixels.fill((255,255,255))
- time.sleep(1)
- pixels.fill((0,0,0))
- time.sleep(1)
- pixels.fill((255,255,255))
- time.sleep(1)
- pixels.fill((0,0,0))
- try:
- GPIO.add_event_detect(SENSOR_PIN_1, GPIO.RISING, callback=Sensor1EventRISING)
- #GPIO.add_event_detect(SENSOR_PIN_1, GPIO.FALLING, callback=Sensor1EventFALLING)
- GPIO.add_event_detect(SENSOR_PIN_2, GPIO.RISING, callback=Sensor2EventRISING)
- #GPIO.add_event_detect(SENSOR_PIN_2, GPIO.FALLING, callback=Sensor2EventFALLING)
- while True:
- SarahsAnimation()
- time.sleep(0.2)
- SarahsAnimationBackwards()
- time.sleep(0.2)
- except KeyboardInterrupt:
- print("BEENDE...")
- print("3...")
- time.sleep(0.5)
- print("2...")
- time.sleep(0.5)
- print("1...")
- time.sleep(0.5)
- print("ENDE")
- pixels = neopixel.NeoPixel(board.D18, 150)
- pixels.fill((0,0,0))
Add Comment
Please, Sign In to add comment