Arduino strange behavior with dc motors

Add decoupling capacitors to the motor's supply, about 10uF or bigger. Also add 0.1uF ceramic capacitors directly across the motor windings as close to the motor as you can get.