getting two servos to rotate exactly opposite of each other

I have an input coming from processing reading audio level and I need two servos to move exactly opposite of each other. They seem to work, but aren’t exactly in sync, they will be opposite phase like I like, and generally moving together but sometimes one is delayed not moving at same angle.

Ideally I would like them to move in a full 360, right now they seem to be moving only 180 and anything over seems to make it go crazy, so some insight on that would be great too. here is the code:

processing

import ddf.minim.*;
import processing.serial.*;
Serial comPort;

Minim minim;
AudioInput in;

int lf = 10;

void setup()
{
  int width = 150;
  int height = 500;
  size(width,height, P3D);
  minim = new Minim(this);
  in = minim.getLineIn();
  // uncomment this line to *hear* what is being monitored, in addition to seeing it
  //in.enableMonitoring();
  frameRate(10);
  comPort = new Serial(this, Serial.list()[4], 9600);
}

void draw()
{
  int maxLevel = 600;
  
  int currentLevel = int(in.left.level()*1000);
  int convertToDegrees = int(map(currentLevel, 0, maxLevel, 0, 360));
  smooth();
  background(0);
  stroke(166, 212, 255);
  fill(76, 169, 255);
  rect(0, height, height, -currentLevel);
  
  line( width/2, height/2, width/2+cos(convertToDegrees)*20, height/2+sin(convertToDegrees)*20);
  ellipse(width/2+cos(convertToDegrees)*20, height/2+sin(convertToDegrees)*20, 10, 10);
  
  
  arc(width/2, 55, 100, 100, PI, sin(radians(convertToDegrees)) );
  
  comPort.write(currentLevel);
  println(currentLevel);
}

arduino:

#include <Servo.h> 

int incomingByte = 0;   
Servo myservo;
Servo myservoTwo;
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(10);  // attaches the servo on pin 9 to the servo object 
  myservoTwo.attach(9); 
} 
 
void loop() 
{ 
  while (Serial.available() > 0) {
    int num=Serial.read()-'0';
    myservo.write(num);
    myservoTwo.write(-num);
 } 
}

Servos arent extremely precise and they are feedbackless, meaning they dont actually report their position back to arduino unless you have servos that have a feedback pin. It fairly easy to modify them to have feedback. Basically solder a wire to the potentiometer wiper and attach it to an analog input.

You can also buy them. This may help.

Assuming that the range of numbers going to the servo is 0-180 couldn't you:

myservo.write(num);
    myservoTwo.write(180-num);

As it is the code is writing a negative number to the servo.
Most hobby servos that I have used don't even get a full 180°.
If you really want to synchronize them I would suggest steppers.

groundfungus:
Assuming that the range of numbers going to the servo is 0-180 couldn't you:

myservo.write(num);

myservoTwo.write(180-num);




As it is the code is writing a negative number to the servo.
Most hobby servos that I have used don't even get a full 180°.
If you really want to synchronize them I would suggest steppers.

ahh, subtracting 180 from the num val make them sync, exactly what I needed. I am pretty sure this will go a full 380 since I can make them spin continuously in a circle.

ahh, subtracting 180 from the num val make them sync, exactly what I needed. I am pretty sure this will go a full 380 since I can make them spin continuously in a circle.

I cannot follow this description. What type of servos are you using? Full 360 usually implies continuous rotation. It is not possible to keep continuous rotation hobby servos in sync. Stepper motors are a better choice if precision is required. What is the application?