UdpString sketch works 35 times the stops.

Hi all,

I'm very new to Arduino and programming of this kind in general.
I've bashed together a sketch to make a BlinkM toggle colours via UDP for a tally light system. I thought I'd cracked it today but I've found that it works 35 times and then stops. It will work again after a reset but does the same again. I think it needs the packet clearing or something like that but can't seem to work out how to achieve this. Please help.

#include <Ethernet.h>
#include <WString.h>
#include <Wire.h> 
#include <BlinkM_funcs.h>
#include <UdpString.h>
#include <WString.h>

#define maxLength 14
String inString = String(maxLength);

byte mac[] = { 0x51, 0x45, 0x44, 0x31, 0x30, 0x31 }; // mac address
byte ip[] = { 172, 16, 17, 101 }; // Arduino's IP address
int localPort = 3002; // local port to listen on
String packet(32); //packet can be max 32 bytes long
byte remoteIp[4]; // holds recvieved packet's originating IP
unsigned int remotePort[1]; // holds received packet's originating port

int i;

int r;
int g;
int b;

String binaryString(4);

/* SETUP: init Ethernet shield, start UDP listening, open serial port */
void setup() {
  Ethernet.begin(mac,ip);
  UdpString.begin(localPort);
  Serial.begin(9600); 
  Wire.begin();
  BlinkM_beginWithPower();
  BlinkM_stopScript( 0x00 );
  BlinkM_setRGB( 0x00, 0xFF, 0x00, 0x00 );
  delay (1000);
  BlinkM_setRGB( 0x00, 0xFF, 0xFF, 0x00 );
  delay (1000);
  BlinkM_setRGB( 0x00, 0x00, 0xFF, 0x00 );
  delay (1000);
  BlinkM_setRGB( 0x00, 0x00, 0x00, 0xFF );
  delay (1000);
}

/* LOOP: wait for incoming packets and print each packet to the serial port */
void loop() {  
    boolean current_line_is_blank = true;
    
  // if there's data available, read a packet
  if(UdpString.available()) {
    int packetSize = UdpString.readPacket(packet,remoteIp,remotePort);
     char* c = packet;
       if (inString.length() < maxLength)  {
         inString.append(c);
       }
       if (inString.contains("$"))  {
           int Pos_r = inString.indexOf("r");
           int Pos_g = inString.indexOf("g");
           int Pos_b = inString.indexOf("b");
           int End = inString.indexOf("!");
           r = atoi(inString.substring((Pos_r+1), (Pos_g)));
           g = atoi(inString.substring((Pos_g+1), (Pos_b)));
           b = atoi(inString.substring((Pos_b+1), (End)));
           BlinkM_setRGB( 0x00, r, g, b );
         }
  }
  
  //wait a bit
  delay(10);
  inString = "";
  packet = "";
}

What version of the Arduino IDE are you using?

There is a known memory leak issue with WString, and a patch that has been posted. Have you applied that patch?

Why do you include WString.h twice?

it works 35 times and then stops

What stops? You can't read any more packets?

Hi Paul,

Thanks for your quick reply - you have been a great help.
As I said in the first post I'm new to Arduino so my forum style probably needs a little honing..!

To address your reply in no particular order...

I'm using IDE 0018, a Duemilanove 328 with ethernet shield and a single BlinkM.

Oops, I didn't notice I'd included WString.h twice - thanks for reading my code more thoroughly than I did..! (Note to self: type fresh code, rather than coping and pasting blocks)

After issuing 35 commands e.g. "$r255g000b000! from a netcat session in OSX Terminal or using the Vista Spyder system that the project is being made to work with) the BlinkM stopped changing colour, although I could still see activity on the Ethernet shield indicators. (Note to self: leave Serial.print() in for debugging)

I hadn't applied the patch to WString but quickly found it here --> http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1241618944/12 and inserted this line to attempt to fix the problem;

~String() { free(_array); }

This seems to have fixed the problem, I've just pushed 50 odd commands at it via OSX Terminal so I suspect that it's sorted. I will soak test it properly using a playback loop on the Spyder at work tomorrow before I start doing any flic-flacs...

Thank's again for your help.

Dan