Heres the sketch loaded currently. I think its just refreshing the servos every time thru the loop and causes them to twitch every now and then.
/* attempting to program UNO with ethernet shield to accept commands from a client.
copied code almost exactly from mouse_test
want to a power transistor to the external voltage regulator to power them up only when a client is connected. this stops the jitter of servos while idle.
PWM pins remaining after ethernetshield are 3, 5, 6, and 9.
*/
#include <Servo.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac address of eth shield
IPAddress ip (192,168,1,177); //permanent local IP address
IPAddress gateway (192,168,1,1); //router address
IPAddress subnet (255,255,255,0);
EthernetServer server(80); //using port 80
Servo servoX, servoY, servoA, servoB; //global variables:
int x1, y1, a1, b1;
//const int servoPower = 2;
const int laserPin = 8;
const int buzzerPin = 7;
bool laser = false;
boolean alreadyConnected = false;
void setup() {
servoX.attach(3);
servoY.attach(5);
servoA.attach(6);
servoB.attach(6);
servoX.write(90);
servoY.write(90);
pinMode(laserPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
//pinMode(servoPower, OUTPUT);
digitalWrite(laserPin, LOW);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.begin(19200);
Serial.print("Web server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
a1 = servoA.read(); //gets actual position of servoA/B to send back to processing.
b1 = servoB.read();
EthernetClient client = server.available();
if (client) {
if (!alreadyConnected){
client.flush();
Serial.println("Disconnected.");
alreadyConnected = true;
}
while (!client.connected()){
//digitalWrite(servoPower, LOW); //turns off servos and laser while no client is connected.
laser = false;
digitalWrite(laserPin, LOW);
}
while (client.connected()){
//digitalWrite(servoPower, HIGH); //powers on servos.
delay(1);
static int v = 0;
if (client.available()>0){
char ch = client.read();
switch(ch) { //read incomming characters from processing and handle them case by case.
case '0'...'9': //0-9 are coordinates.
v = v *10 + ch - '0';
break;
case 'x': //signifies end of x coordinate.
x1 = map(v, 0, 180, 160, 0);
servoX.write(x1);
v = 0;
break;
case 'y': //end of y coordinate.
y1 = map(v, 0, 180, 10, 170);
servoY.write(y1);
v = 0;
break;
case 'l': //turns laser on or off.
laserOnOff();
if (laser){
client.write("1"); //tells processing laser is on.
}
else if (!laser){
client.write("0"); //tells processing laser is off.
}
break;
case 'u': //moves servoA up and sends data back to processing to draw a position bar on screen if in range.
a1 += 2;
if (a1 >= 150){
a1 = 150;
}
servoA.write(a1);
if (a1 < 150){ //if servoA is in range, tell processing to move position bar.
client.write("2");
}
break;
case 'd':
a1 -= 2;
if (a1 <= 20){
a1 = 20;
}
servoA.write(a1);
if (a1 > 20){
client.write("3");
}
break;
case 'a':
b1 -= 2;
if (b1 <= 20){
b1 = 20;
}
servoB.write(b1); //same for servoB:
if (b1 > 20){
client.write("4");
}
break;
case 's':
b1 += 2;
if (b1 >= 160){
b1 = 160;
}
servoB.write(b1);
if (b1 < 160){
client.write("5");
}
break;
case 'b': //fires buzzer while mouse button is held.
digitalWrite(buzzerPin, HIGH);
break;
case 'n':
digitalWrite (buzzerPin, LOW);
break;
}
}
} client.flush();
}
}
void laserOnOff(){ //turns laser on/off.
if (laser == false){
digitalWrite (laserPin, HIGH);
laser = true;
}
else if (laser == true){
digitalWrite(laserPin, LOW);
laser = false;
}
}