MG955 servo joystick control

Hi guys this is my first post so I apologies if their are any errors in my code layout. I am currently in yr 12 and undertaking a project to create a robotic arm which will be controlled by mapping servos on the position of two joysticks. I have taken the design from this tutorial

However, in this tutorial an app was developed in order to control the arm where I am looking to use two joysticks instead. I have had a problem as I am now realizing the code that I was originally intending to use does not work with the MG955 servos. (I tested the code using SG90 servos assuming i could just swap them for the MG995 servos and be done).

The code i am using is this

#include <Servo.h>

Servo servo1;
Servo servo;
Servo servo2;
Servo servo3;
Servo servo4;

const int SW_pin = 4;
int joyX = 0;
int joyY = 1;
int joyX2 = 2;
int joyY2 = 3;
int joyVal;
int joyVal2;
int buttonPushCounter = 0;  // counter for the number for button presses
int buttonState = 0;  // current state of the button
int lastButtonState = 0;  // previous state of the button

void setup() {
  // put your setup code here, to run once:
  servo1.attach(3);
  servo.attach(5);
  servo2.attach(6);
  servo3.attach(9);
  servo4.attach(10);
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  joyVal = analogRead(joyX);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo.write(joyVal);

  joyVal = analogRead(joyY);
  joyVal = map(joyVal, 0, 1023, 0, 180);
  servo1.write(joyVal);

  joyVal2 = analogRead(joyX2);
  joyVal2 = map(joyVal2, 0, 1023, 0, 180);
  servo2.write(joyVal2);

  joyVal2 = analogRead(joyY2);
  joyVal2 = map(joyVal2, 0, 1023, 0, 180);
  servo3.write(joyVal2);
  delay(15);

  // put your main code here, to run repeatedly:

  buttonState = digitalRead(SW_pin); // read the pushbutton input pin

  if (buttonState != lastButtonState) { // compare the buttonState to its previous state

    if (buttonState == HIGH) { //if the current state is HIGH then the button went from off to on: it has been pushed

      buttonPushCounter++;  // increment the counter if the button has been pushed

      Serial.println("ON"); // will print "ON" in the serial monitor when the button is pushed

      Serial.print("number of button pushes:  "); //will print the actual number of times the button has been pushed

      Serial.println(buttonPushCounter);

    } else {

      Serial.println("OFF");  // if the current state of the button is LOW then the button went from on to off

    }

    delay(50);  // allow a debounce, time for the button push to be recognised

  }

  lastButtonState = buttonState;  // save the current state as the last state, for the nest time through the loop

  if (buttonPushCounter == 3) { // on the third push of the button the code will loop back to the beginning and start again

    buttonPushCounter = 0;  // it will start with no count of button pushes

  }

  if (buttonPushCounter == 1) {

    servo4.write(90);
  } else

    if (buttonPushCounter == 2) {

      servo4.write(0);
    }
}

Ideally i would like to be able to control the arm using a joystick however, if that is impossible alternative ideas would be appreciated.

Cheers

apologies if their are any errors in my code layout.

Yes, there are, so please fix your post. Instructions in the forum guide sticky post at the top of the forum section, which you could/should have read before posting.

Never heard of an MG955 servo. Perhaps you mean MG995?

Anyway I would guess that the problem is not with the code but that you do not have sufficient power for those large servos. So how are they connected and what exactly is powering them?

Steve

PaulRB: Yes, there are, so please fix your post. Instructions in the forum guide sticky post at the top of the forum section, which you could/should have read before posting.

Sorry have hopefully edited it correctly.

slipstick: Never heard of an MG955 servo. Perhaps you mean MG995?

Anyway I would guess that the problem is not with the code but that you do not have sufficient power for those large servos. So how are they connected and what exactly is powering them?

Steve

Yea sorry i do mean the MG995 servo.

When i was testing the code i was mainly going off the Arduino's inbuilt 5v. I was a little scared to use an external power source as i burnt out one of the MG995 servos already when accidentally setting the voltage too high.

joyVal = map(joyVal, 0, 1023, 0, 180);

This may be faster, but the clever compiler may already know that.

joyVal = analogRead(joyX) * 181UL >> 10;

The Arduino 5V pin is not suitable to power ANY servo and certainly not those high-power MG995s. An external 5V or 6V supply is what you need. And since the servos in an arm are permanently under load and so don't only need power when they're moving you'll want at least 5A to be safe. For contrast the Arduino 5V pin can safely supply no more than 0.4A.

Steve