简体   繁体   中英

Delays between servo movements in pyfirmata don't work

I'm writing a program in python where moving servos at specific times is a crucial part of the project. To achive this, I am using the pyfirmata library.

I've tried two methods of delays, but none of them seem to work. When I run the code, the servo turns the first time, but after the delay, it doesn't turn and the program just stops, instead of moving the servo to 0 degrees and then stopping.

This is the one with the board.pass_time() built in to the pyfirmata library:

from pyfirmata import Arduino, util
import time
board = Arduino('COM3')
servo = board.get_pin('d:9:s')

servo.write(180)        #This works and turns the servo
time.sleep(1)
servo.write(0)          #This time the servo does not turn, then the program ends

And here's the one with time.sleep():

from pyfirmata import Arduino, util
board = Arduino('COM3')
servo = board.get_pin('d:9:s')

servo.write(180)        #This works and turns the servo
board.pass_time(1)
servo.write(0)          #This time the servo does not turn, then the program ends

I would highly appreciate if someone could help.

Thank you, David

I found the answer! There might be a better answer, but if I write board.get_pin('d:9:s') as board.get_pin('d:9:') , like this, I need to control it with a value from 0 to 5 instead of 0 to 360, but the delays work

Your code is correct but you need to make the delay again to turn it back to 0 degree. So your code should look something like this.

servo.write(180)
time.sleep(2)
servo.write(0)
time.sleep(2)

You need to give the delay everytime you make changes to the servo motor in order to make those changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM