Integrating multiple arduino codes

hello,
for my graduation project, I am using an Arduino Mega, to monitor the health of an industrial machine. I have found the individual codes I need for each component but when I try to integrate the codes I am having trouble. I just have one question I would like to ask if someone can help me, with each code a different serial.begin() number is used do I choose one or do I keep all and use Serial.end()?

The baudrate setting does not affect the Arduino code. It has to be chosen according to the requirements of the party on the other end of the line.

The Mega has independent hardware UARTs that can work at their own individual speed.

so what should I do, do I not include any serial.begin() ? the user of the code will be me, I will be performing an experiment in the lab using Arduino and I want to use the built-in serial monitor in Arduino.IDE

Use the specific settings with Serial1.begin(), Serial2.begin() and so on. For the Serial Monitor, connected to Serial, chose whatever you like and set the Serial Monitor to the same baudrate.

okay thank you so much I will try it now

You do realize that the numbers refer to different, specific I/O ports on the MCU, right?

no, I did not know, thank you for the remark. I am actually fairly new to using Arduino, can you please tell me what topic exactly do I need to search to understand more?

It's more a question about your project. With those serial references, you must have several serial devices attached. So in answer to the second question, I guess the topic you need to search is the documentation for the device you're building...

Generally, you can not willy nilly combine codes that you don't understand, on a hardware platform you don't understand. You posted no details of either here, so it's also impossible to help you for that reason.

for example, I am using two voltage sensors and a RTC. The voltage sensor requires serial.begin(9600) but the RTC needs serial.begin(115200). so what am I supposed to do here? I tried numbering as DrDiettrich suggested it did not work the serial monitor gives no output.


//=======================RTC global==============================================
// Arduino Mega:
// ----------------------
// DS3231:  SDA pin   -> Arduino Digital 20 (SDA) or the dedicated SDA pin
//          SCL pin   -> Arduino Digital 21 (SCL) or the dedicated SCL pin
#include <DS3231.h>
// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);
//====================end of RTC=============================================

//==================voltage sensor global===========================================
const int voltageSensorPin1 = A2;          // sensor1 pin
const int voltageSensorPin2 = A3;          // sensor2 pin
float vIn1;                                // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vIn2;                                // measured voltage (3.3V = max. 16.5V, 5V = max 25V)
float vOut1;
float vOut2;
float voltageSensorVal1;                   // value on pin A2 (0 - 1023)
float voltageSensorVal2;                   // value on pin A3 (0 - 1023)
const float factor = 5.128;               // reduction factor of the Voltage Sensor shield
const float vCC = 5.00;                   // Arduino input voltage (measurable by voltmeter)
//====================end of voltage=============================================




void setup() {
//========RTC setup===================================================
  // Setup Serial connection
  Serial2.begin(115200);
  // Uncomment the next line if you are using an Arduino Leonardo
  //while (!Serial) {} 
  // Initialize the rtc object
  rtc.begin();
  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(20,19,36);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(22, 3, 2023);   // Set the date to January 1st, 2014
//end of RTC===================

//=======setup voltage===============================================
  Serial1.begin(9600);
//end of voltage============
}




void loop() {
//=======RTC reading=======================================================
// Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
  // Send time
  Serial.print(rtc.getTimeStr());  
//end of RTC==============================

// =======voltage reading===================================================
  voltageSensorVal1 = analogRead(voltageSensorPin1);    // read the current sensor value (0 - 1023)
  voltageSensorVal2 = analogRead(voltageSensorPin2);    // read the current sensor value (0 - 1023) 
  vOut1 = (voltageSensorVal1 / 1024) * vCC;             // convert the value to the real voltage on the analog pin
  vOut2 = (voltageSensorVal2 / 1024) * vCC;             // convert the value to the real voltage on the analog pin
  vIn1 =  vOut1 * factor;                               // convert the voltage on the source by multiplying with the factor
  vIn2 =  vOut2 * factor;                               // convert the voltage on the source by multiplying with the factor
  Serial.print("Voltage sensor 1 = ");             
  Serial.print(vIn1);
  Serial.print("V");
  Serial.print(", Voltage sensor 2 = ");             
  Serial.print(vIn2);
  Serial.println("V");
  delay(1000);
//end of voltage reading==================

}

That is what I mean. You don't understand that different serial instances are used for different physical I/O ports. That is a pretty serious lapse, I doubt that we will be able to help you. You need to go do some serious research on the machines and code you have. You haven't even told us what it is.

Sorry, but it's just not feasible to tutor someone from
totally lost ---->>>> competent embedded systems engineer
in one forum thread...

Also, your specific question was already answered in reply #4.

I'm not saying you can't do it, but you asked for help way too soon. Usually, when someone has these kinds of problems, it means that they haven't followed the normal incremental learning path that is required to attain proficiency in this field. I suggest you do that.

Then, when you have a sketch that is close to working, post it here and ask specific questions about code that you at least partially understand.

I understand thank you, I will look into it more.

Provided that these modules are connected to RX/TX pins then e.g. connect the voltage sensor to RX1/TX1 and replace all Serial references in that project by Serial1. The same for the RTC and Serial2.

Suggestion, in prose.

  1. you have a working test sketch for each device, correct? Modify each one in isolation to use different serial ports on your Mega, while presenting output on Serial Monitor(which is also a serial port on the Mega).
  2. Once you have those 3 working sketches, you may start to merge any two to form a working sketch.
  3. now merge the third one.

If, at any point, you have difficulties with this, report back here on this thread(DO NOT START A NEW ONE). Explain what you've done, what problem you're facing, and present your complete code and any error messages from the compile process.
That's what an organized and thorough approach might look like.
Add a complete schematic of how you have things hooked up, as well.

With so many devices and ports, a system diagram would be enormously helpful as well... oh, wait, you said that...

I think I have a different explanation.

@rwq_01 is not (or was not) planning to use the additional Serial/UART ports of the Mega at all. The connected devices don't use Seria/UART communications. The voltage sensors connect to analog inputs. The RTC connected to i²c bus.

@rwq_01 found separate example sketches for measuring voltage and reading the time from the RTC, and these sketches happened to use different baud rates to communicate with Serial Monitor. @rwq_01 is asking how to combine these sketches, given the different baud rates they use.

If I'm right, there is no "need" or "requirement" to use different baud rates when the sketches are combined. Just pick any baud rate you like, but be sure to set serial monitor to the same rate. The authors, of the sketches you are trying to combine, chose those baud rates because they were the rates they use out of habit or preference, and they could have chosen any other baud rate.

There is no need to use Serial1.begin(), Serial2.begin(). These serial ports will not send messages to the Serial Monitor anyway.

Generally, higher baud rates are better, but as you increase the baud rate higher (in the sketch and set Serial Monitor to match), you may find the data becomes corrupted or not received at all, especially if there are long or poor quality wires between the MCU chip and the USB-serial adapter chip (not a problem with Mega since both chips are on the same PCB). So pick the highest baud rate that you find works reliably.

2 Likes

@paulrb
Nice catch. Yep, we were focused on what the user thought was his problem.

thank you so much, yes this is what I was looking for

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