0
votes

I am using Arduino UNO and I have 5 MG995 servo motors. But when i test the machine using 1 servo motor, it does not work. My connections are not wrong. Ports are okay but i don't know why it doesn't work.

My code for one servo motor.

    #include<Servo.h> // include server library
    Servo ser; // create servo object to control a servo
    int poser = 0; // initial position of server
    int val; // initial value of input

    void setup() {
        Serial.begin(9600); // Serial comm begin at 9600bps
        ser.attach(9);// server is connected at pin 9
    }

    void loop() {
        if (Serial.available()) { // if serial value is available
            val = Serial.read();// then read the serial value
            if (val == 'd') { //if value input is equals to d
                 poser += 1; //than position of servo motor increases by 1 ( anticlockwise)
                 ser.write(poser);// the servo will move according to position
                 delay(15);//delay for the servo to get to the position
            }

            if (val == 'a') { //if value input is equals to a
                 poser -= 1; //than position of servo motor decreases by 1 (clockwise)
                 ser.write(poser);// the servo will move according to position
                 delay(15);//delay for the servo to get to the position
            }
         }
     }
1
The variable val should be a char rather than int. - svtag
Same sir. It doesn't listen to keyboard - Çağrı
How are you powering the motor? Is it with 5v from arduino or 5v from an external power supply. If it is the first, I strongly recommend that you use an external power supply. Because as stated in the specifications of the motor No load operating current draw =170mA, which is more than what arduino can provide. - svtag
I am powering by via usb. But it must work by using usb with keyboard. - Çağrı

1 Answers

0
votes

There isn't anything very obvious wrong with the code, as long as you press d more than a so poser stays in range.

  • to test your hardware, run one of the examples such as sweep.
  • you should put in limits for poser so it stays between 0 and 180
  • try also lighting a led when 'a' or 'd' is received so you know that is working