Arduino Due SerialEvent not work in while(true)

String APC220String = "";
boolean APC220readString = false;

String APC220Message ="Study Room Temp : C Humidity : % ";

void serialEvent1(void)
{

while (Serial1.available()) {
char inChar = (char)Serial1.read();
APC220String += inChar;
if (inChar == '\n') {
APC220readString = true;
Serial.flush();
}
}
}

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);

}

void loop()

// UTFT CODE code

{

while(true)

{

// dht22 code
// rtc ds1307 code
// myTOUCH UTFT Touch Panel Code

if (APC220readString) {

Serial.println(APC220String);
// clear the string:
APC220String = "";
APC220readString = false;
}
}
}

This code is not working. please help me. Arduino Due Serial1 interrupt code.

The structure of your loop() is wrong.

SerialEvent(1) MUST be inside loop, you don't need the inner while loop.

Regards,

Graham

The documentation implies that serialEvent() is an interrupt. It isn't.

The main() function which repeatedly calls loop() also looks at Serial.available() and calls serialEvent() after the loop.

Remove the while(true) and it will work.

yes I understand. I tried it happens. I Remove the while(true). but it does not work, touch screen

String APC220String = "";
boolean APC220readString = false;

String APC220Message ="Study Room Temp : C Humidity : % ";

void serialEvent1()
{

while (Serial1.available()) {
char inChar = (char)Serial1.read();
APC220String += inChar;
if (inChar == '\n') {
APC220readString = true;
Serial.flush();
}
}
}

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);

}

void loop()

{

// UTFT CODE code

if (APC220readString) {

Serial.println(APC220String);
// clear the string:
APC220String = "";
APC220readString = false;

}

while(true)

{

// dht22 code
// rtc ds1307 code
// myTOUCH UTFT Touch Panel Code

}
}

thanks for your answer my frineds.

I was changed this code. but not work.

Arduino Due Serial interrupt code.

Does anyone have any information about the serial interrupts?

Do not, under any circumstances, for any reason, at any time, put a basically infinite loop in your loop function. Loop should execute quickly and get to the end. The Arduino core looks like this deep down:

	for (;;)
	{
		loop();
		if (serialEventRun) serialEventRun();
	}

That is, it keeps calling loop for you forever. Also, if needed it'll call serialEventRun() which dispatches those serial events you want. It cannot do this if loop doesn't finish. So, you have two choices. Do what everyone says and refactor your code so that you don't need the infinite loop or put the contents of your serialEvent1() function within your infinite loop. That's technically possible to do and would work. But, it violates the basic way the Arduino sketch is supposed to be set up.

String APC220String = "";
boolean APC220readString = false;

boolean MainMenuTF = true;

String APC220Message ="Study Room Temp : C Humidity : % ";

void serialEvent1()
{

while (Serial1.available()) {
char inChar = (char)Serial1.read();
APC220String += inChar;
if (inChar == '\n') {
APC220readString = true;
Serial.flush();
}
}
}

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);

}

void loop()

{
if (MainMenuTF == true){

// UTFT CODE code
// Menu code and icon

MainMenuTF == false;

}

if (APC220readString) {

Serial.println(APC220String);
// clear the string:
APC220String = "";
APC220readString = false;

}

// dht22 code
// rtc ds1307 code
// myTOUCH UTFT Touch Panel Code

}

thank you collin80!,ghlawrence2000,MorganS worked...

worked...