0

I'm having trouble completing this code for my university final project. I feel like I'm almost there, but I can't seem to find the final buttons to press. I'm using the Arduino Pro Micro.

I have one static pressure transducer connected to the hardware serial pins, RX1 and TX0. I have one differential pressure transducer connected to digital pins 10 (Rx) and 16 (TX), on which I have implemented SoftwareSerial. I'm reading the output data from each of these transducers. Which means technically I don't need to connect the TX pins, because I'm not writing anything to them. They are already programmed to give a continuous serial data stream.

With lots of great help from StackExchange and the Arduino Forums, I'm able to write code to check for a start-marker and end-marker from the two serial ports. I'm storing each character in an array for each of the transducers.

Here is the code I'm using for the static pressure transducer:

const byte numChars = 16;
char paroChars[numChars];     //array to store received data
boolean newParoData = false;

void setup(){
  Serial.begin(9600);     //with computer
  Serial1.begin(9600);     //with Paro transducer
}

void loop(){
  readParo();      //read from Paro and store values in paroChars
  if(newParoData == true){
    Serial.println(paroChars);      //print Paro data with a new line for each value
    newParoData = false;
  }
}

void readParo(){      //reads each character from Paro and stores it in array
  static boolean receiveInProgress = false;
  static byte index = 0;
  char startMarker = '*';
  char endMarker = '\n';
  char pd;
  while(Serial1.available() && newParoData == false){
    pd = Serial1.read();
    if(receiveInProgress == true){
      if(pd != endMarker){
        paroChars[index] = pd;
        index++;
        if(index >= numChars){
          index = numChars - 1;
        }
      }else{
        paroChars[index] = '\0';      //terminate string
        receiveInProgress = false;
        index = 0;
        newParoData = true;
      }
    }else if(pd == startMarker){
      receiveInProgress = true;
    }
  }
}

I do get the correct output on the Serial Monitor from the static pressure transducer when I upload this program to the Arduino:

000114.2927
000114.2928
000114.2927
000114.2929

Similar code goes for the differential pressure transducer:

#include <SoftwareSerial.h>
#define honeywell_RX 10
#define honeywell_TX 16

SoftwareSerial honeySerial(honeywell_RX, honeywell_TX);

const byte numChars = 16;
char honeyChars[numChars];     //array to store received data
boolean newHoneyData = false;

void setup(){
  Serial.begin(9600);     //with computer
  honeySerial.begin(9600);      //with Honeywell transducer
}

void loop(){
  readHoney();      //read from Honeywell and store values in honeyChars
  if(newHoneyData == true){
    Serial.println(honeyChars);    //print Honeywell data with a new line for each value
    newHoneyData = false;
  }
}

void readHoney(){     //read each character from Honeywell and store it in array
  static boolean honeyReceiveInProgress = false;
  static byte index = 0;
  char honeyStartMarker = '?';
  char honeyEndMarker = '?';
  char hd;
  while(honeySerial.available() && newHoneyData == false){
    hd = honeySerial.read();
    if(honeyReceiveInProgress == true){
      if(hd != honeyEndMarker){
        honeyChars[index] = hd;
        index++;
        if(index >= numChars){
          index = numChars - 1;
        }
      }else{
        honeyChars[index] = '\0';      //terminate string
        honeyReceiveInProgress = false;
        index = 0;
        newHoneyData = true;
      }
    }else if(hd == honeyStartMarker){
      honeyReceiveInProgress = true;
    }
  }
}

And this is the output I get on the Serial Monitor for the differential pressure transducer, which is also correct:

01CP=0.0000
01CP=0.0001
01CP=-0.0001
01CP=0.0000

My problem is combining these two programs. Basically, I want to be able to read from the Hardware Serial port first, make sure that the data is valid, store it, then exit the function. After that, read from the SoftwareSerial port, make sure the data is valid, store it, and exit the function. After this I would print both arrays to the Serial Monitor. I don't know how to read from the Serial ports only until the array is filled up with valid characters, then exit. If I combine these two programs, the output is distorted, meaning the values are either bunched together, or there are random characters showing.

I found that these two pressure transducers operate at different frequencies. The static transducer gives serial output at 10Hz, while the differential transducer is about 2Hz. I'm not sure if that factors into the problem.

Basically, I want to get one reading like 000114.2927, then another like 01CP=0.0001, then print both of these to the Serial Monitor. I can't figure out how and where to break out of reading from the serial ports.

Thanks!

  • 2
    read from the Hardware Serial,validate, store, exit function .... read from the Software Serial, validate, store, exit function .... this may cause data loss ...... in loop() check for new data from both devices ... store data and raise flag if data is available ... process data if available – jsotola May 22 '18 at 4:58
  • What exactly is the problem? You didn't include the combined code, so we cannot help you with this. Go the way, that is described in the answers of your other questions. Read every value from the serials, but only send every 5th value for one of it. The other can be send everytime a complete value was received – chrisl May 22 '18 at 12:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.