Guest User

LEGO Technic 42121 motorized code

a guest
Apr 20th, 2021
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. from hub2hub import Remote, ble_handler
  2. from hub import port
  3. from time import sleep_ms
  4.  
  5. #Lib: https://hub2hub.readthedocs.io/en/latest/
  6. ble = ble_handler()
  7. Remote = Remote(ble)
  8.  
  9. led_direction = 0
  10. led_brightness = 0
  11. remode = 0
  12.  
  13. Remote.connect()
  14.  
  15. #A: motor_left
  16. #B: motor_right
  17. #C: arm1
  18. #D: Lights1
  19. #E: arm2
  20. #F: Lights2
  21.  
  22. port.F.pwm(100)
  23. Remote.led(1)
  24.  
  25. while True:
  26.     if remode == 0:
  27.         if Remote.right.plus.is_pressed():
  28.             port.A.pwm(70)
  29.         elif Remote.right.min.is_pressed():
  30.             port.A.pwm(-70)
  31.         else:
  32.             port.A.pwm(0)
  33.  
  34.         if Remote.left.plus.is_pressed():
  35.             port.B.pwm(70)
  36.         elif Remote.left.min.is_pressed():
  37.             port.B.pwm(-70)
  38.         else:
  39.             port.B.pwm(0)
  40.    
  41.     else:
  42.         if Remote.right.plus.is_pressed():
  43.             port.C.pwm(70)
  44.         elif Remote.right.min.is_pressed():
  45.             port.C.pwm(-70)
  46.         else:
  47.             port.C.pwm(0)
  48.  
  49.         if Remote.left.plus.is_pressed():
  50.             port.E.pwm(100)
  51.         elif Remote.left.min.is_pressed():
  52.             port.E.pwm(-100)
  53.         else:
  54.             port.E.pwm(0)
  55.  
  56.     if led_brightness >= 100:
  57.         led_direction = 1
  58.     if led_brightness <= 0:
  59.         led_direction = 0
  60.  
  61.     if led_direction == 0:
  62.         led_brightness = led_brightness+1
  63.     else:
  64.         led_brightness = led_brightness-1
  65.    
  66.     port.D.pwm(led_brightness)
  67.    
  68.     if Remote.right.red.is_pressed():
  69.         remode = 1
  70.         Remote.led(2)
  71.         sleep_ms(100)
  72.     if Remote.left.red.is_pressed():
  73.         remode = 0
  74.         Remote.led(1)
  75.         sleep_ms(100)
  76.     sleep_ms(10)
Advertisement
Add Comment
Please, Sign In to add comment