help with Arduino UDP servo control with Ethernet shield

I am a total newbie at this stuff, but I have been reading the forums and going over examples in the wiki to try to figure this stuff out so please don't slaughter me right away :~. Anyways I have been trying to control a servo over a network using the Ethernet shield for Arduino. Here is my processing code

 UDP udp;  // define the UDP object


int gx = 15;
int gy = 35;
int spos=90;

float leftColor = 0.0;
float rightColor = 0.0;

void setup() 
 udp = new UDP( this, 8888 );  // create a new datagram connection on port 6000
 udp.log( true );         // <-- printout the connection activity
 udp.listen( true );           // and wait for incoming message  
  size(720, 720);
  colorMode(RGB, 1.0);


void draw() 
  rect(150, 320, gx*2, gx*2); 
  fill(180 - (mouseX/4)); 
  rect(450, 320, gy*2, gy*2);

void update(int x) 
String ip       = ""; // the remote IP address
 int port        = 8888;        // the destination port
  //Calculate servo postion from mouseX
  spos= x/4;

  //Output the servo position ( from 0 to 180)
 udp.send("s"+spos, ip, port); 

  // Just some graphics
  leftColor = -0.002 * x/2 + 0.06;
  rightColor =  0.002 * x/2 + 0.06;

  gx = x/2;
  gy = 100-x/2;


It seems to work. The Arduino gets packets, but I am not sure how to get it to respond to them. here is my arduino code, I am sure its broken horribly but I was hoping someone would look at it and offer a fix.

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: 12/30/2008
#include <Servo.h>

Servo servo1;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
  192,168,1,177 };

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet Do I need this? idk

void setup() {
  servo1.attach(14); //analog pin 0
void loop() {
 static int v = 0;
   if ( Udp.available()) {

     char ch = packetBuffer[UDP_TX_PACKET_MAX_SIZE];

//this is a problem ^^ up there =S
      switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
      case 's':
        v = 0;

     char ch = packetBuffer[UDP_TX_PACKET_MAX_SIZE];

The indices into the packetBuffer array range from 0 to UDP_TX_PACKET_MAX_SIZE-1. You are copying the first character beyond the end of the array into ch.

No wonder it doesn't work.

You need some kind of loop to loop through the characters in packetBuffer, after you fill packetBuffer. Currently, it does not look like you ever store anything in packetBuffer.

Aren't you supposed to read from the udp packet when udp.available() is greater than 0?

I have no idea, Never really done any coding before, just started fiddling with it last night. what you said seems reasonable, I think i grasp what your saying. I cant seem to find any similar coding around to copy from. I guess it will be more looking. Thanks! :slight_smile: