fast serial sendin


hi,
im trying increase rate @ can sample analog , digitals , send pc.. need know sampling rate accurately im using interrupt. wondering though there faster way calling serial.print many times? send bytes in 1 call @ end of reading? or serial.print handle buffering , sending effeciently? heres code read , send far. can send 2 analog , 6 digital in 7ms. nee faster. maybe im being unrealistic?
thanks pointers

code: [select]
{
 
 /* analog in, number enabled */
     for(analogpin=0; analogpin<analoginputsenabled; ++analogpin)
     {
           analogdata = analogread(analogpin);
serial.print(analogdata >> 7, byte); // shift high bits output byte
           serial.print(analogdata % 128, byte); // mod 32 small byte
           
               
     }

               serial.print(255, byte); // end signifier  
             
               
//digital pin checking stuff

for(n=2; n<8; ++n) {
                   
                       digitaldata = (byte) digitalread(n);
                       if (digitaldata == low)
                       {
                   x |= (1 << (n - 2));       // forces nth bit of x 1.  all other bits left alone.
                       }
                       else
                       {
                         x &= ~(1 << (n - 2));    // forces nth bit of x 0.  all other bits left alone
                       }

               }
            // if (x != lastdigital){
             serial.print(x, byte);
               serial.print(254, byte); // end signifier  

 }
 looptime = millis() - starttime;
 serial.println(looptime);
                     
}

have tried increasing baud rate?

at 9600 baud, maximum throughput going 0.96 bytes/ms.  if you're sending 5 bytes (two each analog, 1 digital measurements) in 7ms sending 0.71bytes/ms approaching maximum.

code: [select]
serial.print(analogdata % 128, byte)
i'm not sure how % operator gets compiled, may tiny improvement usign logical , and typecast, e.g.
code: [select]
serial.print((byte) analogdata & 0xff80, byte);

put digital lines on 1 port (check atmel docs or pin mapping) , read them in 1 chunk using pinx.  for example, here's bit of code read 4 bits @ once:
code: [select]
tone = pind >> 4;
in example, want read high 4 bits of port d (which maps arduino pins 4-7) , store them 0-15 (low 4 bits) in "tone".  i in 2 operations (register read , shift) rather 4 function calls , bunch of bit wiggling 'em stored way want.

oh, saw "serial.println(looptime)".  if isn't measurement rid of it.

these incremental things, though.  basically you're going have increase baud rate.

-j


Arduino Forum > Forum 2005-2010 (read only) > Software > Interfacing > fast serial sendin


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums