Labview & Arduino

Hi,

I have a functional program running my UNO communicating to Labview via serial communications.

The communications between the UNO and Labview is bidirectional and I’m able to read the temperature data the UNO is logging and upon a button push in Labview it sends back the ASCI character ‘1’ and ‘2’ depending on which button (choice of 2) is pushed. The UNO is then required to send some digital outputs high/low etc…

I can read the characters in the serial monitor on the IDE but have encountered two issues.

  1. The characters ‘1’ and ‘2’ stay on the same line under the same time stamp, I was under the impression that it should start a new line with a new time stamp when the button is pressed next.

  2. The act of receiving a ‘1’ and a ‘2’ should initiate an if/else if condition with actions.

I’ve just taken the piece of code out from the rest to see if that’s enough for some help troubleshooting.

< // Relates to reading character ‘1’ and ‘2’ from Labview to manually control the Arduino
while (Serial.available()>0){
// read the incoming byte
incomingByte = Serial.read();
Serial.println(incomingByte);

// Open the valve manually
if (incomingByte == ‘1’)
{
digitalWrite(relayOpen, HIGH);
digitalWrite(relayClose, LOW);

digitalWrite(ssrDirection, HIGH);

digitalWrite(pulseMotor, HIGH);
}

// Close the valve manually
else if (incomingByte == ‘2’)
{
digitalWrite(relayOpen, LOW);
digitalWrite(relayClose, HIGH);

digitalWrite(ssrDirection, LOW);

digitalWrite(pulseMotor, HIGH);
}
}

Any assistance will be greatly appreciated.

I put your code into a small script with some minor modification and run it on some other board.

void setup()
{
 // put your setup code here, to run once:
 Serial.begin( 9600 );
 while ( !Serial )
 {
   ; // wait for serial port to connect. Needed for native USB port only
 }
 Serial.println( "Type 1 or 2" );
}

void loop()
{
 // put your main code here, to run repeatedly:
 while ( Serial.available() > 0 )
 {
   // read the incoming byte
   byte incomingByte = Serial.read();
   Serial.println( incomingByte );

   // Open the valve manually
   if ( incomingByte == '1' )
   {
     Serial.println( "One" );
     //      digitalWrite( relayOpen, HIGH );
     //      digitalWrite( relayClose, LOW );
     //
     //      digitalWrite( ssrDirection, HIGH );
     //
     //      digitalWrite( pulseMotor, HIGH );
   }

   // Close the valve manually
   else if ( incomingByte == '2' )
   {
     Serial.println( "Two" );
     //      digitalWrite( relayOpen, LOW );
     //      digitalWrite( relayClose, HIGH );
     //
     //      digitalWrite( ssrDirection, LOW );
     //
     //      digitalWrite( pulseMotor, HIGH );
   }
 }
}

Here is the result from the Serial Monitor when I send 1 , 2 and 3

04:51:16.603 -> Type 1 or 2 04:51:18.442 -> 49 04:51:18.442 -> One 04:51:19.191 -> 50 04:51:19.191 -> Two 04:51:19.848 -> 51

Looks OK to me. What did you expect to happen and what happens in your system.

Hi, Thanks for your help.

When I open serial monitor I get the following read out,

21:09:27.072 → 1112122121

All on the same line…

Now that I think of it I have two serial ports open one for the connection between the UNO and Labview and the other for the serial monitor as I can’t have both open at the same time. I am wondering if the serial monitor isn’t getting the whole picture as it’s coming out of the Tx/Rx pins 0 and 1?

I’ve amended my code to:

<

// Relates to reading from Labview to manually control the Arduino
while (Serial.available()>0)
{
// read the incoming byte
byte incomingByte = Serial.read();
Serial.println(incomingByte);

// Open the valve manually
if (incomingByte == ‘1’)
{
Serial.println( “Open” );
digitalWrite(relayOpen, HIGH);
digitalWrite(relayClose, LOW);

digitalWrite(ssrDirection, HIGH);

digitalWrite(pulseMotor, HIGH);
}

// Close the valve manually
else if (incomingByte == ‘2’)
{
Serial.println( “Close” );
digitalWrite(relayOpen, LOW);
digitalWrite(relayClose, HIGH);

digitalWrite(ssrDirection, LOW);

digitalWrite(pulseMotor, HIGH);
}
}

And it still doesn’t print out “Open” or “Close”…

Not sure if you would really want my full code.

Hmm, now that I think of it, Labview is sending an ASCII 1 and 2 so maybe I need to amend what I am reading?

Your output looks like your Labview is actually sending a byte with value 1 not ASCII 1. The ASCII code for '1' is 49. See my previous output.

if( incomingByte == '1' ) compares to the value 49 if( incomingByte == 1 ) compares to the value 1

So ether change LabView to send an ASCII 1 or just change the comparison to 1.

What is strange is that your output in the Serial Monitor does not have linefeeds in them. This should only happen if you use Serial.print instead of Serial.println. This part has nothing to do with what is send by LabView.

I have an Arduino Due and just opened three serial ports. One sends the data to the Serial Monitor, one sends bytes with value 1,2,'1','2', and one receives them. There is a wire between UART2 TX to UART1 RX. Works fine.The UART peripherals send/receive the value in the background, while your software runs. So you should not loose any values.

Thanks for all your help,

I changed the ‘1’ to 1 and ‘2’ to 2 with no change.

In the last ditch attempt,

I’ve added my full code

/*************************************************** 

****************************************************/


#include <Adafruit_MAX31865.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>


// CODE RELATES TO THE LM35 - DS18B20
// Data wire is conntec to the Arduino digital pin 9
//#define ONE_WIRE_BUS 9
// Setup a oneWire instance to communicate with any OneWire devices
//OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
//DallasTemperature sensors(&oneWire);


// CODE RELATES TO THE PT100 - MAX31865
// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 max = Adafruit_MAX31865(10, 11, 12, 13);
// use hardware SPI, just pass in the CS pin
//Adafruit_MAX31865 max = Adafruit_MAX31865(10);
// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000
#define RREF      430.0
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
#define RNOMINAL  100.0


//SSR
int ssrDirection = 9;
int ssrPulse = 8;
// RGB LED 
int ledBlue = 7;
int ledGreen = 6;
int ledRed = 5;
//redLED
int relayOpen = 4; 
//greenLED
int relayClose = 3; 
//yellowLED
int pulseMotor = 2; 
// delay used throughout the program
int delayPeriod = 50;
// time motor will be on to close ball valve
int motorPeriodclose = 30;
// time motor will be on to open ball valve
int motorPeriodopen = 20;
// Relates to Serial Read
int incomingByte = 0;


void setup() {

//  sensors.begin();
 
 Serial.begin(115200);
 max.begin(MAX31865_3WIRE);  // set to 2WIRE or 4WIRE as necessary

 pinMode(ssrPulse, OUTPUT);
 pinMode(ssrDirection, OUTPUT);
 pinMode(ledBlue, OUTPUT); //RGB
 pinMode(ledGreen, OUTPUT); //RGB
 pinMode(ledRed, OUTPUT); //RGB
 pinMode(relayOpen, OUTPUT); // redLED
 pinMode(relayClose, OUTPUT); // greenLED
 pinMode(pulseMotor, OUTPUT); // yellowLED
}

void loop() {

// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
//  sensors.requestTemperatures(); 
 
//  Serial.print("A");
//  Serial.print(sensors.getTempCByIndex(0)); 

//  delay(delayPeriod);
 
 uint16_t rtd = max.readRTD();
 float ratio = rtd;
 ratio /= 32768;
 Serial.print("B"); Serial.println(max.temperature(RNOMINAL, RREF));

 // Relates to reading from Labview to manually control the Arduino
 while (Serial.available()>0)
 {
   // read the incoming byte
   byte incomingByte = Serial.read();
   Serial.println(incomingByte);

    // Open the valve manually
   if (incomingByte == 1)
   {
     Serial.println( "Open" );
     digitalWrite(relayOpen, HIGH);
     digitalWrite(relayClose, LOW);
     
     digitalWrite(ssrDirection, HIGH); 
     
     digitalWrite(pulseMotor, HIGH); 
   }
 
   // Close the valve manually
   else if (incomingByte == 2)
   {
     Serial.println( "Close" );
     digitalWrite(relayOpen, LOW);
     digitalWrite(relayClose, HIGH);
     
     digitalWrite(ssrDirection, LOW);
 
     digitalWrite(pulseMotor, HIGH);    
   }
 }  
 
   // If the temperature is less than 26 degrees C
   // SSR_Direction = change direction of relay to close valve
   // SSR_Pulse = pulse motor closed
   // RGB = flash the blue light (too cold)
   // LEDs = flash the yellow LED
   if (max.temperature(100, RREF) < 26)
   {
     digitalWrite(relayOpen, LOW);
     digitalWrite(relayClose, HIGH);
     
     digitalWrite(ssrDirection, LOW);
 
     digitalWrite(pulseMotor, HIGH);
     delay(delayPeriod);
     digitalWrite(pulseMotor, LOW);
     delay(delayPeriod);
 
     digitalWrite(ssrPulse, HIGH);
     delay(motorPeriodclose);
     digitalWrite(ssrPulse, LOW);
     delay(motorPeriodclose);
     
     digitalWrite(ledBlue, HIGH);
     delay(delayPeriod);
     digitalWrite(ledBlue, LOW);
     delay(delayPeriod);    
   }
   
   // If the temperature is less than and including 28 degrees C
   // SSR_Direction = direction of last transition, no effect as pulse is off
   // SSR_Pulse = off
   // RGB = flash the green light (perfect)
   else if (max.temperature (100, RREF) <= 28)
   { 
     digitalWrite(ledGreen, HIGH);
     delay(delayPeriod);
     digitalWrite(ledGreen, LOW);
     delay(delayPeriod);
   }
   
   // If the temperature is above 28 degrees C
   // SSR_Direction = change direction of relay to open valve
   // SSR_Pulse = pulse motor open
   // RGB = flash the red light (too hot)
   // LEDs = flash the yellow LED
   else
   {
     digitalWrite(relayOpen, HIGH);
     digitalWrite(relayClose, LOW);
     
     digitalWrite(ssrDirection, HIGH); 
     
     digitalWrite(pulseMotor, HIGH);
     delay(delayPeriod);
     digitalWrite(pulseMotor, LOW);
     delay(delayPeriod);
 
     digitalWrite(ssrPulse, HIGH);
     delay(motorPeriodopen);
     digitalWrite(ssrPulse, LOW);
     delay(motorPeriodopen);
    
     digitalWrite(ledRed, HIGH);
     delay(delayPeriod);
     digitalWrite(ledRed, LOW);
     delay(delayPeriod);
   }

}

I’m wondering if you could get the same effect of driving some pins high or low when it receives the 1/2?

Thanks in advance…``

Can you do me a favour and modify your post so that your code is shown as a code window? Below your post you should see a More … click on it and choose Modify. You should get a box with your post and some additional icons. The first one </> allows you to mark text as code. Mark the source section of your code and click the icon. It will place some tags around the code and this makes the code appear like in my first reply.

This is just to make sure it looks better for other people to read later. :slight_smile:

Now to your issue.

Am I correct that, you are using the same Serial connection from your PC for the LabView program and for the Serial Monitor? So while you are attaching LabView you don’t see what is going on? Then you switch back to the Serial Monitor but you can no longer send data from LabView?

Did you try to send the 1 by typing it into the Serial Monitor and press Enter. In that case you need to compare to ASCII ‘1’. This would show you the logic is sound (I believe your Arduino code is fine.) but we need to figure out what LabView is sending you.

Thanks for the advice,

I actually use the USB from the Arduino to communicate to Labview and use pins 0 and 1 via a RS-232 port and RS-232 to USB converter cable to communicate to the serial monitor.

Will have to do some fault finding with another PC, and see what Labview is doing...

I reckon that we should call it near enough :-) and perhaps even just play around with the "1" 1 and see how we go...