Problem:
I have an 6 DoF accelerometer that I can access through SDA/SCL via an ArduinoUNO. I can read inputs through the commands like:
LreadAccX()
There are 2 registers for the sensor, a LOW and HIGH value. The above would read the lower register's X acceleration. These are 8 bit numbers. For example, the above might return:
LreadAccX()
>>>> 01000010
41 in decimal, by the way. I need to get these out as fast as possible, we'd like 1kHz to 400Hz if possible. That means just spitting out binary data and then post processing it.
Example:
0000 0001 1100 1001
This may be a value for the X acceleration. In decimal it says: 457. That's 3 different ASCII chars that I have to log, not 2 as in binary.
1111 0001 1100 1001
This is 61897, so 5 ASCII chars, vs. just the 2 binary ones. Obviously, I want to use binary to optimize for speed.
My Solution
void loop() {
/*
print data to processing. Data broken up into 2 parts, one for the high and low
registers in the sensor.
*/
print_bytes(HreadAccX(),LreadAccX(),HreadAccY(),LreadAccY(),HreadAccZ(),LreadAccZ());
print_bytes(HreadGyroX(),LreadGyroX(),HreadGyroY(),LreadGyroY(),HreadGyroZ(),LreadGyroZ());
Serial.print("A");
}
inline void print_bytes (int HaX, int LaX, int HaY, int LaY, int HaZ, int LaZ)
{
char l = (LaZ >> 48) & 0xff ;
char k = (LaZ >> 44) & 0xff ;
char j = (HaZ >> 40) & 0xff ;
char i = (HaZ >> 36) & 0xff ;
char h = (LaY >> 32) & 0xff ;
char g = (LaY >> 28) & 0xff ;
char f = (HaY >> 24) & 0xff ;
char e = (HaY >> 20) & 0xff ;
char d = (LaX >> 16) & 0xff ;
char c = (LaX >> 12) & 0xff ;
char b = (HaX >> 8) & 0xff ;
char a = HaX & 0xff ;
putchar (l) ;
putchar (k) ;
putchar (j) ;
putchar (i) ;
putchar (h) ;
putchar (g) ;
putchar (f) ;
putchar (e) ;
putchar (d) ;
putchar (c) ;
putchar (b) ;
putchar (a) ;
Serial.print(l);
Serial.print(k);
Serial.print(j);
Serial.print(i);
Serial.print(h);
Serial.print(g);
Serial.print(f);
Serial.print(e);
Serial.print(d);
Serial.print(c);
Serial.print(b);
Serial.print(a);
}
The output is something like:
>>>> asFkDi?g-g^&A
as Fk Di ?g -g ^& A (for clarity, the diff 16 bits have spaces, and the 'A' is
to show a start/stop bit)
It's just garbage in ASCII, but it translates to meaningful 16bit numbers
However, this Chewbacca as far as elegance goes. It also runs at about 200Hz, waaay too slow. I feel that it's the
Serial.print();
functions that are slowing it down. However, if I try:
Serial.print(a+b+c+d+e+f+g+h+i+j+k+l);
all I get is the addition of the numbers.
My Question
How do I get the Seria.print() to output just a string of binary numbers from a set of arguments?