OreganoHauch

MotionSensorLED

Jun 28th, 2022 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.76 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. from numpy import mean, arange
  9. import random
  10. from datetime import datetime
  11. import os
  12.  
  13. # LED strip configuration:
  14. LED_COUNT = 150 # Number of LED pixels.
  15. LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
  16. # LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
  17. LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
  18. LED_DMA = 10 # DMA channel to use for generating signal (try 10)
  19. LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
  20. LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
  21. LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
  22.  
  23.  
  24. # Create NeoPixel object with appropriate configuration.
  25. strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
  26. # Intialize the library (must be called once before other functions).
  27. strip.begin()
  28.  
  29. SENSOR_PIN_1 = 23
  30. SENSOR_PIN_2 = 24
  31.  
  32. GPIO.setmode(GPIO.BCM)
  33. GPIO.setup(SENSOR_PIN_1, GPIO.IN)
  34. GPIO.setup(SENSOR_PIN_2, GPIO.IN)
  35.  
  36. def rainbow(hue):
  37. (r,g,b) = colorsys.hsv_to_rgb(hue, 1.0, 1.0)
  38. R, G, B = int(255 * r), int(255 * g), int(255 * b)
  39. return (R,G,B)
  40.  
  41. def my_callback1(arg):
  42. print("MOTION1!!!")
  43. wait_ms = 50
  44. color = Color(255, 0, 0)
  45. for i in range(strip.numPixels()):
  46. strip.setPixelColor(i, color)
  47. strip.show()
  48. time.sleep(wait_ms / 1000.0)
  49.  
  50. def my_callback2(arg):
  51. print("MOTION2!!!")
  52. wait_ms = 50
  53. color = Color(0, 255, 0)
  54. for i in range(strip.numPixels()):
  55. strip.setPixelColor(i, color)
  56. strip.show()
  57. time.sleep(wait_ms / 1000.0)
  58.  
  59. def RGB_LinearTransition(FromColor,ToColor,FromIndex,ToIndex,i):
  60. if FromIndex >= ToIndex:
  61. raise ValueError("FromIndex must be smaller than ToIndex.")
  62. if i < FromIndex or i > ToIndex:
  63. raise ValueError(f"The index i={i} must lie between FromIndex={FromIndex} and ToIndex={ToIndex}.")
  64. if (type(FromColor), type(ToColor),
  65. len(FromColor), len(ToColor)) != (tuple, tuple, 3, 3):
  66. raise TypeError("FromColor and ToColor need to be tuples with (each) 3 entries.")
  67.  
  68. R1 = FromColor[0]
  69. G1 = FromColor[1]
  70. B1 = FromColor[2]
  71.  
  72. R2 = ToColor[0]
  73. G2 = ToColor[1]
  74. B2 = ToColor[2]
  75.  
  76. length = ToIndex - FromIndex
  77. distance = i - ToIndex
  78. progress_percentage = -distance/length
  79.  
  80. R = round(R2 - (R2-R1)*progress_percentage)
  81. G = round(G2 - (G2-G1)*progress_percentage)
  82. B = round(B2 - (B2-B1)*progress_percentage)
  83.  
  84. return R, G, B
  85.  
  86. def FADE_OUT(t, color_int, FADE_ms=2000):
  87. if t > FADE_ms:
  88. raise ValueError("t must not be larger than FADE_ms.")
  89. pct = (FADE_ms-t)/FADE_ms # progress percentage
  90. color = round(color_int*pct)
  91. return color
  92.  
  93. SensorDict = {
  94. "Sensor 1 Event RISING": False,
  95. "Sensor 2 Event RISING": False,
  96. "Sensor 1 Event FALLING": False,
  97. "Sensor 2 Event FALLING": False,
  98. "Sensor 1 Event Counter": 0,
  99. "Sensor 2 Event Counter": 0,
  100. "Sensor 1 Time": 0,
  101. "Sensor 2 Time": 0,
  102. 0: 0,
  103. 1: 0,
  104. 2: 0,
  105. 3: 0,
  106. 4: 0,
  107. "Loop": 0
  108. }
  109.  
  110. def SarahsAnimation():
  111. if SensorDict["Sensor 1 Event RISING"]:
  112.  
  113. purple = (128,49,167)
  114. petrol = (0,98,105)
  115. yellow = (255,253,0)
  116.  
  117. width = 5
  118. trans = 2
  119.  
  120. time_list = [SensorDict[0],
  121. SensorDict[1],
  122. SensorDict[2],
  123. SensorDict[3],
  124. SensorDict[4]]
  125. avg_time = mean(time_list)
  126. wait_ms = avg_time/strip.numPixels()*1000
  127.  
  128. forw_range = strip.numPixels()-(3*width+2*trans)+1
  129. for forw in range(-(3*width+2*trans),forw_range):
  130.  
  131. for i in range(forw,forw+width):
  132. strip.setPixelColor(i, Color(255,253,0)) # yellow
  133. for i in range(forw+width,forw+width+trans):
  134. R, G, B = RGB_LinearTransition(yellow,petrol,forw+width,forw+width+trans-1,i)
  135. strip.setPixelColor(i, Color(R,G,B))
  136. for i in range(forw+width+trans,forw+2*width+trans):
  137. strip.setPixelColor(i, Color(0,98,105)) # petrol
  138. for i in range(forw+2*width+trans,forw+2*width+2*trans):
  139. R, G, B = RGB_LinearTransition(petrol,purple,forw+2*width+trans,forw+2*width+2*trans-1,i)
  140. strip.setPixelColor(i, Color(R,G,B))
  141. for i in range(forw+2*width+2*trans,forw+3*width+2*trans):
  142. strip.setPixelColor(i, Color(128,49,167)) # purple
  143. for i in range(forw):
  144. strip.setPixelColor(i, Color(0,0,0)) # dark
  145.  
  146. strip.show()
  147. time.sleep(wait_ms / 1000.0)
  148.  
  149. # FADE OUT
  150. FADE_ms = 2000
  151. smoothness = 10
  152. for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
  153. for i in range(forw_range-1,forw_range+3*width+2*trans):
  154. R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
  155. R = FADE_OUT(t, R, FADE_ms=FADE_ms)
  156. G = FADE_OUT(t, G, FADE_ms=FADE_ms)
  157. B = FADE_OUT(t, B, FADE_ms=FADE_ms)
  158. strip.setPixelColor(i, Color(R,G,B))
  159. strip.show()
  160. time.sleep(FADE_ms/1000.0/smoothness)
  161.  
  162. '''while True:
  163. FADE_ms = 2000
  164. smoothness = 10
  165. width=5
  166. trans=2
  167. forw_range = strip.numPixels()-(3*width+2*trans)+1
  168. for i in range(forw_range+2*width+2*trans,forw_range+3*width+2*trans):
  169. strip.setPixelColor(i, Color(128,49,167)) # purple
  170. for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
  171. for i in range(forw_range-1,forw_range+3*width+2*trans):
  172. R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
  173. R = FADE_OUT(t, R, FADE_ms=FADE_ms)
  174. G = FADE_OUT(t, G, FADE_ms=FADE_ms)
  175. B = FADE_OUT(t, B, FADE_ms=FADE_ms)
  176. strip.setPixelColor(i, Color(R,G,B))
  177. strip.show()
  178. time.sleep(FADE_ms/1000.0/smoothness)'''
  179.  
  180. '''for t in range(2,51):
  181. for w in range(2,51):
  182. SensorDict["Sensor 1 Event RISING"] = True
  183. SensorDict[0] = 5
  184. SensorDict[1] = 4
  185. SensorDict[2] = 2
  186. SensorDict[3] = 4
  187. SensorDict[4] = 1
  188. SarahsAnimation(width=w, trans=t)'''
  189.  
  190. def SarahsAnimationBackwards():
  191. if SensorDict["Sensor 2 Event RISING"]:
  192.  
  193. purple = (128,49,167)
  194. petrol = (0,98,105)
  195. yellow = (255,253,0)
  196.  
  197. width = 5
  198. trans = 2
  199.  
  200. time_list = [SensorDict[0],
  201. SensorDict[1],
  202. SensorDict[2],
  203. SensorDict[3],
  204. SensorDict[4]]
  205. avg_time = mean(time_list)
  206. wait_ms = avg_time/strip.numPixels()*1000
  207.  
  208. for backw in range(-(3*width+2*trans), strip.numPixels()-(3*width+2*trans)+1):
  209. backw = strip.numPixels() - (3*width+2*trans)+1 - backw # reverse
  210.  
  211. for i in range(backw,backw+width):
  212. strip.setPixelColor(i, Color(255,253,0)) # yellow
  213. for i in range(backw+width,backw+width+trans):
  214. R, G, B = RGB_LinearTransition(yellow,petrol,backw+width,backw+width+trans-1,i)
  215. strip.setPixelColor(i, Color(R,G,B))
  216. for i in range(backw+width+trans,backw+2*width+trans):
  217. strip.setPixelColor(i, Color(0,98,105)) # petrol
  218. for i in range(backw+2*width+trans,backw+2*width+2*trans):
  219. R, G, B = RGB_LinearTransition(petrol,purple,backw+2*width+trans,backw+2*width+2*trans-1,i)
  220. strip.setPixelColor(i, Color(R,G,B))
  221. for i in range(backw+2*width+2*trans,backw+3*width+2*trans):
  222. strip.setPixelColor(i, Color(128,49,167)) # purple
  223. for i in range(backw+3*width+trans, strip.numPixels()+1):
  224. strip.setPixelColor(i, Color(0,0,0)) # dark
  225.  
  226. strip.show()
  227. time.sleep(wait_ms / 1000.0)
  228.  
  229. # FADE OUT
  230. FADE_ms = 2000
  231. smoothness = 10
  232. for t in arange(FADE_ms/smoothness, FADE_ms+FADE_ms/smoothness, FADE_ms/smoothness):
  233. for i in range(3*width+2*trans):
  234. R, G, B = random.randint(0,255), random.randint(0,255), random.randint(0,255)
  235. R = FADE_OUT(t, R, FADE_ms=FADE_ms)
  236. G = FADE_OUT(t, G, FADE_ms=FADE_ms)
  237. B = FADE_OUT(t, B, FADE_ms=FADE_ms)
  238. strip.setPixelColor(i, Color(R,G,B))
  239. strip.show()
  240. time.sleep(FADE_ms/1000.0/smoothness)
  241.  
  242. def Sensor1EventRISING(arg):
  243. dt_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
  244. print(f"Sensor 1 RISING ({dt_string})")
  245. with open("SensorLog.txt", "a") as f:
  246. os.chdir("/home/pi/Documents")
  247. f.write(f"\nSensor 1: {dt_string}")
  248. SensorDict["Sensor 1 Event RISING"] = True
  249. SensorDict["Sensor 1 Event FALLING"] = False
  250. SensorDict["Sensor 1 Event Counter"] += 1
  251.  
  252. # save Sensor 1 time
  253. Time = time.time()
  254. SensorDict["Sensor 1 Time"] = Time
  255.  
  256. time.sleep(1)
  257. SensorDict["Sensor 1 Event RISING"] = False
  258.  
  259. def Sensor2EventRISING(arg):
  260. dt_string = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
  261. print(f"Sensor 2 RISING ({dt_string})")
  262. with open("SensorLog.txt", "a") as f:
  263. os.chdir("/home/pi/Documents")
  264. f.write(f"\nSensor 2: {dt_string}")
  265. SensorDict["Sensor 2 Event RISING"] = True
  266. SensorDict["Sensor 2 Event FALLING"] = False
  267. SensorDict["Sensor 2 Event Counter"] += 1
  268.  
  269. # measure time difference
  270. Time = time.time()
  271. Sensor1Time = SensorDict["Sensor 1 Time"]
  272. if SensorDict["Sensor 1 Time"] != 0 and Time-Sensor1Time < 30:
  273. Loop = SensorDict["Loop"]
  274. SensorDict[Loop%5] = round(Time-Sensor1Time, 2)
  275. SensorDict["Loop"] += 1
  276. 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")
  277.  
  278. time.sleep(1)
  279. SensorDict["Sensor 2 Event RISING"] = False
  280.  
  281.  
  282. def Sensor1EventFALLING(arg):
  283. SensorDict["Sensor 1 Event FALLING"] = True
  284. SensorDict["Sensor 1 Event RISING"] = False
  285.  
  286. print("Sensor 1 FALLING")
  287.  
  288. def Sensor2EventFALLING(arg):
  289. SensorDict["Sensor 2 Event FALLING"] = True
  290. SensorDict["Sensor 2 Event RISING"] = False
  291.  
  292. print("Sensor 2 FALLING")
  293.  
  294. def KeyboardInterruptAnimation():
  295. pixels.fill((255,255,255))
  296. time.sleep(1)
  297. pixels.fill((0,0,0))
  298. time.sleep(1)
  299. pixels.fill((255,255,255))
  300. time.sleep(1)
  301. pixels.fill((0,0,0))
  302. time.sleep(1)
  303. pixels.fill((255,255,255))
  304. time.sleep(1)
  305. pixels.fill((0,0,0))
  306.  
  307. try:
  308. GPIO.add_event_detect(SENSOR_PIN_1, GPIO.RISING, callback=Sensor1EventRISING)
  309. #GPIO.add_event_detect(SENSOR_PIN_1, GPIO.FALLING, callback=Sensor1EventFALLING)
  310. GPIO.add_event_detect(SENSOR_PIN_2, GPIO.RISING, callback=Sensor2EventRISING)
  311. #GPIO.add_event_detect(SENSOR_PIN_2, GPIO.FALLING, callback=Sensor2EventFALLING)
  312.  
  313. while True:
  314. SarahsAnimation()
  315. time.sleep(0.2)
  316. SarahsAnimationBackwards()
  317. time.sleep(0.2)
  318.  
  319. except KeyboardInterrupt:
  320. print("BEENDE...")
  321. print("3...")
  322. time.sleep(0.5)
  323. print("2...")
  324. time.sleep(0.5)
  325. print("1...")
  326. time.sleep(0.5)
  327. print("ENDE")
  328. pixels = neopixel.NeoPixel(board.D18, 150)
  329. pixels.fill((0,0,0))
  330.  
Add Comment
Please, Sign In to add comment