works on Uno and Mega but not on Lilypad - Serial.ParseInt()

Has anyone else experienced a similar issue with Serial.parseInt() on a lillypad.

I have tested using:

  1. Lilypad Arduino - ( Qty -2 )

  2. Uno R3 -

  3. Mega 2560 -

  4. Mega ADK -

We have several arduinos in this project. All have an Xbee Series one connected. One arduino is the ‘master’ and ‘transmits’ a comma delimited string of 7 values, each value between 0 and 255 ( e.g.: 0,12,345,67,255,128,64 ). It sends it continuously and we are not concerned about flow control ( I have considered this may be the issue, more on that below) or the order of the data - it is sort of like a random number generator but the data comes from an audio frequency analyzer and we are using it to drive LEDs on the remote arduinos.

The receiving arduinos, we have a couple of flavors; but all essentially do the same thing. They use Serial.parseInt() to receive and parse the string and drive LEDs using Digital output and PWM.

So using any combination of the above listed hardware - except the lilypad - for sending and/or receiving and this all works…we have tested using version 1.0, 1.01, 1.04 and 1.05.

However, when we use the Lilypad with the EXACT same sketch it does NOT work - to keep the playing field even I have come up with the sketch below to help debug the problem. I used LEDs to determine where the sketch is since I need the lilypad’s only Serial Tx and Rx pins for the Xbee. So the same sketch, the same bread board and the same power supply - just swapping out the Lilypad for one of the standard Arduinos.

This program, running on either Mega or Uno, works fine. However, when we substitute in the Lilypad it sits there with pin 3 off - i.e. at Serial.parseInt(). I suspect the issue is some sort of buffer problem because, purely by accident, I discovered if I turn off the ‘Sending’ arduino I then see the RX light on the ‘Receiving’ Xbee turn off…and then the Lilypad appears to execute one full loop properly. But with the ‘Sending’ Xbee running and continuously sending data the Lilypad hangs.

BTW- all the Xbee serial i/f are set to 9600 baud - changing that from 115200 to 9600 did not help. Also note that I ordered a brand new Lilypad to eliminate a hardware problem and the 2nd Lilypad behaves identical to the 1st.

I am going to go off and explore ( i.e. google) ways to dump the serial buffer on each loop to see if that helps… I really need the Lilypad to work as the project is for t-shirts ( sewing Unos or Megas, while doable is not desirable) … but in the mean time, thank you for reading this far and any suggestions would be greatly appreciated.



// Test Program

int ground = 8; // use as a common ground for all the LEDs
int FreqVal[7]; //array to receive the data from the Xbee stream
//int PinString[7] = {3,5,6,9,10,11,11}; // normally we would light up 6 LEDs based upon value of FreqVal - this line is commented out.
int PinString[7] = {5,5,5,5,5,5,5}; // ← used for debug only - here we just dump the analogwrite to pin 5
void SendColor(int pin, int intense)

  • analogWrite( 11 , 0); // turn off Pin 11 to indicate pre analogwrite*

  • analogWrite( PinString[pin] , intense) ; *

  • delay(1000); // wait a second so I can catch it*

  • analogWrite( 11 , 255); // turn the light back on*
    void setup()

  • Serial.begin(9600); //Serial Monitor Init the baudrate - like bits per second*

  • pinMode(ground,OUTPUT); // common ground for LEDs*

  • digitalWrite(ground,LOW); // set ground low*

  • analogWrite( 3 , 255); // set debug pin 3 high - so I know its working*

  • analogWrite( 6 , 255); // set debug pin 6 high - so I know its working*

  • analogWrite( 11 , 255); // set debug pin 8 high - so I know its working*

  • delay(4000); // wait 4 seconds*

  • analogWrite( 3 , 0);*

  • delay(2000);*

  • analogWrite( 3 , 255); // blink pin 3 - signals the end of setup()*
    void loop()

  • while (Serial.available() > 0) // Read from Xbee*

  • {*

  • delay(1000);*

  • for (int j=0; j<7; j++)*

  • {*

  • analogWrite( 3 ,0); // turning off pin 3 indicates we are about to execute Serial.parseInt()*

  • FreqVal[j] = Serial.parseInt();*

  • delay(500);*

  • analogWrite( 3 , 255); // Turn 3 on again - so we are out of parseInt()*

  • }//end for*

  • for (int i=1; i < 7; i++)*

  • {*

  • analogWrite( 6 ,0); // Turn pin 6 off indicates we are calling SendColor()*
    _ int Intensity = FreqVal*; _
    SendColor(i, Intensity); _
    analogWrite( 6 ,255); // Turn pin 6 back on means we have returned*_
    * }//end for*
    * }//end while*
    }// end loop

The only difference the Lilypad has to the other Arduinos (at least to the UNO) is the clock speed. It runs on 8MHz.

The most probable cause for a freeze is a constant stream of characters on the serial interface that are not digits:

// returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters
int Stream::peekNextDigit()
  int c;
  while (1) {
    c = timedPeek();
    if (c < 0) return c;  // timeout
    if (c == '-') return c;
    if (c >= '0' && c <= '9') return c;
    read();  // discard non-numeric

This will not return if there is constantly a stream of characters (because of the timeout) and none of them is a digit or ‘-’.
I would try to check what your Lilypad is receiving. If you expect it to be digits and it gets something else, you have the cause of your problems.