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
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 !!
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;
}
}
}
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.
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.