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