Hi everyone,
I'm working on a university final project and I'm trying to read values from two pressure sensors via two serial ports, one being a Software Serial instance on pins 8 and 9, and the other being the Hardware Serial on pins RX1 and TX0. My board is an Arduino Pro Micro.
With great help from the people on this forum and Robin2's thread "Serial Input Basics," I have been able to read characters from the sensors, store them in an array, and print them out to the Serial Monitor. But so far I've only been able to do them separately. I can't figure out how to read one array of characters from one sensor, then move on to the second sensor and do the same.
The sensors are named Paro and Honeywell. The Paro is programmed to give data at 10Hz. The Honeywell is programmed to give data at 2Hz. I'm not sure if the large difference in frequency is a problem.
With my code, I get five or six values from the Paro, followed by one value from the Honeywell. I can't figure out read just one value from each sensor and print them one after the next. Basically, I want to fill up one array with characters, print it, and then fill it up again with new characters. But my code seems to run too fast and I have no control over when the array is filled up and printed.
Here is the code I have:
#include <SoftwareSerial.h>
#define honey_RX 8
#define honey_TX 9
SoftwareSerial honeySerial(honey_RX, honey_TX); //Honeywell on pins 8(RX) and 9(TX)
const byte numChars_Honey = 12; //Honeywell data is 12 characters long
const byte numChars_Paro = 13; //Paro data is 13 characters long
char honeyChars[numChars_Honey];
char paroChars[numChars_Paro];
boolean newHoneyData = false;
boolean newParoData = false;
void setup(){
Serial.begin(9600); //with computer
Serial1.begin(9600); //with Paro on HardwareSerial
honeySerial.begin(9600); //with Honeywell on SoftwareSerial
}
void loop(){
readParo();
if(newParoData == true){
Serial.println(paroChars);
newParoData = false;
}
readHoney();
if(newHoneyData == true){
Serial.println(honeyChars);
newHoneyData = false;
}
}
void readHoney(){ //read each character from Honeywell and store it in array
static boolean receiveInProgress = false;
static byte index = 0;
char honeyStartMarker = '?';
char honeyEndMarker = '?';
char hc;
while(honeySerial.available() && newHoneyData == false){
hc = honeySerial.read();
if(receiveInProgress == true){
if(hc != honeyEndMarker){
honeyChars[index] = hc;
index++;
if(index >= numChars_Honey){
index = numChars_Honey - 1;
}
}else{
honeyChars[index] = '\0'; //terminate string
receiveInProgress = false;
index = 0;
newHoneyData = true;
}
}else if(hc == honeyStartMarker){
receiveInProgress = true;
}
}
}
void readParo(){ //reads each character from Paro and stores it in array
static boolean receiveInProgress = false;
static byte index = 0;
char paroStartMarker = '*';
char paroEndMarker = '\n';
char pc;
while(Serial1.available() && newParoData == false){
pc = Serial1.read();
if(receiveInProgress == true){
if(pc != paroEndMarker){
paroChars[index] = pc;
index++;
if(index >= numChars_Paro){
index = numChars_Paro - 1;
}
}else{
paroChars[index] = '\0'; //terminate string
receiveInProgress = false;
index = 0;
newParoData = true;
}
}else if(pc == paroStartMarker){
receiveInProgress = true;
}
}
}
And the output is like this:
01CP=0.0003
000114.6621
000114.6619
000114.6619
000114.6621
000114.6619
01CP=0.0003
The values are formatted the correct way. The first and last values are from the Honeywell, while the middle ones are from the Paro. As you can see, there are five Paro data for each one Honeywell data. How can I write code to print one Honeywell data followed by one Paro data, or vice-versa? I am looking for an output like this:
01CP=0.0003
000114.6621
01CP=0.0002
000114.6621