Mega2560 How to Define Serial Port2 for output and using Bluetooth input Port0

Hello all,
First about me… I am amazed at the support from this community ! These Arduinos are amazing. I recently took a Micro-controller course, but I’m still a novice. I learned more from this Forum.
Issue.… getting this error… Serial2 was not declared in this scope… and this line is highlighted… if (Serial2.available()) { // check for Serial2
Scope and hardware of project data flow… Four 1K Potentiometers connected to A0-A3 Ports of a Nano. Nano sends byte stream via Bluetooth HC-05 (master) connected to Rx0 Tx1 then to another HC-05 (slave) connected to Tx0 Rx0 of a Mega2560. I also have an Led connected to Mega D11 for troubleshooting. Then Nextion 3.5 connected to Rx2 Tx2 of the Mega powered from a separate USB charger. On the Nextion I have 4 gauges that I want to control with the 4 Pots. Both Arduinos powered from their USB Ports to my PC and all grounds are in common as all components except for the Nextion are on a breadboard which allows me to disconnect the Rx0 Tx0 of the HC-05 wires of the Arduinos for Up-loading Sketches. Both Hc-05 powered 3.3V from their respective Arduinos. Once programming is complete I will separate the Arduinos and their HC-05’s into their respective boxes with dedicated 5v power supplies for each.
Successes…I can brighten / dim the LED on the Mega by turning the Pot connected to the Nano. therefor I know the Pot input, Mapping, Bluetooth communication and LED output are ok. The Nextion 3.5 Touchscreen is connected to Serial port 2 and there is no response on it.
Troubleshooting… with a different sketch, if I connect a Potentiometer to the Mega A0 and the Nextion on Mega Serial Port0, I can see the Gauge I made on the Nextion work as needed. Therefor, Nextion will work if correct data sent it. I also included many Libraries that I saw were being used on other projects, in vain.
I spent many late hours reading through the forum and Googling and hit a wall trying to figure out how to read data on Port0 and write this data to Port2.
Any help would be awesome. Thanks in advance.
Code… This is the Mega2560 (slave) Code that has the Nextion attached to it. I didn’t think the Nano (master) code would be relevant as it seems to be working. I can upload if needed.

/* Sketch for Mega.... Input from Tx0, Rx0 from HC-05. Output to Nexium 3.5" Tx2, Rx2.
*/
#include <Arduino.h>
#include <NexConfig.h>
#include <Nextion.h>
#include <NexHardware.h>
#include <HardwareSerial.h>
#include <EasyTransfer.h>
EasyTransfer ET;
#define PIN Serial2 RX (17);  // atempt to define Serial2. Not sure syntax here ???
#define PIN Serial2 TX (16);


int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
int pot4 = 0;

struct DATA_STRUCTURE {  // these variables store the serial data Bytes from bluetooth
  int pot1;
  int pot2;
  int pot3;
  int pot4;

};
DATA_STRUCTURE myData;  // give name to Data group

void setup() {
  // Setup code here, to run once:


  Serial.begin(38400); // Open serial port and intialize easyTransfer port for Bluetooth
  ET.begin(details(myData), &Serial);
  Serial2.begin(9600); // open Serial 2 Port to nextion

  pinMode(11, OUTPUT); //LED's used to verify that variable signal from Pot being received
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop() {
  // main code here, to run repeatedly:

  if (ET.receiveData()) {   // check to see if Bluetooth data packet in verify checksum

    // LED's used to verify that variable signal from Pot being received
    delay(100);              // any faster results in Led chatter
    analogWrite(11, myData.pot1);
    analogWrite(9, myData.pot2);
    analogWrite(6, myData.pot3);
    analogWrite(3, myData.pot4);

    delay(50);   // allow time between outputs. (not sure if needed ???)

    // Send Data to nextium via Serial2

    if (Serial2.available()) { // check for Serial2
      Serial2.print ("z0.val=");  // for Pot1
      Serial2.print (pot1);
      Serial2.write (0xff);  // always send this three lines (3bytes) after each command sent to the nextion display
      Serial2.write (0xff);
      Serial2.write (0xff);

      Serial2.print ("z1.val=");  // for Pot2
      Serial2.print (pot2);
      Serial2.write (0xff);  // always send this three lines (3bytes) after each command sent to the nextion display
      Serial2.write (0xff);
      Serial2.write (0xff);

      Serial2.print ("z2.val=");  // for Pot3
      Serial2.print (pot3);
      Serial2.write (0xff);  // always send this three lines (3bytes) after each command sent to the nextion display
      Serial2.write (0xff);
      Serial2.write (0xff);

      Serial2.print ("z3.val=");  // for Pot4
      Serial2.print (pot4);
      Serial2.write (0xff);  // always send this three lines (3bytes) after each command sent to the nextion display
      Serial2.write (0xff);
      Serial2.write (0xff);

    }
  }

newtothejob:

#include <HardwareSerial.h>

#define PIN Serial2 RX (17);  // atempt to define Serial2. Not sure syntax here ???
#define PIN Serial2 TX (16);

Get rid of this, just call serial2 serial2, and use it like serial0

The ports are fixed in hardware and you cannot select the pin that is assigned to a port. You have a choice of 4 with one being committed to the debug console and programming. I think the best thing is for to get the Arduino cookbook and while you are waiting on it, read the online documentation about using the additional ports. It is easy and straightforward. In that process you will learn a lot about the Mega. There are a lot of examples on line as well.

Hello and thanks for pointing me in the right direction.l
Cookbook on order.
I Have deleted the code per reply 1 and am getting the error 'Serial2 was not declared in the scope".
How do you declare Serial2 ? Tried to find an example but it seems that this is not necessary for others sketches.
Thanks again.
Error highlights this line......

 #define Serial2 Serial2

This is a Mega. As I said, you use the same code for all serial ports. You don't need any of that define stuff.

void setup() {
Serial.begin(9600);
Serial1.begin(38400);
Serial2.begin(115200);
Serial3.begin(9600);

Serial.print("Hi, Serial, Just Serial here");
Serial1.print("Serial1 here");
Serial2.print('I'm the other one, Serial2");
Serial3.print("Um, last as usual");
}

That is ALL the code you need to start and use four ports on a Mega. I believe your main problem is that you are simply using ancient code, and I think you might have made a mistake ordering a cookbook. People don't buy cookbooks in the 21st century, as they can be relied upon to go out of date before the ink is dry. This might go some way to explain why you don't see computer bookshops like you used to. Places like this forum is where you learn what to do - and it's free..

You need to select "Arduino Mega or Mega 2560" under Tools > Board in the IDE, I would suspect it has gotten changed to another board that lacks the additional hardware serial ports.

Yup that was it !
I realized this after I re-did the Sketch from scratch and it compiled ok and the 'Serial2 not defined' error went away. I had the two sketches open, compared the two and saw that the original had migrated to the Nano Board so the IDE didn't know we are using a Mega. I am using a Nano for my master Arduino and may have mixed things up. Must be newtothejob mistake !
Anyway..... now I'm troubleshooting the Nextion. I'm receiving the analog signal ok from the master Nano to the Mega via bluetooth because I can dim / brighten an LED on the Mega output pin11. I'm printing this data to the Nextion to a Gauge I made and the needle just flickers instead of actually moving.
I'm thinking I may need to somehow hold the value being received from the data transfer via serial0 and then write it to the Nextion via serial2 because in the serial monitor I can see that the voltage from the Pots are not totally steady and this may be causing weird data being printed to the Nextion ID's. Just my thought......
Any ideas here......
Thx

newtothejob:
I realized this after I re-did the Sketch from scratch and it compiled ok and the 'Serial2 not defined' error went away. I had the two sketches open, compared the two and saw that the original had migrated to the Nano Board so the IDE didn't know we are using a Mega. I am using a Nano for my master Arduino and may have mixed things up. Must be newtothejob mistake !

When you start the IDE, it will remember the board setting from when it was closed, so you do need to be careful when working with multiple board types.

Ah makes sense now. thanks for the explanation.

Now this is curious.....
When I print the data of the Struct to the nexton nothing happens on the needle of my gauges. Reminder from above... The struct has 4 Int's from the 4 pots of the master Nano and will brighten / dim LEDS on the slave Mega and I also want to show this on gauges of the Nextion serial2. However, if I print a number from 0-359 it reponds accordingly.
So, can someone give some insight as to how the struct works and do I need to manipulate or prep the data in the struct for the Nextion ?
Here is a snip of the code from above. First case no worky, second case worky.
Case1.... this is what I want to happen but a nogo.
Serial2.print ("z0.val="); // for Pot1
Serial2.print (pot1);
Serial2.write (0xff); // always send this three bytes
Serial2.write (0xff);
Serial2.write (0xff);
Case 2.... for troubleshooting serial2 connection is working but not what I want
Serial2.print ("z1.val="); // for Pot2
Serial2.print (150);
Serial2.write (0xff);
Serial2.write (0xff);
Serial2.write (0xff);

Thanks in advance

int pot1 = 0;
int pot2 = 0;
int pot3 = 0;
int pot4 = 0;

struct DATA_STRUCTURE {  // these variables store the serial data Bytes from bluetooth
  int pot1;
  int pot2;
  int pot3;
  int pot4;

};
DATA_STRUCTURE myData;  // give name to Data group

The data from the struct is myData.pot1, myData.pot2, etc. The variables pot1, pot2, etc, are completely separate from the struct, even though they have the same name.

You are using the struct properly with the analogWrite function:

    // LED's used to verify that variable signal from Pot being received
    delay(100);              // any faster results in Led chatter
    analogWrite(11, myData.pot1);
    analogWrite(9, myData.pot2);
    analogWrite(6, myData.pot3);
    analogWrite(3, myData.pot4);

Wow it works !!! You have no idea how nice it is to pass this step and get on to the next. I spent hours researching and modifying the script and didn't realize that mistake.
thanks so much !

This topic was automatically closed after 72 days. New replies are no longer allowed.