Leonardo-Micro CDC communication

Hi everybody,
I'm new in Arduino and I need some help please, since I work with biomedical instrumentation but I'm not an expert in computer programming! I have to read from 12 emg sensors with my Micro. I proceeded to change the ADC prescaler select bits to achieve the desired sampling frequency, and this seems to be working properly (total acquisition time about 400 microseconds). Now I have to send these values to my pc at the appropriate speed (acquisition + transmission <= 1 millisecond). I read on the ArduinoBoardMicro page

The 32U4 also allows for serial (CDC) communication over USB and appears as a virtual com port to software on the computer. The chip also acts as a full speed USB 2.0 device, using standard USB COM drivers.

and from what I understand with Serial. commands it uses the CDC protocol. The only way I have found is to send the array with the 12 values with Serial.write(buf, len), hence without ASCII conversion of Serial.print(). Furthermore with Serial.print() I can only send one at a time with a for cicle and it's too slow. This is the code:

[quote]
[color=#CC6600]int[/color] pin[] = {A0,A1,A2,A3,A4,A5,4,6,8,9,10,12};
[color=#CC6600]byte[/color] a[] = {0,0,0,0,0,0,0,0,0,0,0,0};

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](115200);

   [color=#CC6600]while[/color] (![color=#CC6600][b]Serial[/b][/color]) {
     ; [color=#7E7E7E]// wait for a serial connection to be established[/color]
  }   
  
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
     
     [color=#CC6600]for[/color] ([color=#CC6600]byte[/color] i = 0; i < 12; i++)
     {
       a[i] = [color=#CC6600]byte[/color]([color=#CC6600]analogRead[/color](pin[i])/4);      
     }  
     [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](a, sizeof(a));
     [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]flush[/color]();
     
     [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]""[/color]);
}

[/quote]

Now how can I view and save the Micro sent values array? Otherwise, where can I find the

standard USB COM drivers

mentioned in the Main Site and how can I use them to improve transmission rate and than use the Serial.print(a*) command? There are other ways to increase the byte transfert rate? Thanks.*

This is the code

Really? Does your code REALLY look like that?

Do not use Copy For Forum. It's useless. Ctrl-A, Ctrl-C, and Ctrl-V (after pressing the # icon) are all you need to use.

The only way I have found is to send the array with the 12 values with Serial.write(buf, len)

That's going to be far faster than converting the bytes to strings, sending the strings, and converting the strings back to bytes.

But, since the ADC returns ints, sending bytes requires care. Since I can't read that mess you posted, I can't tell if you take this care, or not.

Furthermore with Serial.print() I can only send one at a time

Neither true or relevant. You can convert all the values to a single string, using sprintf() and send that string "all at once". But, the data won't be sent any faster than doing 12 separate Serial.print()s.

It takes just as long to send the data whether the data to be sent is defined by one call or 12, since the amount of data is the same in either case.

I'm sorry! This is the code:

int pin[] = {A0,A1,A2,A3,A4,A5,4,6,8,9,10,12};
byte a[] = {0,0,0,0,0,0,0,0,0,0,0,0};

void setup() {
   Serial.begin(115200);
   while (!Serial) {
     ; // wait for a serial connection to be established
  }   
  
}

void loop() {
     for (byte i = 0; i < 12; i++)
     {
       a[i] = byte(analogRead(pin[i])/4);
     }  
     Serial.write(a, sizeof(a));
     Serial.flush();
}

This is the code:

It does what? What application on the PC is expecting to read binary data?

It send to pc the arrays that i'm tryin to read with Matlab using fread.

Change your code to use

for(byte b=0; b<sizeof(a); b++)
{
   Serial.print("a[");
   Serial.print(b);
   Serial.print("] = ");
   Serial.println(a[b], HEX);
}

in place of:

     Serial.write(a, sizeof(a));

Get rid of the useless call to Serial.flush()!

Then, open the serial monitor. Do you see reasonable values being printed?

If so, put the Serial.write() back, in place of the for loop. Then, ask on the matlab forum why the data doesn't get read correctly.

You might want to explain what ACTUALLY happens. "It doesn't work" is too lame to solicit help.

Thanks a lot, your tips were precious. It seems to be working properly! Bye