Can't stop the motor.

When my motor attached, and then if I detach it, it doesn’t work. I want to stop it from spinning. I’m new to Arduino and not good at English. Thanks.
My code:
if (cm > 30) {
servo_5.detach();
}
if (cm <= 30) {
servo_5.attach(5);
servo_5.write(0);
}

I'm sure you're doing something wrong. Just no clue why, but that's mostly due to you failing to give any clues.

Now please go and read the sticky and following the instructions try again.

I mean it automatically spins when it attaches. But when I detach it (want to stop it), it doesn't work.

Reading instructions is hard, isn't it?

First thing first, I'm not good at English. So I may not understand some instructions.

The easy clue is detach isn't work for me.

Telling something isn't working is not telling anything at all! Tell us something useful!

Did you already open the link I gave you in #2? Incomplete code (and no code tags). No info on the servo in question. No explanation what "it doesn't work" means exactly (what happens, and how is it different from what you want to happen?). No schematic. Probably totally wrong use of the detach() function (which you normally don't need to begin with). Did you actually read the manual pages of the Servo library and the code samples that come with it?

Punzaza_: The easy clue is detach isn't work for me.

detach() does not do what you seem to think it will. It just stops sending a signal to the servo. If you have a continuous rotation (or 360 degree) servo getting no signal will not stop it turning. You have to send the correct signal.

Please tell us EXACTLY what servo/motor you are using and post your COMPLETE code.

Steve

Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR http://forum.arduino.cc/index.php/topic,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)