Controlling a servo motor with two arduinos? I have two Arduinos, two buttons and one servo motor.

I want to control a servo motor with two Arduino's when I pressed the buttons. I linked Arduino PWM pins (first Arduino PWM pin is 9, and second one is 3) to the servo motor signal input.

When I pressed button 1 servo motor moving a few, or pressed button 2. but the signal isn't clear, and servo motor doesn't move correctly that I assigned degree.

What can I do for the solution?

My first Arduino code is here:


#include&lt;Servo.h&gt;

#define Buton 8

Servo myServo;

void setup() {
  pinMode(Buton, INPUT);
  myServo.attach(3);
}

void loop()
{
  if (digitalRead(Buton) == 1)
  { 
    myServo.write(90);
  }
  else
  {
    myServo.write(180);
  }

}


and second arduino code is: 

#include&lt;Servo.h&gt;

#define Buton 8

Servo myServo;

void setup() {
  pinMode(Buton, INPUT);
  myServo.attach(9);
}

void loop()
{
  if (digitalRead(Buton) == 1)
  { 
    myServo.write(90);
  }
  else
  {
    myServo.write(180);
  }

}
You can't just connect random pins together like that. Connecting two output pins together is a recipe for disaster. Best case scenario: nothing works. Worst case scenario: you break one or both Arduinos.

Only one Arduino can control a servo. If you want a second Arduino to make something happen to the servo you have to make it tell the Arduino that is controlling the servo that something should happen.

That could be a simple output of the second Arduino connected to an input of the first Arduino, in which case it kind of acts like a button. Or you could create some kind of communication channel between them - maybe using a Serial connection, or a Wire (I2C) connection. There's many ways, and which is best depends on how much control one Arduino needs to exert over the other.