Arduino UNO only works with usb cable

Well, clearly, there is nothing wrong with the code and there is no point in looking at it, but you can bet there is everything wrong with the battery, which is almost certainly a PP3. You are sending a boy on a man's job and you need a mans's battery. So put the PP3 back in the smoke detector you stole it from and try 6xAAs or perhaps 2x 18650s. or maybe your robot has some decent power available.