Sorry for the delay in replying.
If you haven’t already, rename main.py to main_old.py. That way it won’t auto run every time you do a reset etc. It’s what I do when testing my own code.
Does your code run OK when run locally from your PC?
And does it run OK if you manage to save it to the Motor 2040.
I don’t own a motor but I do own an Inventor 2040. I can likely edit and test your code on that, if you don’t mind posting it?
Use the preformatted text button, the </> , and it will retain the indents etc.