0

I may be approaching this wrong but my by goal is to create a python program to position a servo smoothly by giving the initial servo angle and the final servo angle. I thought I would use a function that stepped the servo smoothly based on calculated increments. Below is the simple code;

def move(servoIni, servoFin):
 MS_IN_SECOND = 1000
 zero = servoIni + 1
 max_amplitude = servoFin - zero
 amplitude = min(max_amplitude, 100)
 i = 0
 sleep_duration = .01
 freq = 240
 step = 2*pi*freq/(MS_IN_SECOND/sleep_duration)
 while True:
                 val = zero + amplitude*sin(i)
                 if int(val) == servoFin:
                         break
                 return int(val)
                 i += step
                 sleep(sleep_duration)

 val = move(servoIni = 80, servoFin = 110)
 print('Servo Angle = ', val)

When I run the code it prints the first step, in this case "81" and then ends. What I would like is it to print each step until it reaches "servoFin"; in the example val = 110. What am I missing? Any suggestions would be greatly appreciated. Thanks Brian Mc.

  • 1
    You need to move your print into the while block, otherwise print is called only once – Simon Apr 10 '21 at 22:44

1 Answers1

1

return inside the while loop breaks it mid first iteration

Vulwsztyn
  • 2,140
  • 1
  • 12
  • 20