How do I "dynamically" name my servo?
Posted: Mon Jul 11, 2022 12:53 am
Sorry guys. Another question:
Thanks to OlivierLenoir who answered my question earlier about randomisation of a string...
I'm trying to control my servos (10 of them) using the result of the randomisation. I'm sure it's something simple, but I don't know the correct term for this to find the answer.
So I kind of name my servos A to J:
Then I move my servo by using duty_u16
Then I assigned the order number A to J (1 to 10):
Then I run the code as per Olivier:
And of course it didn't like it. I was trying to substitute the letter into the object "servo" but it's of course invalid. Also tried "" and {} but doesn't work. The closest I've got is:
but it says AttributeError: 'str' object has no attribute 'duty_16'
So how do I rename the "Object" for PWM with the above? Couldn't find the answer with Google... I've probably misunderstood something.
Thanks in advance!
Adrian
Thanks to OlivierLenoir who answered my question earlier about randomisation of a string...
I'm trying to control my servos (10 of them) using the result of the randomisation. I'm sure it's something simple, but I don't know the correct term for this to find the answer.
So I kind of name my servos A to J:
Code: Select all
servoA = PWM(Pin(0))
servoA.freq(50)
servoB = PWM(Pin(1))
Code: Select all
servoA.duty_u16(4500)
Code: Select all
targets = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
Code: Select all
count_target = range(len(targets))
for t in count_target:
target = choice(targets)
servo(target).duty_u16(1350)
targets.remove(target)
utime.sleep(1)
print(f'{target}')
Code: Select all
target.duty_16(1350)
So how do I rename the "Object" for PWM with the above? Couldn't find the answer with Google... I've probably misunderstood something.
Thanks in advance!
Adrian