Arduino Mega 2560 Project

Hey there,

I am looking for some guidance on my Arduino project. I'll try and explain it as best possible.

I have two Arduino Mega 2560.
Arduino Mega 2560 1 has a TFT LCD Mega Shield v2.2, and a TFT_320QVT LCD Touch Screen. I have had this screen running with no dramas.
Arduino Mega 2560 2 is in a plastic enclosure, and has multiple different reading devices attached (DHT22 Module, Temperature Probe, MQ2 Module, Photo Resistor, Relay Board Module).

I have had Mega 2560 2 working correctly, outputting through Serial to a nearby tablet, reading to GoBetwino, and saving to the tablet. But after having multiple issues with the tablet, I have decided to try and make it read to the LCD Screen.

After 4 or so days of spending more than half the day looking up different ways such as using Serial1 (this worked best), Wire etc (it could be my lack of knowledge of how to implement what I read into my project, or maybe I haven't found the right thing yet), I at max only had it somewhat working (I could send commands from the LCD on Arduino Mega 2560 1 to Arduino Mega 2560 2, but could never have it respond). I am hard wiring the 2 together through Serial 1. (I will maybe look into wireless in the future).

My goal is to be able to display live readings from Mega 2560 2 on Mega 2560 1, and be able to control the relays attached to Mega 2560 2 on Mega 2560 1.

How can I best achieve my desired results?
If you have any questions, please ask!

I am hard wiring the 2 together through Serial 1

So TX to RX on one and RX to TX on the other AND ground on Arduino 1 to ground on Arduino 2?

If so then it is your software and you are going to have to post your code.

Try and make the two codes as simple as possible so we don't have to wade through all the other stuff.

Grumpy_Mike:
So TX to RX on one and RX to TX on the other AND ground on Arduino 1 to ground on Arduino 2?

If so then it is your software and you are going to have to post your code.

Try and make the two codes as simple as possible so we don't have to wade through all the other stuff.

Hey Grumpy_Mike,

Yes, they are currently connected through Tx1 to Rx1, Rx1 to Tx1, I did not realize I had to have the two grounded together, so will do this, although I undoubtedly know it is my code that is the issue.

I have just been working on making it as clean as possible for reading, so will have a post ready shortly.

Thank you!

This is some of what I have already have on the Arduino Mega 2560 1 (The LCD Screen).

String mystring = "";
char character;
int NEW_READING_AVAIL = 0;
String NEW_READING_DATA;

void setup(){
  Serial.begin(9600);
  Serial1.begin(19200);
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_HI);
  myButtons.setTextFont(SmallFont);
  myButtons.setSymbolFont(Dingbats1_XL);
  NEW_READING_AVAIL=0;
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  PAGE_MAIN();
}

void loop(){
  SERIAL_UPDATE();  
}

void SERIAL_UPDATE(){
  if(Serial1.available()){
    character=Serial1.read();
    //Serial.print("Character: ");
    //Serial.println(character);
    mystring.concat(character);
    //Serial.print("Data: ");
    //Serial.println(mystring);
    process(mystring);
  }
 
}

void process(String DATA) {
  if(DATA.startsWith("@")&&DATA.endsWith("#")){
    DATA.remove(0,1);
    DATA.remove(DATA.indexOf("#",1));
    if(DATA.substring(0,3)=="RRR"){
      DATA.remove(0,3);
      NEW_READING_DATA=DATA;
      NEW_READING_AVAIL=1;
      mystring="";
    }
    
  }
}

void PAGE_ROOMREADINGS(){
  myButtons.deleteAllButtons();
  myGLCD.clrScr();
  int butBack, pressed_button;
  myGLCD.setColor(255, 0, 0); 
  myGLCD.fillRect(0, 0, 319, 20); 
  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(255, 0, 0);
  myGLCD.print("*** ControlBot V4.2.0 ***", CENTER, 3);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.print("ROOM READINGS", CENTER, 30);
  myGLCD.print("Temperature: ", 10, 50);
  myGLCD.print("Humidity: ", 10, 70);
  myGLCD.print("Res Temperature: ", 10, 90);
  myGLCD.print("Light: ", 10, 110);
  butBack = myButtons.addButton( 10, 190, 300,  30, "Back");
  myButtons.drawButtons();
  
  while(1){
    Serial1.write("@RRR#"); 
    SERIAL_UPDATE();
    if(NEW_READING_AVAIL==1){
      myGLCD.print("DATA",10,130);
      int i=0;
      NEW_READING_DATA.toCharArray(CHAR_ARRAY,20);
      char *p = CHAR_ARRAY;
      char *str;
      while ((str = strtok_r(p, ",", &p)) != NULL){
        switch (i){
        case 0: 
          myGLCD.print(str, 110, 50);
          i++;
          break;
        case 1:
         myGLCD.print(str, 85, 70);
          i++;
          break;
        case 2:
           myGLCD.print(str, 140, 90);
          i++;
          break;
        case 3:
           myGLCD.print(str, 65, 110);
          i++;
          break;
        }
       
      }
      NEW_READING_AVAIL=0;
    }
    if(myTouch.dataAvailable()==true){
      pressed_button=myButtons.checkButtons();
      if(pressed_button==butBack){
        PAGE_READINGS();
      }
    }
  }
}

In the infinite while loop, it was supposed to write through Serial1 to Arduino Mega 2560 2 "@RRR#", which the Mega 2 would match to certain component readings, and send back the appropriate data. Instead, the Arduino 2 read out random gibberish characters. I tinkered with the baud rates on both arduino and I got the Mega 2 finally receiving "@RRR#", but then as soon as I opened the Serial monitor on Mega 1 to see if it was receiving any response, the Mega 2 Serial started reading gibberish .

I am cleaning up the code on the Mega 2560 2 for posting.

I did not realize I had to have the two grounded together, so will do this,

OK try that before posting any more code.

Grumpy_Mike,

I have just tested.
The data is successfully sent from Mega 1 to Mega 2, for a little while.
For about 20 messages, the data displays on Mega 2 Serial:
Character: @
Data: @
Character: R
Data: @R
Character: R
Data: @RR
Character: R
Data: @RRR
Character: #
Data: @RRR#

But after that the data turns to:
Character: @
Data: @
Character: R
Data: @R
Character: @
Data: @R@
Character: R
Data: @R@R
Character: #
Data: @R@R#
Character: R
Data: R
Character: R
Data: RR
Character: @
Data: RR@
Character: #
Data: RR@#
Character: R
Data: RR@#R
Character: R
Data: RR@#RR
Character: R
Data: RR@#RRR
Character: @
Data: RR@#RRR@
Character: #
Data: RR@#RRR@#
Character: R
Data: RR@#RRR@#R

Possibly it is receiving information too fast?
Also, when I open Mega 1 Serial, the characters no longer turn to gibberish on Mega 2 Serial (possibly the grounding?)

Code from the Arduino Mega 2.

char SERIAL_CHARACTER;
String SERIAL_DATA;
String SERIAL_CONTENT;

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

void loop() {    
    SERIAL_UPDATE();
}

void PROCESS(String DATA){
  if(DATA.startsWith("@")&&DATA.endsWith("#")){
    
    DATA.remove(0,1);
    DATA.remove(DATA.indexOf("#",1));
    
    if(DATA.substring(0,3)=="RRR"){
      Serial.println(DATA);
      SERIAL_CONTENT="@RRR,";
      SERIAL_CONTENT+=itoa(GET_EXTERNAL_TEMP(),buf,10);
      SERIAL_CONTENT+=",";
      SERIAL_CONTENT+=itoa(GET_EXTERNAL_HUMIDITY(),buf,10);
      SERIAL_CONTENT+=",";
      SERIAL_CONTENT+=itoa(GET_WATER_TEMP(),buf,10);
      SERIAL_CONTENT+=",";
      SERIAL_CONTENT+=itoa(GET_LUX_EXTERNAL(),buf,10);
      SERIAL_CONTENT+="#";
      Serial1.write(SERIAL_CONTENT.c_str());
      SERIAL_DATA="";
    }else{
      SERIAL_DATA=""; 
    }
  }
}

void SERIAL_UPDATE(){
  if(Serial1.available()){
    SERIAL_CHARACTER=Serial1.read();
    Serial.print("Character: ");
    Serial.println(SERIAL_CHARACTER);
    SERIAL_DATA.concat(SERIAL_CHARACTER);
    Serial.print("Data: ");
    Serial.println(SERIAL_DATA);
    PROCESS(SERIAL_DATA);
  }
}

I believe this is all the relevant code... forgive me if I have missed something.

I added a delay after Serial1.write("@RRR#"); which seems to have stopped the data from straying, but having a delay when it will be expecting response data is not ideal... can anyone help me with a different approach?

  1. increase the baud rate in the debug to at least the rate of the interconnections.
  2. Decrease the baud rate in your sending Arduino

Grumpy_Mike:

  1. increase the baud rate in the debug to at least the rate of the interconnections.
  2. Decrease the baud rate in your sending Arduino

Sorry, I'm not quite up to speed with the references! Do you mean increase the baud rate in Serial monitor to match Serial1 baud rate? I dropped the baud in the sending Arduino to 9600 then 4800 but all I got was garbled characters.

Hi,
Just a question, are these two boards side by side, or separated by some significant distance?
The reason I ask is, if they are side by side, can one mega do all the work of sensing and display?

Tom..... :slight_smile:

TomGeorge:
Hi,
Just a question, are these two boards side by side, or separated by some significant distance?
The reason I ask is, if they are side by side, can one mega do all the work of sensing and display?

Tom..... :slight_smile:

Hey Tom,

They unfortunately need to be apart, as the Mega that does the sensing goes in a room that cannot be accessed during certain periods (due to light restrictions, which having the light of the screen in the room would also cause issues).

Thank you!

Just reversed the sender to receiver and receiver to sender, and had Mega 2 (Serial 9600, Serial1 19200) sending data to Mega 1 (Serial 9600, Serial1 19200) , and it is sending/ receiving data correctly... Yet they don't seem to respond to one another.

Do you mean increase the baud rate in Serial monitor to match Serial1 baud rate?

No.

You are talking between two Arduino and from the sound of it over a long distance so that baud rate (Serial1) needs to be slow say 1200 baud on BOTH arduinos.

Then you are talking to the serial monitor over USB with debug, that must be as fast as possible say 19200 baud or faster. You must change the baud rate on the serial monitor to match this, use the drop down menu in the lower right hand corner of the window.

Grumpy_Mike:
No.

You are talking between two Arduino and from the sound of it over a long distance so that baud rate (Serial1) needs to be slow say 1200 baud on BOTH arduinos.

Then you are talking to the serial monitor over USB with debug, that must be as fast as possible say 19200 baud or faster. You must change the baud rate on the serial monitor to match this, use the drop down menu in the lower right hand corner of the window.

I have made these changes, but it has made no difference.
Half hour or so ago, it somehow started working, as I clicked into the page, I sent the request from Mega 1, the Mega 2 then responded, and the screen was populated with readings exactly as intended, but then it just stopped again... I have no idea how/ why it worked at that time, but I'v been trying to recreate it since.

EDIT: I have just found out that to achieve the result mentioned above, I have to have the Serial monitor open for Mega 1???

If it used to work and now doesn't then it is a hardware problem as software is the same every time on a system like the Arduino.

Try the test using short wires ( about 1ft ) for all three connections.

Grumpy_Mike:
If it used to work and now doesn't then it is a hardware problem as software is the same every time on a system like the Arduino.

Try the test using short wires ( about 1ft ) for all three connections.

I have shortened the cable to no more than 1 foot long. Further more to discovering Mega 1 was displaying data when Serial Monitor was open, it now seems to be displaying data even when it's not open (how it's supposed to be). I did have to move the request out of the while(1) loop, so now it only requests the data once when the page is opened.

I have added in the use of millis() to refresh the request, so it is now displaying fresh data every 1000ms.

One thing I am still having trouble with, when ever I find the data stops refreshing, I open up the serial monitor for Mega 2, and I find my incoming data string is clogged with garbled data. How can I best sort my incoming data so garbled data is discarded?

Another thing, as I was typing this and playing around with the screen on mega 1 seeing where it errors, I found it stopped refreshing data again (pinpointed this event to when I upload to Mega 2), i reset mega 2 a few times, reset mega 1 a few times but could not get it to display data again, but as soon as I hit serial monitor for mega 1, it starts displaying again... can anyone explain this??

Hi,
Just a thought, have you tried twisting the tx/rx pair, say a turn every 1cm or so?
Or use some twisted pair out of a CAT5 cable.

Tom... :slight_smile:

TomGeorge:
Hi,
Just a thought, have you tried twisting the tx/rx pair, say a turn every 1cm or so?
Or use some twisted pair out of a CAT5 cable.

Tom... :slight_smile:

Sorry Tom, do excuse my ignorance but I have not heard of this before, can you explain what this achieves?

Thank you!