I'm trying to read some data from labview (they're integers separated by a comma) however when i put a wait in the while loop with a time less than 100 ms the indicators keep flashing and Im getting ceros.
This is my arduino code:
#include <OneWire.h>
#include <DallasTemperature.h>
#define Pin 2
int ecg = 0;
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);
{
delay(1000);
Serial.begin(9600);
sensors.begin();
}
void loop()
{
int corazon= analogRead(0);
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0));
Serial.print(",");
Serial.println(corazon);
delay(1);
}
First of all, you have two loops running in parallel and in each loop you are reading from the same serial port. Reading data from the port removes it from the serial input buffer, so each character that arrives in the buffer will only be read by one loop or the other, not both. There isn't any synchronisation between the loops so it'll be random which character goes where. Each loop will read an incomplete message most of the time, so your code can't decode it to a number, so it returns a zero.
Second, as explained in my answer to your last question, don't use Bytes at Port
, use the read termination character setting when you configure the port. That way each Serial Read
will return one complete message. If you do this, you don't need a wait function in the loop - it will run only as fast as it needs to to keep up with the incoming data.
It says here that Serial.println
sends a carriage return then a newline at the end of the data, so you want to use newline (decimal 10) as the termination character. This is actually the default anyway, so as soon as you remove the duplicate loop and the Bytes at Port
it'll probably start working.
In your example code I can't see any reason for the two parallel loops, but if you really need to make the data from the Arduino available in two different loops in LabVIEW, use a communication technique such as a queue or notifier to pass it from one to the other. There are examples and help on these in LabVIEW.
By the way, there's no need for the first two frames of the sequence structure in your loop - the code will execute in that sequence anyway because of dataflow.