OreganoHauch

LED strip with 2 motion sensors

Jun 27th, 2022 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import time
  2. from rpi_ws281x import PixelStrip, Color
  3. import argparse
  4. import RPi.GPIO as GPIO
  5. import board
  6. import neopixel
  7. import colorsys
  8.  
  9. # LED strip configuration:
  10. LED_COUNT = 150 # Number of LED pixels.
  11. LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
  12. # LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
  13. LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
  14. LED_DMA = 10 # DMA channel to use for generating signal (try 10)
  15. LED_BRIGHTNESS = 100 # Set to 0 for darkest and 255 for brightest
  16. LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
  17. LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
  18.  
  19. # Create NeoPixel object with appropriate configuration.
  20. strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  21. # Intialize the library (must be called once before other functions).
  22. strip.begin()
  23.  
  24. SENSOR_PIN_1 = 23
  25. SENSOR_PIN_2 = 21
  26.  
  27. GPIO.setmode(GPIO.BCM)
  28. GPIO.setup(SENSOR_PIN_1, GPIO.IN)
  29. GPIO.setup(SENSOR_PIN_2, GPIO.IN)
  30.  
  31. def my_callback1():
  32. wait_ms = 50
  33. color = Color(255, 0, 0)
  34. for i in range(strip.numPixels()):
  35. strip.setPixelColor(i, color)
  36. strip.show()
  37. time.sleep(wait_ms / 1000.0)
  38.  
  39. def my_callback2():
  40. wait_ms = 50
  41. color = Color(0, 255, 0)
  42. for i in range(strip.numPixels()):
  43. strip.setPixelColor(i, color)
  44. strip.show()
  45. time.sleep(wait_ms / 1000.0)
  46.  
  47. try:
  48. GPIO.add_event_detect(SENSOR_PIN_1, GPIO.RISING, callback=my_callback1)
  49. GPIO.add_event_detect(SENSOR_PIN_2, GPIO.RISING, callback=my_callback2)
  50. while True:
  51. print(f"SENSOR 1: {GPIO.input(SENSOR_PIN_1)}")
  52. print(f"SENSOR 2: {GPIO.input(SENSOR_PIN_2)}")
  53. time.sleep(2)
  54.  
  55. except KeyboardInterrupt:
  56. print("BEENDE...")
  57. print("3...")
  58. time.sleep(1)
  59. print("2...")
  60. time.sleep(1)
  61. print("1...")
  62. time.sleep(1)
  63. print("ENDE")
  64.  
Add Comment
Please, Sign In to add comment