2

I am trying to control a continuous servo (MG996R) with an RF remote, but it's not working; the servo just spins in one direction.

I am new to Arduino so I don't really know if the code is correct.

This is my code:

#include <Servo.h>

Servo servothumb;         
Servo servoindex;         
Servo servomiddle;
Servo servoring;
Servo servopinky;

void setup() {
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  Serial.begin(9600); // setting baud rate
  Serial.println(" Data 4 Ch Remote Control"); 
  servothumb.attach(9); 
  servoindex.attach(10); 
  servopinky.attach(11);
  servoring.attach(12);
  servomiddle.attach(13);
}

void loop() {
  if (digitalRead(2) == HIGH) { // Button B pressed
    Serial.println("B");
    servothumb.write(90); 
    servoindex.write(90); 
    servopinky.write(90);
    servoring.write(90);
    servomiddle.write(90);
  } else {
    servothumb.write(90); 
    servoindex.write(90); 
    servopinky.write(90);
    servoring.write(90);
    servomiddle.write(90);
  }

  if (digitalRead(3) == HIGH) { // Button D pressed
    Serial.println("D");
  }
  if (digitalRead(4) == HIGH) { // Button A pressed
    Serial.println("A");
    servothumb.write(0); 
    servoindex.write(0); 
    servopinky.write(0);
    servoring.write(0);
    servomiddle.write(0);
  } else {
    servothumb.write(90); 
    servoindex.write(90); 
    servopinky.write(90);
    servoring.write(90);
    servomiddle.write(90);
  }
  if (digitalRead(5)== HIGH) { // Button C pressed
    Serial.println("C");
    servothumb.write(180); 
    servoindex.write(180); 
    servopinky.write(180);
    servoring.write(180);
    servomiddle.write(180);
  } else {
    servothumb.write(90); 
    servoindex.write(90); 
    servopinky.write(90);
    servoring.write(90);
    servomiddle.write(90);
  }
}
8
  • the servo doesn't stop or go the other way if i use the rf remote it just spins clockwise
    – James Lim
    Jan 31, 2021 at 10:00
  • is it the servo or the RF remote? ... you really need to do some debugging by using the simplest code to test only one thing
    – jsotola
    Jan 31, 2021 at 10:05
  • Your code does not control a servo, but 5 of them. In the loop, the if statements do not exclude each other. That means either the if or the else branch is executed for each if block. That means you switch the speed values for the servos very fast, again and again, if one or more buttons are pressed. If no button is pressed, then only the three else branches are executed and your servos stay at value 90. How this value is interpreted depends on the servo; some stop, some need another value to stop, some do not stop at all. BTW: The mg996r is not a continous servo, did you modify it? Jan 31, 2021 at 10:09
  • how do you add a stop statement for a continuous servo? i didn't modify it I bought one online it was already continuous
    – James Lim
    Jan 31, 2021 at 10:18
  • If you bought a modified version, ask the vendor, how to stop it. This is not specified for the original. I do not know how it is modified. How could I? ;-) Modification is normally achieved with just removing the mechanical connection of the motor and the potentiometer that senses the position of the motor. It depends on the resistance of the potentionmeter that's now fix. You could try to write a program that iterates over all values from 0 to 180, sends this value to the servo and also prints it to the Serial monitor. Then, with luck, you'll see the motor stop, and the monitored value. Jan 31, 2021 at 13:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.