Beginner Micropython question - functions

Viewed 30

I am relatively new to python and I have a question. I have some code to move a servo back and forth but I would like to put that in a function so I could write once and call it when needed. How would I go about doing that please? I have looked up what to do but how do I know what the key words are that go in the function? Any guidance would be appreciated.

from machine import Pin, PWM

from utime import sleep

# declaring pwm object for servo motor pin i.e., GPIO_1

pwm = PWM(Pin(16))

# setting PWM frequency at 50Hz

pwm.freq(50)

while True:

    for position in range(1500, 4500, 50): # changing angular position

        pwm.duty_u16(position)

        sleep(0.1) # delay

    for position in range(4500, 1500, -50):

        pwm.duty_u16(position)

        sleep(0.1) # delay

I'm using Thonny and a Raspberry pi pico.

Cheers,

Ben

0 Answers
Related