changing servo from sg90 to mg996r coding doesnt work?

hi, im a mechatronic students doing project on a diy rc car. im using previously arduino uno, hbridge, 1 dc motor, and 1 sg90 servo and a joystick. the coding work perfectly fine with function such as forward reverse dc motor on y axis and tilt function servo on x axis.

now i had change my servo to a high torque servo called the mg996r tower pro servo. and it doesnt work now. the servo start spinning endlessly instead of the tilt function. is using other servo causing this malfunction or does a mg996r servo require a different sset of coding. do enlightened me please. thank you very much for your time and effort.

this is my coding:

#include <Servo.h>

#define H_BRIDGE_FWD 5
#define H_BRIDGE_REV 6
#define SERVO_PIN 9 
#define SERVO_POT_PIN A0 // Y AXIS 
#define MOTOR_POT_PIN A1 // X AXIS

#define JOYSTICK_LOW_THD 491 
#define JOYSTICK_HIGH_THD 531

#define JOYSTICK_MIDDLE 511

#define SERVO_MIN 0
#define SERVO_MIDDLE 90
#define SERVO_MAX 180


Servo myservo;

int SERVO_ANALOG, MOTOR_ANALOG;

void setup() 
{
pinMode(H_BRIDGE_FWD, OUTPUT);
pinMode(H_BRIDGE_REV, OUTPUT);
myservo.attach(SERVO_PIN); 
}

void loop() 
{
readAxis();
if (MOTOR_ANALOG <= JOYSTICK_LOW_THD)
{
int revPWM = map(MOTOR_ANALOG, 0, JOYSTICK_LOW_THD, 255, 0);
reverse(revPWM);
}
if (MOTOR_ANALOG >= JOYSTICK_HIGH_THD)
{
int fwdPWM = map(MOTOR_ANALOG, JOYSTICK_HIGH_THD, 1023, 0, 255);
forward(fwdPWM);
}
if (MOTOR_ANALOG > JOYSTICK_LOW_THD && MOTOR_ANALOG < JOYSTICK_HIGH_THD)
{
stopMotor();
}
if (SERVO_ANALOG <= JOYSTICK_LOW_THD)
{
int angleLeft = map(SERVO_ANALOG, 0, JOYSTICK_LOW_THD, SERVO_MIN, SERVO_MIDDLE);
myservo.write(angleLeft);
}
if (SERVO_ANALOG >= JOYSTICK_HIGH_THD)
{
int angleRight = map(SERVO_ANALOG, JOYSTICK_HIGH_THD, 1023, SERVO_MIDDLE, SERVO_MAX);
myservo.write(angleRight);
}
if (SERVO_ANALOG > JOYSTICK_LOW_THD && SERVO_ANALOG < JOYSTICK_HIGH_THD)

{
myservo.write(SERVO_MIDDLE);
}
delay(10);
}

void readAxis() 
{
MOTOR_ANALOG = analogRead(MOTOR_POT_PIN);
SERVO_ANALOG = analogRead(SERVO_POT_PIN); 
}

void forward(int PWM) 
{
analogWrite(H_BRIDGE_FWD, PWM);
digitalWrite(H_BRIDGE_REV, 0); 
}

void reverse(int PWM) 
{
digitalWrite(H_BRIDGE_FWD, 0);
analogWrite(H_BRIDGE_REV, PWM); 
}

void stopMotor() 
{
digitalWrite(H_BRIDGE_FWD, 0);
digitalWrite(H_BRIDGE_REV, 0); 
}

thanks for the advance help.

//updates

i have found the reason for the failed functionability. is because my new mg996r servo is 360 continous rotation servo. therefore, need to change the coding. any help is highly appreciated. thanks

No picture is attached.

If "spinning endlessly" means it rotates through more than 360 degrees then a normal MG996R simply cannot do that. So either you have a continuous rotation version rather than a standard servo or you have a faulty servo.

Generally the same code will drive a standard SG90 and a standard MG996R though it is possible the angles may need to be adjusted slightly. The MG996R also needs a lot more current so swapping them over can cause problems if whatever battery/power supply you're powering the car with is overloaded.

Steve

Please edit your post to add code tags. See the “How to use this forum” post.

Post a wiring diagram (hand drawn, not Fritzing).

slipstick:
No picture is attached.

If “spinning endlessly” means it rotates through more than 360 degrees then a normal MG996R simply cannot do that. So either you have a continuous rotation version rather than a standard servo or you have a faulty servo.

Generally the same code will drive a standard SG90 and a standard MG996R though it is possible the angles may need to be adjusted slightly. The MG996R also needs a lot more current so swapping them over can cause problems if whatever battery/power supply you’re powering the car with is overloaded.

Steve

hi steve.thanks for your insight. i failed to upload the picture due to the size is too large and when i resize it became too blurry. so i decided not to put one. sorry for that. i believe my problem now is becaus epreviously im using a 180 deg servo. now im using 360 servo so the coding will not work and will rotate endlessly. i have no idea how to do the coding for a 360 one. if u can help i be delighted, or else i think i might just bought a new servo. thanks again

jremington:
Please edit your post to add code tags. See the "How to use this forum" post.

Post a wiring diagram (hand drawn, not Fritzing).

alright. done. thanks for the guidance.

Post a wiring diagram (hand drawn, not Fritzing).

Which will show, of course, that you are not trying to power the servo from the 5V Arduino pin.

Paindrayden96:
i believe my problem now is becaus epreviously im using a 180 deg servo. now im using 360 servo so the coding will not work and will rotate endlessly. i have no idea how to do the coding for a 360 one. if u can help i be delighted, or else i think i might just bought a new servo. thanks again

A continuous rotation servo (that's a better name than 360 degree) cannot be sent to a specific position. The writes that position a normal servo just change its direction and speed of rotation.

For positional control you'll need a different servo. A standard MG996R would do it but there many other possibilities. Just make sure it does NOT say 360 degree or continuous rotation.

Steve