Processing controls servos over ethernet for about 6 seconds, arduino freezes

So, amazingly, Ive gotten these two sketches to work over ethernet, but arduino seems to freeze after about six seconds.
Im wondering if Im just flooding it with data and it cant handle it. When I restart the processing sketch it starts over just fine.
Thanks in advance for any help!
PS. Id also like to add a watchdog timer that shuts off the laser if no commands are recieved for likem, 30 seconds.
I played around with it before and it would work, but it would shut off the laser no matter what, after the specified time and wouldnt recieve any more commands.

Arduino sketch:

/* attempting to program UNO with ethernet shield to accept commands from a client. 
copied code almost exactly from mouse_test
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
boolean alreadyConnected = false;

Servo servoX, servoY, servoA, servoB;
const int laserPin = 8;
const int buzzerPin = 7;
bool laser = false;
int x1, y1, a1, b1;

void setup() {
  pinMode(laserPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(laserPin, LOW);
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.print("Web server address:");

void loop() {
  a1 =;
  b1 =;
  EthernetClient client = server.available();
  if (client) {
    if (!alreadyConnected){
      alreadyConnected = true;
    while (client.connected()){
      static int v = 0;
      if (client.available()>0){
        char ch =;
        switch(ch) {//read incomming characters from processing and handle the case by case
          case '0'...'9'://0-9 are coordinates
            v = v *10 + ch - '0';
          case 'x'://signifies end of x coordinate.
            x1 = map(v, 0, 180, 160, 0);
            v = 0;
          case 'y': //end of y coordinate
            y1 = map(v, 0, 180, 10, 170);
            v = 0;
          case 'l'://turns laser on or off
          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;
            if (a1 < 150){//if servoA is in range, tell processing to move position bar.
          case 'd':
            a1 -= 2;
            if (a1 <= 20){
              a1 = 20;
            if (a1 > 20){
          case 'a':
            b1 -= 2;
            if (b1 <= 20){
              b1 = 20;
            servoB.write(b1);//same for servoB
            if (b1 > 20){
          case 's':
            b1 += 2;
            if (b1 >= 160){
              b1 = 160;
            if (b1 < 160){
          case 'b'://fires buzzer while mouse button is held.
            digitalWrite(buzzerPin, HIGH);
          case 'n':
            digitalWrite (buzzerPin, LOW);
    } 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;

Processing sketch:
Any lines with “serial” in them have been commented out and replaced by client commands.

//import processing.serial.*;
Client myClient;
int xpos=90;
int ypos=90;
int a=320; // position of green servoA position bar on left side of screen.
int b=320; //position of red servoB position bar on bottom of screen.
boolean laser = false;
boolean ab=true; //switches control of A and B servos with mousewheel.
//Serial port;

void setup() {
  size(640, 640); //sets size of window
  //println(Serial.list()); //list com ports
  //port = new Serial(this, Serial.list()[0], 19200);
  myClient = new Client(this, "", 80);

void draw() {//draws window and graphics.
  if (mousePressed && (mouseButton == RIGHT)) {//sends buzzer on command.
   // port.write("n");//sends buzzer off command.
  if (keyPressed){//pressing 'a' activates control of servoA with mousewheel.
    if (key == 'a'){
      ab = true;
    else if (key == 'b'){
      ab = false;
  //if (port.available()>0){//check to see if any thing sent back from arduino on serial bus.
  if (myClient.available() > 0){  
    //int ls =;
    int ls =;
      case '1'://turn laser on and draw dot/line.
        laser = true;
      case '0': // turn laser off.
        laser = false;
      case '2'://move servoA position bar down.
        a = a + 7;
      case '3'://move servoA position bar up.
        a = a - 7;
      case '4'://move servoB position bar left.
        b = b - 7;
      case '5'://move servoB position bar right.
        b = b + 7;
  rect(0, 0, 640, 640);// grey rectangle.
  text("Click left mouse to toggle laser on and off. Buzzer stays on while right mouse is held.", 20, 30);
  text("Press 'a' to control Servo A with mouse wheel. Press 'b' for Servo B.", 20, 60);
  fill(150, 150, 150);
  ellipse(mouseX, mouseY, 40, 40); //outer target ring.
  if (laser) {//checks to see if arduino sent laser on command, if it did, draw dot and line to it.
    fill(255, 0, 0);
    ellipse(mouseX, mouseY, 10, 10);//red dot
    stroke(150, 100, 0);
    line(320, 640, mouseX, mouseY);//line to red dot
  fill(0, 255, 0); //draws green servoA position bar
  rect(0, a+10, 20, 10);
  fill(255, 255, 0);//draws servoB position bar.
  rect(b+10, 620, 10, 20);
  update(mouseX, mouseY);//sends mouse position to update() function
void update(int x, int y) {
  //calculate servo position from mouseX/Y:
  xpos = x/4;
  ypos = y/4;
  //output to servo (0-180):
  //port.write(xpos + "x");
  //port.write(ypos + "y");
  myClient.write(xpos + "x");
  myClient.write(ypos + "y");
void mouseClicked() {//sends laser command to arduino
  if (mouseButton == LEFT){
  /*if (mouseButton == CENTER){
    if (ab == false){
      ab = true;
    else if (ab == true){
      ab = false;

void mouseWheel(MouseEvent event){//sends commands to arduino to move servoA or B
  float e = event.getAmount();
  if(ab == true){
    if (e > 0){
      //port.write("u");//send up command.
    if (e < 0){
      //port.write("d");//send down command.
  else if (ab == false){
    if (e > 0){
      //port.write("a");//send left command.
    if (e < 0){
      //port.write("s");//send right command.

Just noticed all this at the bottom of Processing. I have no idea what it means.

java.lang.NullPointerException at Source) at Source) at eth_mouse_test.update( at eth_mouse_test.draw( at processing.core.PApplet.handleDraw( at processing.core.PGraphicsJava2D.requestDraw( at at

It seems to be that Processing is freezing, not Arduino.

Any ideas?

Is the below intentional?



Thanks! lol. maybe that will help!