Strange behavior on digitalRead when using serial1

Hi All,

This is my first post here, and I am stuck for a few days on this problem and I cant find the solution on the web/in the forums.
I am making an device to control inputs in Microsoft Flight simulator and display the simulation variables on the touchscreen. Serial is used to communicate with an C# tool on my PC (not part of this code), and Serial1 is used to communicate to the touchscreen.

Hardware:

  • Arduino Mega (Geekcreit)
  • nextion 7 inch intelligent touchscreen
  • Rotary encoder

Encoder is connected to:
Digitalpin 2 = encoder switch
Digitalpin 3 = Data pin on encoder
Digitalpin 4 = Clock pin on encoder
And of course + to 5v and – to ground.

Screen is connected to
TX1 18 = RX of the screen
RX1 19 = TX of the screen
And of course + to 5v and – to ground.

The following code is a selection of the code to demonstrate the problem. If I run the code and turn the encoder to the right I get random “Left” / “Right” prints in the debug monitor. The same applies for turning left.
If I commend out the method “SentDataToScreen()” the encoder works as expected. So the Serial1.print / Serial1.write looks to interfere with the digitalRead.

int encoderOutA = 4; // CLK pin of Rotary Enocoder
int encoderOutB = 3; // DT pin of Rotary Enocoder
int encoderPush = 2; // sw pin of Rotary Encoder

int buttonStatus = 0;
int presentState = LOW;
int previousState = presentState;


void setup()
{
  Serial.begin (115200);
  Serial.setTimeout(50);
  Serial1.begin(9600);

  //Setup Rotery encoder
  pinMode (encoderOutA, INPUT);
  pinMode (encoderOutB, INPUT);
  pinMode (encoderPush, INPUT_PULLUP);

}

void loop()
{
  ReadDataFromEncoder();
  SentDataToScreen();
}

void ReadDataFromEncoder()
{
  //Rotary
  presentState = digitalRead(encoderOutA);


  if (presentState != previousState)
  {
    if (digitalRead(encoderOutB) != presentState)
    {
      Serial.println("Right");
    }
    else
    {
      Serial.println("Left");
    }
  }
  previousState = presentState;

  //PushButton
  int btn = digitalRead(encoderPush);
  if (btn != buttonStatus)
  {
    if (btn == 0) //Button is pushed
    {
      Serial.println("PushButton");
    }
    buttonStatus = btn;
  }
}

void SentDataToScreen()
{
  Serial1.print("t2.txt=");
  Serial1.print("\"");
  Serial1.print("info");
  Serial1.print("\"");
  Serial1.write(0xff);
  Serial1.write(0xff);
  Serial1.write(0xff);
}

I hope someone can help with this problem
Thanks in advance!

probably because you "saturate" Serial1 by bombarding it with messages at every loop(). At some point the buffer will be full and print becomes blocking, which will impact your ability to read the encoder properly

why don't you just talk to your Nextion only when there is something to say, a bit like you do for the Serial console?

"Speech is silver, but silence is golden"...

Thanks a lot J-M-L that was indeed the problem, I reduced the call for SentDataToScreen() to 10 times per second and now it works like a charm

great

Really you should only send messages when it's necessary. There is no point asking to re-display what's already on the display.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.