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);
}
}