I am working on an open source project for health care and we are using a radar sensor to detect breathing rates in patients. I am using the custum library by the company's employee and have come across a strange problem. I believe this to be a problem with my code and how I am using their library on my Arduino so I opted to post my problem here as opposed to their site, if that should turn out to not be the case I am happy to move it.
In receive_data() starting on line 52 there is a loop that never closes. In order to debug I added logs which have exposed a strange situation. The if(Serial.available()) statement fails but in the else statement the string is still printed to the console implying that the Serial is available. I am a web dev and this is my first time working with hardware and C++ so any advice at all is appreciated.
snippet of my debug code starting on line 58 of the library in receive_data()
if (Serial.available() > 0){
    Serial.println("Serial available");
    continue;
   } else {
    Serial.println("Serial unavailable try again");
   }
   char c = Serial.read(); // Get one byte from radar
  Â
  Serial.println(c);
I found this by trying to solve another problem which might be possibly related so I will mention it.
The first while loop in this function never ends. At first I assume this was due to the sensor's response not being incorporated into the error catching but after wrapping each while loop in a if(Serial.available()) block they ran better meaning the program got further before an error message. This is more confusing considering the problem I am having now is returning false for the same if statement. This is more than likely due to my not knowing C++ but since it is located in the same place it may be relevant.
Here is the code running on the Arduino Mega 2560
#include <XeThruRadar.h>
XeThruRadar radar;
int error_count = 0;
void setup() {
 radar.init(); //sets Serial
 radar.reset_module(); // clears past data
 radar.load_respiration_app(); // boots sensor
 radar.execute_app(); //starts receiving data
}
void loop() {
Â
 float movement = radar.get_resp_movement();
 Serial.println(movement); //always -99.0 error code because of infinite loop
Â
 //If it returns -99.0, that means it did not receive a proper measurement
 if (movement == -99.0) {
  error_count++;
  if (error_count > 5) {
  Â
   //Reset error counter
   error_count = 0;
  }
 }
 else {
  //Reset error counter
  error_count = 0;
 Â
  //Movement is usually between -1 and 1, so move it to 0 to 1 instead:
  float brightness = movement + 1.0;
  brightness += 1.0;
  brightness = brightness/2.0;
 }
}
The if(Serial.available()) statement fails but in the else statement the string is still printed to the console implying that the Serial is available
Serial.available() checks to see whether any serial data is available to read, not whether it is possible to use the serial interface. Hence it is possible to print the message when Serial.available() returns zero.
If you want to know whether Serial is ready you can use if(Serial)
I think if(Serial) only means something on boards like Leonardo, etc.:
On 32u4 based boards (Leonardo, Yùn, ecc) , if (Serial) indicates wether or not the USB CDC serial connection is open. For all other instances, including if (Serial1) on the Leonardo, this will always returns true.
Thanks for the link to the library. That makes it really easy to see what's going on.
The while(1) loops do have an exit. It's the break; statement that lets you get out of the loop. The continue; statements are also important - they skip the rest of the loop and go back to the top.
That library will basically wait forever until it sees the right combination of start and end characters. It's good to demonstrate that your Arduino can talk to the radar but completely unsuitable for a real program.
@UKHeliBob thank you for that distinction it is very helpful. Is the a consensus on whether if (Serial.available() > 0) or if(Serial) is the proper code for an Arduino Mega 2560 as in this case or for Arduinos in general outside of the Leonardo?
In my case I know the Serial is available and want to wait until data is actually being passed. @MorganS you are completely on point, this is a starter library I am still in the prototyping phase. The problem is that the break; statement is never reached because the delimiter is never sent. This was solved initially by adding the if statements but this ended up causing problems later on in the program.
In Javascript my native language I could set up Promises to solve this but so far my research has not found a C++ implementation due to the synchronous nature of the language. All I need to do is have the program wait until data is being received, on initial setup and upon subsequent commands, and then continue running the rest of the code after line 60.
KibaGateaux:
Is the a consensus on whether if (Serial.available() > 0) or if(Serial) is the proper code for an Arduino Mega 2560 as in this case or for Arduinos in general outside of the Leonardo?
if(Serial) is only useful for boards with native serial (ATmega32U4 based boards like Leonardo and Pro Micro, Due, Zero and other SAMD boards). For the Mega 2560 and all other non-ATmega32U4 boards it will always return true no matter what the status of Serial so it's completely useless.
KibaGateaux: @UKHeliBob thank you for that distinction it is very helpful. Is the a consensus on whether if (Serial.available() > 0) or if(Serial) is the proper code for an Arduino Mega 2560 as in this case or for Arduinos in general outside of the Leonardo?
They tell you different things...
if (Serial.available() >0) is true if there is 1 or more character available to read from the serial buffer. This is probably what you want.
if (Serial) is true on all boards without native USB all the time, and true only when the serial connection has been opened on a board with native USB (leonardo/micro/due/zero).
From the snippet you posted that is better formatted...
if (Serial.available() > 0){
 Serial.println("Serial available");
 continue;
}
else
{
 Serial.println("Serial unavailable try again");
}
char c = Serial.read(); // Get one byte from radar
Serial.println(c);
Check is serial is available but read something whatever the state.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
If you are communicating with the Xe Module through pins 0 and 1 of the Arduino, they are the programming pins.
Trying to use three devices of one RS232 link is not possible.
The three devices are, the Arduino, the Xe Module and the PC.
KibaGateaux:
In my case I know the Serial is available and want to wait until data is actually being passed. @MorganS you are completely on point, this is a starter library I am still in the prototyping phase. The problem is that the break; statement is never reached because the delimiter is never sent. This was solved initially by adding the if statements but this ended up causing problems later on in the program.
In Javascript my native language I could set up Promises to solve this but so far my research has not found a C++ implementation due to the synchronous nature of the language. All I need to do is have the program wait until data is being received, on initial setup and upon subsequent commands, and then continue running the rest of the code after line 60.
OK, so the posts between that one and this one should have pointed out that "available" is the wrong word to use. And they might have indicated to you where you went wrong. Your added debug code stops waiting and tries to read a char when there's nothing received and when there is something received ("available") then it continue's back to the top of the loop and ultimately does nothing.
I'm still unclear on your specification....
The library code as presented will wait FOREVER until it gets the right combination of start and end characters. It does not release control back to your code until that occurs. Is this what you want for testing purposes?