2-Dimensional Array to byte String

Hello,

I have an LED message display project where i'm trying to transfer a 56byte array from Processing to Arduino.
Here's a link for more info and code: http://arduino.cc/forum/index.php/topic,61583.0.html

Just wondering if someone could advise me how I should pass a 2D array into a serial string with start/end characters?
Similarly, I'm also looking for the Arduino to put the incoming serial string back into an array.

Array looks like this...

// The 56 Byte RED Image Array
int[][] Rimg = {  // default array displays 'ARDUINO'
  {00011000,01111100,01111100,01000010,00111000,01000010,00011000},  // [0] Entire Row 1
  {00100100,00000000,00100010,01000010,00010000,01100010,00100100},  // [1] Entire Row 2 
  {01000010,01000010,00100010,01000010,00010000,01010010,01000010},  // [2] Entire Row 3
  {01111110,01111100,00100010,01000010,00010000,01001010,01000010},  // [3] Entire Row 4
  {01000010,01001000,00100010,01000010,00010000,01000110,01000010},  // [4] Entire Row 5
  {01000010,01000100,00100100,01000010,00010000,01000010,00100100},  // [5] Entire Row 6
  {01000010,01000010,01111000,00111100,00111000,01000010,00011000},  // [6] Entire Row 7
  {00000000,00000000,00000000,00000000,00000000,00000000,00000000}   // [7] Entire Row 8
};

Could anyone help me out please? :wink:

Sure. By asking some questions.

// The 56 Byte RED Image Array
int[][] Rimg = {  // default array displays 'ARDUINO'

7 columns and 8 rows of ints do not occupy 56 bytes on the PC or on the Arduino.

Since the values in the array are all less than or equal to 255, why isn't the array type byte?

Why do you want to convert a bunch of bytes to string representation, which will require as much as 9 times as much space, when you could send the bytes in binary mode?

If you send the data in binary mode, you can just iterate through the array, row by row or column by column, sending the elements one by one. On the receiving end, just put the bytes in the array in the same order (row by row or column by column) as they were sent.

Hmm looks like I've got a bit confused here then..

In Processing I used 'int[][]' because 'byte[][]' didn't appear to work out. The console gives tells me I cannot convert int to byte. Perhaps this is to do with the format of the items contained in the array e.g. '10010010'? I hadn't realised it would take up so much space - looks like I need to do some more reading up..

I assumed that I need to have a binary string to send from Processing so that I can have start/end characters.. So that the Arduino could recognise a full 'image' to prevent the led display from showing nonsense where bytes may have been missed out in communication.

How should I go about sending a string in binary mode from Processing?

Thanks :wink:

Actually, yeah, you have a problem with the initialization. You probably want to be assigning values like 0b01111110 to the array elements, not 01111110.

alexdlp:
I assumed that I need to have a binary string to send from Processing so that I can have start/end characters.. So that the Arduino could recognise a full 'image' to prevent the led display from showing nonsense where bytes may have been missed out in communication.

How should I go about sending a string in binary mode from Processing?

I recommend you start off simple, and not worry too much (or at all) about corrupted data until it actually proves to be a problem. At most, I might implement some form of simple synchronization, where the Arduino issues a reset command to your Processing sketch to start transmitting data from the beginning. This would be more of an initialization activity that the Arduino performs on startup.

If at some point in the future corrupted data transmission does prove to be a problem that needs to be solved, then you can look into some form of packetized transmission that incorporates a checksum for data integrity, but that gets complicated fairly quickly.

As for how to send data from Processing, I'm not all that familiar with Processing, but I believe it handles serial comms similar to Arduino (or rather Arduino handles serial comms similar to processing). So you should just be able to either Serial.write() your array data out or alternately walk through your array and Serial.write() each value. Look at the Processing reference for the Serial class and you should find information on how to use write().

Alrighty, I'm gonna take little steps first because you might be able to tell this is all new for me..

Let's get the byte array sorted out... Instead of using int type data, I should be using byte type.

So instead of...

int[] charA = {  // character set for the letter 'A'
  00011000,  // [0] row one
  00100100,  // [1] row two
  01000010,  // [2] row three
  01111110,  // [3] row four
  01000010,  // [4] row five
  01000010,  // [5] row six
  01000010,  // [6] row seven
  00000000   // [7] row eight
};

Does this look more sensible?

// character set for the letter 'A'
byte[] charA = {24,36,66,126,66,66,66,0};

The only thing is you don't get a nice visual representation of the 'pixels' for each matrix row and i'll have to work out the binary values. At least I'm not using loads of unnecessary space like before with integers.

Before I go through all the characters, does this look right?

int[] charA = { // character set for the letter 'A'
00011000, // [0] row one

That may look like binary, but it is actually octal.

You are mixing C# array declarations into your C++ code, too. In C++, the [] follow the array name, not the type.

Oh dear.. So it should be...?

byte charA[] = {  // character set for the letter 'A'
  24,36,66,126,66,66,66,0};

So it should be...?

Yes, but why are you using decimal values? Hexadecimal or binary is more readable in this case.

Alright I'm sorry but I'm now totally confused, what would 00011000 (if that's octal) look like in a byte type array as binary?? :s

B00011000 or 0b00011000

The sequence of characters 00011000 is interpreted by the compiler as octal because it starts with a leading 0. I'm pretty sure that you meant that sequence of characters to represent a binary value, so it needs a leading B or 0b.

char whatever[] = {0b00011000, ... };

Okay, here's what i've just tried in Processing... for both 0b,B and char,byte array types.

Console: "Cannot find anything named "B00011000"

char charA[] = {  // character set for the letter 'A'
  B00011000,
  B01000100
};

"Unexpected Token: b00011000"

char charA[] = {  // character set for the letter 'A'
  0b00011000,
  0b00100100
};

"Cannot find anything named "B00011000"

byte charA[] = {  // character set for the letter 'A'
  B00011000,
  B01000100
};

"Unexpected Token: b00011000"

byte charA[] = {  // character set for the letter 'A'
  0b00011000,
  0b00100100
};

This is starting to bug me now :astonished:
Thanks for all the quick responses btw :wink: I love this forum 8)

Java - Grrrrr.
Nope, sorry, you're stuck with hex, octal or decimal, but not, it seems, binary.

EDIT: Here's a cunning plan: define all the constants as octal unsigned long (ALWAYS with a leading zero), so you can see what it is supposed to look like, but convert to binary on the fly as you transmit!
Neat, huh?

but convert to binary on the fly as you transmit!
Neat, huh?

Binary, hexadecimal, or decimal are just ways to represent the values so they make sense to the user. The data is stored internally in binary, so the "convert to binary" step is not needed.

The data is stored internally in binary, so the "convert to binary" step is not needed.

Yes, obviously, but 0101 (the decimal constant 65 expressed in octal) is not the same as 0b0101 (the decimal constant 5 expressed in binary).
The OP wants a convenient visual representation of a bit pattern, in an environment (Processing) that doesn't offer a binary representation, but has (presumably) masses of storage,
So, solution, write the binary values in octal, purely for visual representation and legibility, but convert to equivalent binary (by shifting three places instead of one for each octal digit) at run-time as they are transmitted.

(sheesh, some people)

Well, I got the OP started on this, so I suppose I have to finish it, too :wink:

OK, a litle of fooling around in Processing reveals the unbinary() procedure. It takes a string and converts it to int

byte charA[] = { byte(unbinary("01100011")) } ;
println(charA[0]);

This complete Processing program will print 99 on the output screen

So to make it a little more readable, and because memory is cheap & plentiful in the Processing program, I suggest that the source array is a lot of strings, and when you output it to the Arduino then use the unbinary(). Something like

String[] charA = {  // character set for the letter 'A'
  "00011000",  // [0] row one
  "00100100",  // [1] row two
  "01000010",  // [2] row three
  "01111110",  // [3] row four
  "01000010",  // [4] row five
  "01000010",  // [5] row six
  "01000010",  // [6] row seven
  "00000000" } ; // [7] last row

// When outputting
for (int i=0; i<8; i++)
// Serial.write(byte(unbinary(charA[i]))) ;
println(byte(unbinary(charA[i]))) ;