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
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.
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
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?
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 }
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 !!
Lamya:
I check after, so I can have something to read, isn't it right ?
Do you read your mail before or after you check to see if there is something in the mailbox?
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.
Lamya:
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 !!
48 is the ascii code for 0
49 is the ascii code for 1
Didn't you say you sent a 0 and a 1? Sounds like you got the right thing back to me.
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;
}
}
}