So, I have this function coded in python that prints the servo positions (I attached it to the bottom). It works fine, but when I attempt to iterate this (i.e. a function that finds the positions, moves the servo, finds the new positions, moves the servo, etc.), the program will return incorrect values for every iteration beyond the first. Can someone tell me why this happens and possibly a solution to this problem? Thank you in advance.
[code]import serial
import time
import sys
def hi(ServoSerial):
comData=[0,0,0,0]
comData[0]=170
comData[1]=12
comData[2]=16
comData[3]=0
com1Data=[0,0,0,0]
com1Data[0]=170
com1Data[1]=12
com1Data[2]=16
com1Data[3]=1
com2Data=[0,0,0,0]
com2Data[0]=170
com2Data[1]=12
com2Data[2]=16
com2Data[3]=2
for i in range(1,3):
if i==1:
coms0data=bytearray(comData)
coms1data=bytearray(com1Data)
coms2data=bytearray(com2Data)
serv1=ServoSerial.write(coms0data)
serv2=ServoSerial.write(coms1data)
serv2=ServoSerial.read(size=2)
serv1=int.from_bytes(serv2,sys.byteorder)
else:
coms0data=bytearray(comData)
coms1data=bytearray(com1Data)
coms2data=bytearray(com2Data)
serv2=ServoSerial.write(coms1data)
serv2=ServoSerial.read(size=2)
serv2=int.from_bytes(serv2,sys.byteorder)
serv1=int(serv1/4)
serv2=int(serv2/4)
print(serv1)
print(serv2)[/code]