Help with serialport buffer overwrite on Nano

This serial port is driving me crazy... The project is for Adalight.

Here is the code:

#include <FastSPI_LED.h>

//------------------------------------------------------
// Config
//------------------------------------------------------

//LED communication-pin
#define PIN 4

//Number of LEDs
#define NUM 50

//Key byte to syncronize begining of frame with BobLight (prefix)
#define KEY 0xFE

//Change order of RGB below for some LED pixel brands.
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *leds;

//int pixel = 0;
//int color = 0;
//int buffer;

//------------------------------------------------------
// Setup - Main Code
//------------------------------------------------------
void setup()
 {
  //Baud Rate
  Serial.begin(115200);

  //FastSPI Setup (choose correct Chipset)
  FastSPI_LED.setLeds(NUM);
  FastSPI_LED.setChipset(CFastSPI_LED::SPI_TM1809);
  FastSPI_LED.setPin(PIN);
  FastSPI_LED.init();
  FastSPI_LED.start();
  leds = (struct CRGB*)FastSPI_LED.getRGBData(); 
  memset(leds, 0, NUM * 3);
  FastSPI_LED.show();

  //Startup (brighten to full white, then off)
  for(int i=0; i<150; i++)
   {for(int j=0; j<NUM; j++)
     {leds[j].r = i;
      leds[j].g = i;
      leds[j].b = i;}
    FastSPI_LED.show();
    delay(15);}
  memset(leds, 0, NUM*3);
  FastSPI_LED.show();

  //Send "Ada" for auto port-detection by BobLight
  Serial.print("Ada\n");
  delay(500);

  //Display Placment Pattern for 50-leds at 16x9 (15x8 + 4 corners)
  leds[0].r = 255; leds[0].g = 255;  //Yellow Center Bottom
  leds[1].g = 255; leds[2].g = 255; leds[3].g = 255; leds[4].g = 255; leds[5].g = 255; leds[6].g = 255; leds[7].g = 255;
  leds[8].r = 255;  //Red Corner
  leds[9].b = 255; leds[10].b = 255; leds[11].b = 255; leds[11].r = 63; leds[12].b = 255; leds[12].r = 255; leds[13].b = 255; leds[13].r = 255; leds[14].b = 255; leds[14].r = 63; leds[15].b = 255; leds[16].b = 255;
  leds[17].r = 255;  //Red Corner
  leds[18].g = 255; leds[19].g = 255; leds[20].g = 255; leds[21].g = 255; leds[22].g = 255; leds[23].g = 255; leds[24].g = 255;
  leds[25].r = 255; leds[25].g = 255;  //Yellow Center Top
  leds[26].g = 255; leds[27].g = 255; leds[28].g = 255; leds[29].g = 255; leds[30].g = 255; leds[31].g = 255; leds[32].g = 255;
  leds[33].r = 255;  //Red Corner
  leds[34].b = 255; leds[35].b = 255; leds[36].b = 255; leds[36].r = 63; leds[37].b = 255; leds[37].r = 255; leds[38].b = 255; leds[38].r = 255; leds[39].b = 255; leds[39].r = 63; leds[40].b = 255; leds[41].b = 255;
  leds[42].r = 255;  //Red Corner
  leds[43].g = 255; leds[44].g = 255; leds[45].g = 255; leds[46].g = 255; leds[47].g = 255; leds[48].g = 255; leds[49].g = 255;
  FastSPI_LED.show();
 
  //MainLoop
  for(;;)
   {if (Serial.available())
     {if (Serial.read()==KEY)
       {Serial.write(KEY); delay(1);
        if (!Serial.available()) {}
        if (Serial.read()==0x00)
         {Serial.write(0xFD); delay(1);
          if (!Serial.available()) {}
          if (Serial.read()==0x00)
           {Serial.write(0xFC); delay(1);
             
            //Now Load All Bytes
            for (int pixel=0; pixel<NUM; pixel++)
             {if (!Serial.available()) {}
              leds[pixel].r = Serial.read();
              Serial.write(leds[pixel].r); delay(1);
              if (!Serial.available()) {}
              leds[pixel].b = Serial.read();
              Serial.write(leds[pixel].b); delay(1);
              if (!Serial.available()) {}
              leds[pixel].g = Serial.read();
              Serial.write(leds[pixel].g); delay(1);
             } 
            FastSPI_LED.show();
            //Serial.flush();
 } } } } } }


//------------------------------------------------------
// End
//------------------------------------------------------

void loop()
{
  // loop() is avoided as even that small bit of function overhead
  // has a measurable impact on this code's overall throughput.
}

Here is the output when sending it a HEX file of 153bytes at 115200:

FE FD FC 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 70 82 94 
A6 B8 C6 D8 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 
FF FF FF FF FF FF FF FF FF

You can see it starts dropping bytes after 66.

It gets to 73 at 57600, it gets to A9 at 19200.
At 9600 it doesn't work with delay(1). With delay(2) it get to C9.
At 4800 it needs delay(3) it works for my test, but when I run BobLight at 4800 with a 1sec delay between packets, I just see the second half as white pixels for a second, and then they all go off except the last pixel which stays blue.

I would love to run at 115200 and get my 150byte-packets, one packet a second

I probably cant help anyway but I dont understand
I see 153 bytes there for your 115200
Are you saying the FF s should be something else
Where is the fast LED library?

Why do you have all those delay()s in your read code? They are causing your read code to be a lot slower than it could be.

There are a number of coding styles, with respect to position of the curly braces and code. None of them suggest jamming code up tight against a {, on the same line. There are good reasons why that style has not seen widespread use.

        if (!Serial.available()) {}

If and while do completely different things. They are not interchangeable.

You have quite a few of these where you need to have while statements.

Yes, those FF's and not supposed to be there... it is supposed to count up. It does not work without the Delays and you need longer delays at slower baudrates. I am not sure why, but I guess if (!Serial.available()) {} is not correct unless you delay after a Serial.read(). As I said "At 4800 it needs delay(3)" and it works that way for sending my test count-up file with correct 153 bytes everytime. I now have it working with boblight+getpixel too, kinda... the colors are off, but it does change in response to screen color. Also, it is delayed so a single color-change fades in over 5 seconds, blinking once a second.

Can someone please explain to me how boblight (1.3b for windows) is sending it's packet structure? I thought it was <0x00> <0x00> <1:r> <1:g> <1:b> <2:r> ... Is the packet format different then that?

I am also looking in to just writing my own windows program that uses CreateDIBSection to read the screen pixels and sends out the serial bytes to the Arduino.

EDIT: But I still don't understand how I can send a single 153-byte packet at 115200 and receive them all correctly. Why does it drop all those bytes, skipping, and then replacing the missing end bytes with FF?

I figured out my issue, why I needed the delays (or didn't need them), and got 115200 working.

"if (!Serial.available()) {}" should have been "while (!Serial.available()) {}", then the delays are unnecessary and everything works full speed!

Now does anyone know the packet format of bob-light? It is still not working right, but my self-made app works, it just has a while to go before it can average pixels as well as boblight already does.

If any one is interested... I made my own software for both Arduino driving WS2811 bulbs (or anything else the FastSPI library is compatible with) and for Windows. I'll be selling ready-made kits shortly. All you have to do is peal the back cover off the tape, stick it on the back of your 46"-70" TV, and plug in the power and usb cables (both included). Then run my software and you got yourself an Adalight/Atmolight/RPMlight that works at 30fps even in Windows 7/8 with Aero enabled.

Contact me at gmail if interested (in my product, or my open-source code, or with any questions)... same username as here.

DSC02859.JPG

rpmccormick:
If any one is interested... I made my own software for both Arduino driving WS2811 bulbs (or anything else the FastSPI library is compatible with) and for Windows. I'll be selling ready-made kits shortly. All you have to do is peal the back cover off the tape, stick it on the back of your 46"-70" TV, and plug in the power and usb cables (both included). Then run my software and you got yourself an Adalight/Atmolight/RPMlight that works at 30fps even in Windows 7/8 with Aero enabled.

Contact me at gmail if interested (in my product, or my open-source code, or with any questions)... same username as here.

?? ????? ?? ?? ??????? ????? ? ?????????? ?? WS2811????? ????? ??????????