Arduino 1.0.5-r2 sketch not working anymore in 1.6.3

Howdy internet people,

I have a simple Arduino sketch that I use to drive a 16x16 RGB pixel matrix. The chipset is called WS2812 and I am using Adafruit’s library.

The sketch I have looks like this:

#include <Adafruit_NeoPixel.h>

int leds = 256;
int i = 0;

Adafruit_NeoPixel mx = Adafruit_NeoPixel(leds, 3, NEO_GRB + NEO_KHZ800);

void setup()
{
  mx.begin();
  mx.All(0);
  mx.Show(0);

  Serial.begin(200000);  
}

void loop()
{
  if (Serial.available() > 2 
    && Serial.read() == 1 
    && Serial.read() == 2 
    && Serial.read() == 3)
  {
    i = 0;
    while (i < 256)
    {
      if (Serial.available() > 2)
      {
        byte b1 = Serial.read(); 
        byte b2 = Serial.read(); 
        byte b3 = Serial.read(); 
        
        mx.Set(i++, mx.Rgb(b1,b2,b3));
      }
    }
    
    mx.Show(0);
  }
}

The PC software I am using to send the data is a .NET application that does something like this:

var arduinoSerial = new SerialPort();
arduinoSerial.PortName = comPort;
arduinoSerial.BaudRate = baudRate; // 200000 

try
{
    arduinoSerial.Open();
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

Followed by this code being called in a loop:

arduinoSerial.Write(new byte[] { 1,2,3 });
arduinoSerial.Write(bytes.ToArray());

The bytes array always contains 1+16163 bytes. The data is being send 30 times per second.
The code works fine with the default Arduino 1.0.5-r2 installation without any adaptations to the Arduino libraries (e.g. HardwareSerial buffer sizes).

Now, the problem!
I finally got around to installing the latest version of Arduino but now the sketch doesn’t work anymore. For some reason the Arduino stops reading the Serial because Serial.available() returns 0.

It does this after it has successfully received 20 leds, which amounts to 63 bytes. After that Serial.available() will returns ALWAYS 0, even though a lot more data was sent.

I’ve been up all night trying to pin-point the exact problem in the HardwareSerial classes but that didn’t result in anything. I’ve also tried other baudrates and timings, but nothing so far has allowed me to send more than 64 bytes at once.

Here’s what I want to know:

  • Are there any breaking changes in the Serial code that I should know about?
  • Do you know of other changes or problems regarding the Serial since 1.5?
  • What should I change in the code to get the Serial to work?
  • Can I use the HardwareSerial from 1.0.5-r2 in 1.6.3 if I patch the core? Should I?
  • What other options do I have?

Hope you can help, thanks a lot! All help is very appreciated.

Here's a video of the LED matrix in action: https://www.youtube.com/watch?v=wWysMw5Jd-c

Serial.begin(200000);

Does arduino support this baud rate ??

Maybe. I've also tried 9600, 19200, 57600 and 115200 and all of them show the same result.

http://arduino.cc/en/Serial/available

But I don't know why it worked before ....

What if you change this :

if (Serial.available() > 2)

to

if (Serial.available() > 0)

waski: http://arduino.cc/en/Serial/available

But I don't know why it worked before ....

Good point. I don't know why either. I do remember reading about it somewhere and trying it out, and it definitely works in 1.0.5-r2.

Did you try changing

if (Serial.available() > 2)

?

Yes, doesn't work yet but I am getting different results though. Let me work at it a little more ..

Allright, all fixed now, thanks for your suggestion. It turned out to be very relevant! I've made two fixes to the code to get everything working again:

1) Any idea why this needs to be 0?

if (Serial.available() > 0)

2)

// Changed arduinoSerial.Write(new byte[]{1, 2, 3}); arduinoSerial.Write(bytes.ToArray()); // To arduinoSerial.Write(bytes.ToArray());

The 1,2,3 message is still part of the payload but it is written onto the Serial bus at once.