Advertisement
SigmaBoy456

MicroBit Smart Home Sonar Dist Checking #245

Jan 29th, 2025 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. #config Variable
  2. ArrPinLight = [
  3.     DigitalPin.P1,
  4.     DigitalPin.P2
  5. ]
  6. delay = 1
  7. forloopdelay = 1
  8. MaxDist = 10
  9. Sound = soundExpression.hello
  10. music.set_volume(255)
  11. #function set
  12. delay = delay * 100
  13. forloopdelay = forloopdelay * 100
  14. def TurnLight(signal : int):
  15.     global forloopdelay, ArrPinLight
  16.     for i in ArrPinLight:
  17.         pins.digital_write_pin(i, signal)
  18.         basic.pause(forloopdelay)
  19.  
  20. def on_forever():
  21.     global MaxDist, delay, Sound
  22.     dist = sonar.ping(DigitalPin.P14, DigitalPin.P15, PingUnit.CENTIMETERS)
  23.     if dist > 0 and dist <= MaxDist:
  24.         TurnLight(0)
  25.         music.play(music.builtin_playable_sound_effect(Sound), music.PlaybackMode.UNTIL_DONE)
  26.     else:
  27.         TurnLight(1)
  28.     basic.pause(delay)
  29. basic.forever(on_forever)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement