Hi everyone, I really need your help. Okay here's what I'm trying to do from what I know Arduino Mega have 3 additional serial port , so in total there's 4. It's just nice since I got 4 in my hand right now which I want it to work with Arduino Mega. However, after I do up a simple coding from scratch and verifying it there's error so here's my coding :
//Testing with one more serial devices that shall be written in the comment status
#include <SoftwareSerial.h>
/*
This file is to integrate : pH , tds , blow off and fill in sensor into Arduino Mega
*/
//TDS
#define rxpin 2 //set the RX pin to pin 2
#define txpin 3 //set the TX pin to pin 3
//pH
#define rxpin1 19
#define txpin1 18
//Blow OFF
#define rxpin2 17
#define txpin2 16
//FILL IN
#define rxpin3 15
#define txpin3 14
//ColorDetector Sensor
//#define rxpin4 4
//#define txpin4 5
SoftwareSerial myserial(rxpin,txpin);
//SoftwareSerial myserial4(rxpin4, txpin4); //enable the soft serial port
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete = false; //have we received all the data from the Atlas Scientific product
void setup(){ //set up the hardware
Serial.begin(38400); //set baud rate for the hardware serial port to 38400
//Serial for TDS emulated virtual port
myserial.begin(38400); //set baud rate for software serial port to 38400
//rx and tx for pH pin 19 & 18
Serial1.begin(38400);
//rx and tx for BLOW OFF pin 17 & 16
Serial2.begin(38400);
//rx and tx for Fill IN pin 15 & 14
Serial3.begin(38400);
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
}
void serialEvent() { //if the hardware serial port receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
if(inchar == '\r') {
input_stringcomplete = true;
} //if the incoming character is a <CR>, set the flag
}
void loop(){ //here we go....
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
myserial.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
//FILL IN
while(Serial3.available()){
char inchar = (char)myserial.read(); //get the new char
sensorstring += inchar; //add it to the sensorString
if (inchar == '\r') {
sensor_stringcomplete = true;
}
}
//BLOW OFF
while(Serial2.available()){
char inchar = (char)myserial.read(); //get the new char
sensorstring += inchar; //add it to the sensorString
if (inchar == '\r') {
sensor_stringcomplete = true;
}
}
//pH
while(Serial1.available()){
char inchar = (char)myserial.read(); //get the new char
sensorstring += inchar; //add it to the sensorString
if (inchar == '\r') {
sensor_stringcomplete = true;
}
}
//TDS
while (myserial.available()) { //while a char is holding in the serial buffer
char inchar = (char)myserial.read(); //get the new char
sensorstring += inchar; //add it to the sensorString
if (inchar == '\r') {
sensor_stringcomplete = true;
} //if the incoming character is a <CR>, set the flag
}
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been received in its entirety
Serial.print(sensorstring); //use the hardware serial port to send that data to the PC
sensorstring = ""; //clear the string:
sensor_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
}
}