Pinpong实例servo

塞尔沃汽车

Python代码演示如何控制servo电机Arduino主控板连接Windows或Linux计算机,Servo电机连接主控板D4插头

#-*编码:UTF-8-*-#实验效果:Servo控件#连接:连接Arduino主控板Windows或Linux计算机并连接servo到D4导入时间If no port number is entered, automatic recognition will be performed #Board("uno","COM36").begin()  # Initialization by specifying the port under Windows #Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux #Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac  s1 = Servo(Pin(Pin.D4)) # Initialize the servo pin by passing Pin into Servo  while True:     #s1.angle(0) # Control the servo to turn to the 0 degree position Method 1     s1.write_angle(0)  # Control the servo to turn to the 0 degree position Method 2     print("0")     time.sleep(1)      #s1.angle(90) # Control the servo to turn to the 90 degree position     s1.write_angle(90)  # Control the servo to turn to the 90 degree position Method 2     print("90")     time.sleep(1)      #s1.angle(180) # Control the servo to turn to the 180 degree position     s1.write_angle(180)  # Control the servo to turn to the 180 degree position Method 2     print("180")     time.sleep(1)      #s1.angle(90) # Control the servo to turn to the 90 degree position     s1.write_angle(90)  # Control the servo to turn to the 90 degree position Method 2     print("90")     time.sleep(1)