Arrays

Hello everybody,

I want to save a 7*24 array by entering the data manually using (Serial.read) function, but every time I try the code with displaying the array just to make sure I get numbers different than the numbers I entered.

array[i][j]=Serial.read();
if (Serial.available() > 0)
      { do something }

I tried declaring the array as boolean, byte, and integer

P.S. the numbers should be either 0 or 1

You can't populate an entire array from Serial.read with one operation...

Define the array, build a loop-inside-a-loop to go over all its indexes, and for each "cell" in the array wait until a value is available, then read it in.

Same for output.

P.S. the numbers should be either 0 or 1

But mostly, they'll be -1 or zero

Try here

yes I wrote it in for loops

for(i=0;i<7;i++)
for(j=0;j<24;j++)
{
    array[i][j]=Serial.read();
    if (Serial.available() > 0)
        { do something }
}

but it's still not working, and why will it be 0 and -1 ? I need it to be only 0s and 1s

It'll be mostly -1, because that's what "Serial.read" returns when there's nothing there to read, which is most of the time.
This will explain more

but if there is something to read it weill read it correctly, right ?

and how can I make it work? it doesn't read the numbers I enter !

but if there is something to read it weill read it correctly, right ?

Oh, yes, absolutely.

it doesn't read the numbers I enter !

Maybe they haven't arrived by the time you've finished reading them.

What is the "it" that doesn't work?

The trick is to only read things when there's something there to read, not (as you have it) read something and then check to see if there's something there

Something like this?

Tested. Works. Unstated lessons involved.

// put title, date and author here
// event-driven collection and display for public use, 5/1/15, by GoForSmoke

const byte dim1 = 3, dim2 = 4;
char array[ dim1 ][ dim2 ];
byte i, j;

void setup() 
{
  Serial.begin( 115200 );
  Serial.print( "\n Enter " );
  Serial.print( dim1 );
  Serial.print( " lines x " );
  Serial.print( dim2 );
  Serial.println( " chars\n" );
}

void loop() 
{
  if ( Serial.available())
  {
    array[ i ][ j++ ] = Serial.read();
    
    if ( j >= dim2 )
    {
      j = 0;
      i++;
    }
  }
  
  if ( i >= dim1 )
  {
    Serial.println( );
    for ( i = 0; i < dim1; i++ )
    {
      for ( j = 0; j < dim2; j++ )
      {
        Serial.print( array[ i ][ j ] );
      }
      Serial.println( );
    }
    
    i = j = 0;
  }
}

Of course, rather than 0 or 1, that's more likely to fill the array with 48 or 49 ;), or indeed, and value above 32.

Something like this?

yes exactly, although I still don't know why my code didn't work, but thanks a lot :wink:

AWOL:
Of course, rather than 0 or 1, that's more likely to fill the array with 48 or 49 ;), or indeed, and value above 32.

yes, when I try my code I get numbers of 48, 49 and 10 .... why ?? :frowning:

yes, when I try my code I get numbers of 48, 49 and 10 .... why ??

What are the decimal ASCII codes for 0, 1 and linefeed I wonder ....

I still don't know why my code didn't work,

What code would that be?

Delta_G:
Why are you checking for Serial.available AFTER you try to read from Serial? Wouldn't it make more sense to check if there is something to read BEFORE you try to read it?

I check after, so I can have something to read, isn't it right ?

AWOL:
What code would that be?

This code:

array[i][j]=Serial.read();
if (Serial.available() > 0)
      { do something }

I check after, so I can have something to read, isn't it right ?

So you empty the mailbox, and then see if there's a letter in it.

You're still not getting the hint about snippets.
Ho-hum.

So you empty the mailbox, and then see if there's a letter in it.

but the error is not there, it worked but not correctly, when I declare the array as char it gives the correct numbers with other printing problems, but when I declare it as boolean or integer it gives 48 and 49 !!

but the error is not there

Well, we still can't see your code, so we'll just have to take your word for it.

No, it's not right. Serial.available tells you if there is something in the serial buffer to be read. Serial.read takes that something out of the serial buffer and returns it, or returns -1 if nothing is there. You need to know if something is there to be read before you try to read it.

ah, I got you ... ok I fixed it, but the char problem is still there, I need it to be int or boolean

Didn't you say you sent a 0 and a 1? Sounds like you got the right thing back to me.

but I don't want it to be in ASKII code, I want it 0 and 1

Well, we still can't see your code, so we'll just have to take your word for it.

sorry, here it is

// saving and printing a 7 days schedule
//(24 hours a day)

const byte dim1 = 7, dim2 = 24;
float array[ dim1 ][ dim2 ];
byte i, j;
int time=12;
String clock="PM";
int day=1;

void setup() 
{
  Serial.begin( 115200 );
  Serial.print( "\n Enter " );
  Serial.print( dim1 );
  Serial.print( " lines x " );
  Serial.print( dim2 );
  Serial.println( " chars\n" );
}

void loop() 
{
  if ( Serial.available())
  {
    array[ i ][ j++ ] = Serial.read();
    
    if ( j >= dim2 )
    {
      j = 0;
      i++;
    }
  }
  
  if ( i == dim1 )
  {
    i=0;
    //Serial.println( );
    for ( day = 1; day <= dim1; day++ )
    {
      Serial.print("Day ");
      Serial.print(day);
      Serial.println(": ");
      //day=day+1;
      clock="PM";
      i++;
      for ( j = 0; j < dim2; j++ )
      {
        Serial.print("\t");
        Serial.print("at ");
        Serial.print(time);
        Serial.print(" ");
        Serial.print(clock);
        if (time<10)
        Serial.print("\t\t");
        else    
        Serial.print("\t");
        Serial.println( array[ i ][ j ] );
        time = time + 1;
        if (time == 12)
       {
        if (clock == "PM")
        clock="AM";
        if (clock == "AM")
        clock == "PM";
       }
       if (time == 13)
       time = 1;
       //Serial.println( );
      }
    
    i = j = 20;
    }
  }
}

but I don't want it to be in ASKII code, I want it 0 and 1

So subtract '0' (or 48) from what you get or save 0 or 1 in the first place instead of '0' or '1'

You'll have to convert it. The easy way for single digits is to just subtract from it the ascii code for 0.

Thanks, now i get it. I will try to apply it in my code.

Make sure that the data you convert ( by sutracting '0' ) is a digit.

if ( data >= '0' && data <= '9' )
{
// process as a digit and you can turn digit sequences into values for ints, longs, etc.
}
else
{
// save/return value accumulated and set state to look for the next entry.
}

Always code to handle input errors and possible exceptions to your logic.
If you leave a hole you chance a crash or worse, bad output.

PS -- go to and bookmark -- http://www.asciitable.com/

Type char variable is signed 8 bit integer. If the value is 65, print will show the letter A.
You can address the char array by index or pointer or string.h function.

But that said, what gets sent over serial does not have to be text. It can be binary values that read directly into the bytes of variables. The free terminal emulator PuTTY and Windows Hyperterminal let you send files to the Arduino. The files can be binary data output from some PC program.
Usually I send text because it has a kind of built-in error check and I can read it, but sending binary values is immensely faster and easier. There's a check called CRC where you unsigned add every byte read to a CRC byte and if it rolls over, it rolls over. At the end of the transfer, the number you should have ended up with is sent, otherwise you need to resend for the data. That lets you be more sure the transmission was received correctly, serial has no guarantees. When text gets screwed up, you can see it.