I am working on a university final project and have run into a problem with programming Arduino, after a 3 year hiatus. I have a pressure transducer that is sending pressure data at 10Hz. I have successfully programmed my Arduino Pro Micro to read the data as it is coming in. I'm using the SoftwareSerial library to set digital pins 8 and 9 to RX and TX, respectively. As the pressure transducer is continuously streaming data values, I read each character, store them in an array, and print them to the Serial Monitor. I'm using a start and end-marker to recognize each incoming character and put them into the array. The raw data output from the transducer is in the form:
*000114.2927
*000114.2926
*000114.2927
And this is the code I am using.
#include <SoftwareSerial.h>
#define paroRX 8
#define paroTX 9
SoftwareSerial paroSerial(paroRX, paroTX);
const byte numChars = 16;
char paroChars[numChars]; //array to store received data
char paroToSend[numChars]; //array to transmit
boolean newParoData = false;
void setup(){
Serial.begin(9600); //with transmitter radio
paroSerial.begin(9600);
}
void loop(){
paroSerial.listen();
readParo(); //read from Paro and store values in paroChars
if(newParoData == true){
for(int i=0; i<16; i++){
paroToSend[i] = paroChars[i];
}
Serial.print(paroToSend); Serial.print('\n'); //transmit Paro data with a new line for each value
newParoData = false;
}
}
void readParo(){
static boolean receiveInProgress = false;
static byte index = 0;
char startMarker = '*';
char endMarker = '\n';
char pd;
while(paroSerial.available() && newParoData == false){
pd = paroSerial.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;
}
}
}
And this is the output that I get, which is correct. I don't really want that * in front anyways.
000114.2927
000114.2928
000114.2927
000114.2929
But all of this happens at 10Hz, which means I get a constant stream of readings on the Serial Monitor. I want to slow this down to 1Hz, or just one reading per second. The obvious solution that occurred to me was to put a delay in the loop. So I tried to implement this with delay(1000) and using the millis() function as well, but I get strange readings like:
000114.2927
***000114
**2928
***.000114
000114.2928
How can I read at 1Hz from the 10Hz data stream? I want to just store one data value from the stream per second in the form of 000114.2927, then ignore the rest while the Arduino does something else.
Thanks!
How can I read at 1Hz from the 10Hz data stream?
... discard 9 readings