Hi everyone, I really need your help. Okay I have several sensor and I need to get them work together on Arduino UNO board although I know Arduino Mega is capable of holding up to 4 rx/tx but I want to test out on UNO board. Okay I've read this article , NewSoftSerial | Arduiniana , it's on the NewSoftwareSerial.h . Actually I'm wondering if my arduino software is equipped with it.
Because right now my code is like this that communicate with ONE sensor only!
/*
Integrating flow meter and color detector together to work together in one arduino board
*/
#include <SoftwareSerial.h> //add the soft serial libray
#define rxpin 2 //set the RX pin to pin 2
#define txpin 3 //set the TX pin to pin 3
SoftwareSerial myserial(rxpin, txpin); //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
myserial.begin(38400); //set baud rate for software serial port to 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
}
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
}
}
What do I need to change to make it work with multiple serial port ? can I just define rx and tx again on other port? or is there specific stuff I have to get the multiple serial communication working?
The SoftwareSerial has a few restrictions, it can only receive one channel at the time. If you want to communicate with the sensors one by one, it is not a problem.
Read the next reference, it explanes the different versions.
Thanks Peter_n for replying. Yup I'm fine to transmit and receive data one at a time, Okay! I will take a look on the reference you gave me. Thanks again.
Hi again, Okay I read up and did the following changes and right now I try to use a push button to switch between 2 serial devices but it didn't work as I expect it would be as only 1 device is active only no matter what I try. So here's the code:
/*
Integrating flow meter and color detector together to work together in one arduino board
*/
#include <SoftwareSerial.h> //add the soft serial libray
#define rxpin 2 //set the RX pin to pin 2
#define txpin 3 //set the TX pin to pin 3
SoftwareSerial myserial(rxpin, txpin); //enable the soft serial port
SoftwareSerial portOne(4,5); // pin 4 RX , pin 5 TX
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
boolean lastButton = LOW;
boolean currentButton = LOW;
int switchPin = 8; // switch pin 8
int ledPin = 13;
void setup(){ //set up the hardware
Serial.begin(38400); //set baud rate for the hardware serial port to 38400
myserial.begin(38400); //set baud rate for software serial port to 38400
portOne.begin(38400); //set baud rate of serial port to 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
pinMode(switchPin,INPUT); // set as input
pinMode(ledPin,OUTPUT); // set as output
digitalWrite(ledPin,LOW); // set as default off
myserial.listen(); // set as active
}
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
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin); //read the pin value
if ( last != current) // if previous reading is not the same as this one
{
delay(5);
current = digitalRead(switchPin); // read the pin value
}
return current;
}
void loop(){ //here we go....
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
portOne.listen();
digitalWrite(ledPin,HIGH);
delay(30);
digitalWrite(ledPin,LOW);
delay(30);
}
else
myserial.listen();
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
}
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
}
}
Plenty. Your layout looks like shit, for starters. Use Tools + Auto Format to layout your code so that it is readable.
Get rid of the useless comments. If the comment is important enough to include, it goes on a line by itself.
Where do you actually read from portOne? Why is the one instance called portOne while the other is called myserial? The names should reflect what is attached to the SoftwareSerial instances. I'm 99.999% certain that you have not attached a myserial to pins 2 and 3.
I also suspect that serialEvent will trip you up by adding characters or changing inputStringComplete when you don't expect it. Personally I would just use if (Serial.available() > 0) to check if data is ready when I am ready to read it.
I also suggest you divide your code into a series of different functions so that each just deals with one small piece of the action. That way it would be much easier to see how different pieces are triggered by different inputs. For example the function to read stuff into mySerial could be designed only to work when a particular switch was pressed - the logic to choose to read that stuff would all be within that function. There would be another function to read and save the button values.
@PaulS Thanks for the advice I would take note in future. Yeah I also thinking of a way to read portOne and deactivating myserial but no matter what I do myserial will always be active because there's one thing I need to note is that both sensor is the same and the command they receive is similiar too. So the only thing I can differentiate is their device number thus, I will continue try it today and see how's my progress and Yup I did connect 2 serial devices for port 2,3 and 4,5.
@Robin2 I understand what you meant. I will take note in future thanks for the advice. I would amend my code today and see how it goes. Thanks for the advice.
Hi everyone, I really need your help. As the topic head stated that's what I'm trying to progresss right now. Okay a brief introduction on what I'm doing, I using 2 embedded system which is equip with uart to allow user to communicate with it. And by default the baud rate is 38400. For each individual set up I'm able to bring it up and get the result I wanted thus I want 2 of the serial communication to work on a single board
I've researched online and seen many forum on this issue but there was no definite answer or rather it didn't work out. I also know that Arduino Mega have 4 rx/tx pin but sadly I don't have the budget :(( So right now I study that SoftwareSerial.h is able to do make digital output pin to become a virtual rx/tx pin and that's what I'm trying to achieve. I also found out that it wouldn't send out data to the 2 device in one row so I don't mind sending the data one by one to the device.
Here's what I've done so far but what I've seen is some weird character and I can't seem to extract out the second device on the board.
Here's what I see on the Serial Monitor, I've used int a and b to let me know that it is looping and running through so I know where to trouble shoot the problem:
Do you have budget for a dual serial processor?
$27 + shipping for a kit with Atmega1284P, dual hardware serial to handle 38400 data rates.
Pretty easy to set up & run in the IDE. http://www.crossroadsfencing.com/BobuinoRev17/
Oh.. I thought it was a different thread because I'm experiencing programming problem while the other is interfacing problem. But anyway Thanks Robin2.
Thanks Trex and crossRoads for replying.
@crossRoads Unfortunately nope. I don't have the resources. is there any alternative method?
@Trex hahaha maybe to a certain extent but anyway is my code written correctly because I can see transmission for both device but I can only receive one device thus it very weird. plus it should send out some value out since is a data logger but it sending out some weird characters.
Okay here's my problem. I'm have 2 embedded system that is equip with UARTs to allow user to configure it to suit each user needs. Up to now, I'm able to interface with one of it individually with arduino UNO board easily but I want to make full use of the board by putting in 2 embedded system inside to save cost and space.
After reading several forums , posts and guide, I understand that it's possible to do with SoftwareSerial.h in Arduino , However, it will send data to the active device and to send to another you have to deactivate the current device and activate the other device vice versa. Thus, I found out the function to do this was YourSerial.listen(); to activate this device and deactivate the rest.
Unfortunately, up till now I haven't got any breakthrough even reading Arduino Cook Book... Here's my code :
#include <SoftwareSerial.h> //add the soft serial libray
#define rxpin 2 //set the RX pin to pin 2
#define txpin 3 //set the TX pin to pin 3
int ledPin = 13;
SoftwareSerial myserial(rxpin, txpin); //enable the soft serial port
SoftwareSerial myserial1(rxpin, txpin);
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
int a = 0;
void setup(){ //set up the hardware
Serial.begin(38400); //set baud rate for the hardware serial port to 38400
myserial.begin(38400); //set baud rate for software serial port to 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
pinMode(ledPin,OUTPUT);
}
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....
Serial.print("a:'");
Serial.print(a);
Serial.println("'");
if(a<100){
myserial.listen();
digitalWrite(ledPin,LOW);
delay(100);
}
else
{
myserial1.listen();
digitalWrite(ledPin,HIGH);
delay(100);
}
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
}
if(a>100){
inputstring="I\r";
sensor_stringcomplete = true;
while (myserial1.available()) { //while a char is holding in the serial buffer
char inchar = (char)myserial1.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
}
delay(200);
}
else{
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
}
delay(200);
}
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
}
a++;
delay(100);
}
and here's what I see when I try to type in command for my second embedded system:
It work finely with the first embedded system that comes running in first.
I've taken a copy of the code from the previous Post and I have been trying to reorganize it. But I can't disentangle all the stuff about reading the different software serial instances.
Can you describe when you want to read which device and what you want to do with the data you get from it?
@Robin2 It's suppose to have a switch so that I can open the data log manually and off it manually by using a button. But that's abit too advance me for now.. So right now I just want to make it in a way that after 100 counts , the current serial device will be switch off and another serial device will be switch on. I need the Serial.available() as my data log will only be activate if I type letter "I" with a carriage return on the serial monitor input bar. So for now I only can get the first one working ,so I'm not sure if I'm typing the code wrongly. As what I know YourSerial.listen() is a very strong and useful function that disable all the other serial device and listen to this serial device you activated.
@Budvar10 I define the rx and tx pin seperately. The 2 serial device uses the same baud rate. I referred to that reference when I was making progress and my code is mostly based on that example as a structure. As you can see down there they used the same baud rate for 2 serial device and different pins which I did the same for myserial is pin 2(rx) and 3(tx) while myserial1 is pin 4(rx) and 5(tx). I'm not sure if what my claimed is right... but if it's wrong could you enlighten me. Thanks I'm still new to programming so pardon me