i am trying to see if I can output serial data in a 2d arrray. I already made a function which was able to print out single bytes, but it had trouble with a string of bytes.
In the forum i found an topic from Robin2 about how to serial print a string of bytes and I tried to implement it, but i dont get the result i expect to have.
I tried at first with this code
void printArray ( const int [][ 3 ] ); // prototype
const int rows = 3;
const int columns = 6;
char array1[ rows ][ columns ] ;
int incomingByte = 0; // incoming byte for serial read
char row = 0;
char column = 0;
boolean newData = false;
void recvWithEndMarker() {
char endMarker = '\n';
while (Serial.available() > 0 && newData == false) {
incomingByte = Serial.read();
if (incomingByte >= 32 ) {// SAVE if it is a PRINTABLE char
array1[ row ][ column ] = incomingByte;
column++;
if (column >= columns) {
column = columns - 1;
}
}
else {
array1[ row ][ column ] = '\0'; // terminate the string
column = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(array1[ row ][ column ]);
newData = false; }
}
void setup () {
Serial.begin(9600);
Serial.println("<Arduino is ready>");}
void loop () {
recvWithEndMarker();
showNewData();
if (row = 3)
{
row =0;
}
else
{
row++;
}
if ( column = 6)
{
column = 0;
}
else
{
column++;
}
}
put it only prints out "This just in ... "
after that i changed this one function the Serialprintln
void showNewData()
{
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(array1);
newData = false; }
}
but it gives an error of 'no matching function for call to 'println([3][6])'
Which why I ask how am I able to show the serial input of a string of bytes in a 2D array if these functions don't work?thanks in advance
if (row = 3)
Start by fixing the obvious problems like the line above
= is for assignment
== is for comparison
UKHeliBob:
if (row = 3)
Start by fixing the obvious problems like the line above
= is for assignment
== is for comparison
I just took those out since I forgot that those were from a previous attempt to do something with 2d arrays. So they were not really relevant codes for this case. But I fixed the obvious issues now and I got this now.
written code
const int rows = 3;
const int columns = 6;
char array1[ rows ][ columns ] ;
int incomingByte = 0; // incoming byte for serial read
boolean newData = false;
void recvWithEndMarker() {
char endMarker = '\n';
static byte row = 0;
static byte column = 0;
while (Serial.available() > 0 && newData == false) {
incomingByte = Serial.read();
if (incomingByte != endMarker ) {// SAVE if it is a PRINTABLE char
array1[ row ][ column ] = incomingByte;
column++;
if (column >= columns) {
row++;
}
}
else {
array1[ row ][ column ] = '\0'; // terminate the string
column = 0;
newData = true;
}
}
}
void showNewData()
{
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(array1[ row ][ column ]);
newData = false; }
}
void setup () {
Serial.begin(9600);
Serial.println("<Arduino is ready>");}
void loop () {
recvWithEndMarker();
showNewData();
}
when I type in hello I get as a result This just in ...h . Also when i take out [ row ][ column ] in the showNewData, then i get the error I previously mentioned in my post
no matching function for call to 'println([3][6])
. I was thinking in the first scenario I specified array1[ row ][ column ] so
it might only this the byte from that specific coordinate, but how do i get back the other bytes when I type a message like hello in the serial monitor?
The code in your latest post does not compile
row and column are out of scope in showNewData()
UKHeliBob:
The code in your latest post does not compile
row and column are out of scope in showNewData()
Sorry for my late reply, yes I saw that didnt compile. And i saw that i didn't copy it over what was written there in my sketch.
in showNewData I wrote rows and columns
void showNewData()
{
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(array1[ rows ][ columns ]);
newData = false; }
}
But I tested it and this was the one that compiles. But nothing comes out of it or rather some weird symbol gybberish(see attachment).
So i wanted to see if what I did was that i tried to print the byte from rows and colums( both were specified as 3 and 6). So I changed the code to
Serial.println(array1[ 1 ][ 1 ]);
I only typed in one byte the letter h to see if that gets printed, but even that doesn't work.
There is quite a bit wrong with your program.
For instance each row of the array is not terminated with a '\0' so you can't print a single row and even if you could then
Serial.println(array1[ rows ][ columns ]);
would only print one byte
Please describe exactly what you are trying to do.
im a bit confused with your question. are you trying to send numbers or words?
if you are sending characters or strings in ascii format then i would recommend using a separator character so the receiving code can look for it to split the incomming string.
if you can post example of what info you are sending i can give code example of the best way to send it.
also does this array need to be stored by the recieving device or do you simply want the recieving device to print to somehow print it to something?