Hi everyone!
I'm trying to use Serial, Serial1 and Serial2 on mega.
When i just use serial and serial1 there is no problem but when i put serial2 with this, all my delays are down. The problem is the same if i use Serial and Serial2 if i put Serial1 delays are down again.
I tried to use serialevent but the problem is the still the same.
The error happended on serial1.read() or serial2.read(), the function is in my loop and it broke down all the delay (in the setup and in the loop).
Maybe this attitude is normal, so i need a solution to replace my delay() with another function but which one?
Thank you for your time and help.
all my delays are down
Whatever that means.
Code. Post it.
The normal answer to replacing delay() is to use millis() as a timer. See the BlinkWithoutDelay example in the IDE, but what do you mean that delay() is "down" ?
I meant delay(100000) run in 2 secs and delay(2000) are like 1ms.
if(0 < Serial2.available())
{
tem = Serial2.read();
bufferWrite[buffW] = (char)tem;
Serial.print(bufferWrite[buffW]);
if(WriteX+1 > 13)
{
WriteX = 0;
if(WriteY+1 < 3)
{
WriteY ++;
lcd.LCD_set_XY(WriteX, WriteY);
}
}
else
{
WriteX++;
lcd.LCD_set_XY(WriteX, WriteY);
}
lcd.LCD_write_char(bufferWrite[buffW], MENU_NORMAL);
buffW ++;
}
if(0 < Serial1.available())
{
tem = Serial1.read();
bufferRead[buffR] = (char)tem;
Serial.print(bufferRead[buffR]);
if(ReadX+1 > 13)
{
ReadX = 0;
if(ReadY+1 < 3)
{
ReadY ++;
lcd.LCD_set_XY(ReadX, ReadY);
}
}
else
{
ReadX++;
lcd.LCD_set_XY(ReadX, ReadY);
}
lcd.LCD_write_char(bufferRead[buffR], MENU_NORMAL);
buffR ++;
}[/table]
When we say
Post code
, we usually mean "all of it, inside code tags", because, of course, there are no delays in the snippet you posted.
I wish you will something!
SerialAnalyzerLCD4884.ino (12.7 KB)
#define BUFF_MAX_SIZE 5000
char bufferRead[BUFF_MAX_SIZE];/*data read on serial 2*/
char bufferWrite[BUFF_MAX_SIZE];/*data write on serial 1*/
10,000 bytes of your 8,096 bytes gone right there.
Psst, 8192, but the sentiment is the same.
What is the problem with the buffer size?
What is the problem with the buffer size?
You have 8K of SRAM on the Mega. That space needs to include the stack, the heap, all variables, and all string literals in the code. Using two arrays that take more memory than you have is a problem, you know. There is no room for any stack (so you can't call functions) or heap (so no local variables). There is no room left for libraries to allocate arrays, either, like HardwareSerial does.
Thank you for your answer about the buffer size. It solve the problem. Can you explain me why my error with the buffer size kill delay?
Because if you read or write to memory you don't own, all bets about proper functioning of any given sketch are off.