How can I stock data, and send them over by serial? I want to make an array with data from the sensor. In data, I have displacement in x/y axis. I want to send them in the array, because in my python code I don't send them one by one, slow down really quickly program.

Arduino code responsible for sensor

class MouseRptParser : public MouseReportParser
{
protected:
 void OnMouseMove (MOUSEINFO *mi);
};

void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{

    Serial.print(mi -&gt;dX, DEC);
    Serial.print(",");
    Serial.println(mi -&gt;dY, DEC);

};


I tried to do something on my own, but I get stuck with an error about data not in the scope. I try some variant placing it, but I don't succeed .

class MouseRptParser : public MouseReportParser {

 void OnMouseMove (MOUSEINFO *mi); };

void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{ int i;   for (i=0;i&lt;10;i+2)   {
        data[i]=(mi -&gt;dX, DEC);
        data[i+1]=(mi -&gt;dY, DEC);   } 
  Serial.println(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9]);

    delay(20); };


EDIT:  I have now program that's making an array of 10 cells, but values in it are constatnt 10.

#include &lt;hidboot.h&gt;
#include &lt;usbhub.h&gt;

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include &lt;spi4teensy3.h&gt;
#endif
#include &lt;SPI.h&gt;
int data[10];

class MouseRptParser : public MouseReportParser
{
protected:
 void OnMouseMove (MOUSEINFO *mi);
};

void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
{

    for (int i=0; i&lt;10; i=i+2)
  {
    data[i]=(mi-&gt;dX, DEC);
    data[i+1]=(mi-&gt;dY, DEC);

  }
  for(int j=0; j&lt;10; j=j+1)
  {  
  Serial.print(data[j]);
  Serial.print(",");
}
Serial.println();
  delay(100);
};

USB     Usb;
USBHub     Hub(&amp;Usb);
HIDBoot&lt;USB_HID_PROTOCOL_MOUSE&gt;    HidMouse(&amp;Usb);

MouseRptParser Prs;

void setup()
{
    Serial.begin( 9600 );
#if !defined(__MIPSEL__)
    while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
    //Serial.println("Start");

    if (Usb.Init() == -1)
        Serial.println("OSC did not start.");




    HidMouse.SetReportParser(0, &amp;Prs);
}

void loop()
{


  Usb.Task();

}
There is no such thing as "all at once" with serial. The whole point of serial is that it sends things one character at a time.

It is entirely up to you to decide what constitutes one "block" of data.

There is absolutely no difference (besides some slight processing overheads) between:

Serial.print("Hello");
Serial.print(" ");
Serial.println("World");


and:

Serial.println("Hello World");


Both of them result in Serial sending:

H
e
l
l
o
[space]
W
o
r
l
d
[cr]
[lf]


No matter how you "batch" stuff together (or attempt to) it makes no difference.

You need to have:


Some way of identifying the start of a block of data,
A mechanism to receive the entire block a byte at a time, and
Some way of identifying the end of a block of data.


How you do that is entirely up to you.



Here's an example of how to arrange the storage:

class MouseRptParser : public MouseReportParser
{
    private:
        int dx[10];
        int dy[10];
        int dpos = 0;

    protected:
        void OnMouseMove (MOUSEINFO *mi) {
            dx[dpos] = mi-&gt;dX;
            dy[dpos] = mi-&gt;dY;
            dpos++;

            if (dpos == 10) {
                dpos = 0;
                for (int i = 0; i &lt; 10; i++) {
                    Serial.print("X");
                    Serial.print(dx[i]);
                    Serial.print("Y");
                    Serial.print(dy[i]);
                }
                Serial.println();
            }
        }
};


What that gives you is a pair of 10-entry arrays, one for X and one for Y values. Each time a report arrives the current DX and DY values are stored. The pointer for where to store is incremented.  Then, if that pointer equals 10, reset it to 0 and print all the values.

I chose a format that gives you:

X3Y-2X2Y-4X6Y8X-3Y7X6Y38X176Y76X-4Y8X8Y3X39Y-32X71Y4


That is, an "X" followed by a decimal value for a change in X, and a Y followed by a decimal value for a change in Y. The whole lot is then terminated by a standard CR/LF line ending pair.