Turning a DC motor a set number of Rotations using an encoder
You never tell the motor to stop.

void loop(){
   while(counter&lt; 360*5){
     analogWrite(motor,255);
   }
   analogWrite(motor, 0); //add something like this or whatever could stop it
   //you may want to disable the counter now too
}


However, even with that change you are likely a long way from having a working, or at least reliable system - see other issues in my comment on the question.