Control angle of MG996R servo Cw and ccw

Hello,
I need a code where a DS18b20(digital thermal sensor) sends signal to arduino and arduino will send the value to servo .The servo will first rotate 60 degree to cw direction .then it will keep rotatating to 30 degree ccw and 30 degree cw until the temperature value drops below 50 degrees. Finally the motor will come back to its previous position to zero degrees. The program will start again if the temperature value rises more than 50 degree. the servo must come back to its previous position.
I'm controlling the angle with rotation delay. As ccw rotation point of my motor is 98.

we.ino (1.72 KB)

If you are not using a continuous rotation servo, the terms cw and ccw in your post are redundant. The servo figures out which direction to rotate to get to the proper new position from its current position. You don't need to tell it that.

If you are using a continuous rotation servo, you control speed and direction, not position, so the absolute values in your post can't be achieved.

What does the code actually do, and how does that differ from what you want?

Finally, do not attach code that can be included, between code tags, in your post.

There's a useful function called loop() which does looping for you. Yours is empty and for some odd reason you seem to be doing the looping by calling setup() over and over again. Why?

And you don't need to keep attaching and detaching the servo. It's pointless.

So does your program do what you want it to? If not what does it do?

Steve

if i use the code in loop(), the servo comes back to its original position but it keeps rotating ccw.i just need to stop the continuous rotation after temperature drop.the servo is continuous 360 degree

the servo is continuous 360 degree

Then you can only control its speed (which also controls its direction of rotation). You can NOT control its position. You must determine its position, and make it stop rotating (by setting the speed to 0), by some external means.

PaulS:
Then you can only control its speed (which also controls its direction of rotation). You can NOT control its position.

PaulS:
Then you can only control its speed (which also controls its direction of rotation). You can NOT control its position. You must determine its position, and make it stop rotating (by setting the speed to 0), by some external means.

whats the difference between position and direction of rotation

A bit like you saying that your position is in London England and your direction of travel is North. Or, you're in Cairo Egypt, heading west.

A servo has only a position, centred on 90 degrees nominally and it can move to say 0 or 180 frpm 90. The direction it needs to move to get to 0 or 180 from 90 is something it knows: just tell it to go to 180.

A continuous "servo" spins in either CW or ACW direction at some speed or other, but has no notion of position. If you spin it at some or other speed, and tell it to stop, it will stop wherever inertia and friction dictate.

The movement you describe, which seems to take place entirely over a 60 degree arc, is very simple for a standard servo. It is basically impossible for a continuous servo. So you need a different servo.

Steve

1 Like