In this lesson we demonstrate how to create a custom library on the Raspberry Pi Pico W to control a servo. We create a servo class, and then show how to use it as a library in future programs.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== class servo: def __init__(self,sPin): import machine self.servoPin=sPin self.obj=machine.PWM(machine.Pin(self.servoPin)) self.obj.freq(50) def pos(self,angle): writeVal=6553/180*angle+1638 self.obj.duty_u16(int(writeVal)) |
