also i don't understand how you're code work...
I see that you're replace the int (i = 0; i=50; i++) by an fonction: motorMoving
But i don't know how you managed to replace it..
Did you put and constant at the beginning? Do i have to put a newfonction on the void loop?