LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Synchronisation with arduino and Labview using visa

Hi,

I wrote a simple VI to send commands to my arduino uno and behave according to them.

My problem is that:arduino seems not to be synchronised with laview.The only value that is read is the first one.I came with this conclusion after trying to change the command to the board.

Here is a quick look at my arduino code:


void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
pinMode(13,OUTPUT);
// Serial.flush();
// prints title with ending line break

}

char data='F';
void loop()
{ delay(50);
// Serial.write('ok');
if (Serial.available() )
{

//Serial.write("A");
data=Serial.read();
}
delay(50);
if (data=='0')
{
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}

if (data=='1')
{
digitalWrite(13,HIGH);
delay(4000);
digitalWrite(13,LOW);
delay(4000);
}


}

 

 

 

Thank you for your attention.

 

 

 

 

 

 

 

 

 

0 Kudos
Message 1 of 8
(5,522 Views)

It looks like your VI is stuck waiting for an 'ok' message from the Arduino in the inner while loop, but the Arduino code never sends it (it has been commented out). So the VI will be waiting forever and not send any commands. You can see exactly what the VI is doing by clicking the "Highlight Execution" button in the toolbar of the Block Diagram (the small lightglobe icon), and then hit the Run button to see what is being executed.

 

The VI itself could definitely use a redesign. A basic state machine would improve it (with states such as 'Wait for OK' and 'Send Command').

 

Also just a tip when posting code - make sure to use the Insert Code option. It makes code much more readable:

 

void setup()
{ //Initialize serial and wait for port to open: Serial.begin(9600); pinMode(13,OUTPUT); // Serial.flush(); // prints title with ending line break }
char data='F';
void loop() {
delay(50); // Serial.write('ok'); if (Serial.available() ) { //Serial.write("A"); data=Serial.read(); }
delay(50);
if (data=='0') { digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000); }
if (data=='1') { digitalWrite(13,HIGH); delay(4000); digitalWrite(13,LOW); delay(4000); } }

 




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
Message 2 of 8
(5,496 Views)

Actually i updated the Vi and the code as follows and i have the same problem.

Any suggestions?

void setup() { 
 //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  pinMode(13,OUTPUT);
 // Serial.flush();
  // prints title with ending line break 
   
} 

char data='F';
void loop() 
{ 
//  Serial.write('ok');
// if (Serial.available()  ) 
 //{
   
  //Serial.write("A");     
data=Serial.read();

 //}
 
delay(50);
if (data=='0')
{
  digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
} 

if (data=='1')
{
  digitalWrite(13,HIGH);
delay(4000);
digitalWrite(13,LOW);
delay(4000);
} 
data='4';

} 
    

 

0 Kudos
Message 3 of 8
(5,420 Views)

There is a tutorial on how to use serial port to communicate with the Arduino Uno.

It is written in C# which you can easily translate to LabVIEW.

http://www.codeproject.com/Articles/473828/Arduino-Csharp-and-Serial-Interface

 

It works. I simplified the C# program for my use.

0 Kudos
Message 4 of 8
(5,405 Views)

If we take a step back, what is the expected behaviour of the Arduino?

 

Looking at the VI and Arduino code I'm guessing it's something like:

  1. User presses Send button on VI with the command character to send in Numeric 3 (a 0 or 1)
  2. The Arduino then turns on the LED for one (or four) seconds, then turns it off for one (or four) seconds
  3. The Arduino then waits for a new command

 

Now what is actually happening is probably more like this:

  1. User presses Send button with command character (say it is 0 in this example)
  2. Arduino receives the '0', and turns the LED on for a second, then off for a second.
  3. The Arduino repeats the above action numerous times, even if the Send button is off in the VI

 

What I think is happening is the VI sends hundreds of commands to the Arduino when you press Send. This is because the loop rate of your while loop is 100Hz (a 10ms delay) when the button is pressed. These messages are only processed one at a time on the Arduino, and requires two seconds to process (or up to eight seconds if the command is '1'). The serial port maintains a buffer of all of these commands, so will take a long time to process them all.

 

The simplest fix here is to set the mechanical action of the Send button to Latch When Released. This means LabVIEW will read the button when it is pressed (true), then automatically reset the button (false). Doing this means only one command will be sent over the serial port for every button press. At the moment even if you quickly set the Send button true, then back to false, the while loop will have iterated at least ten times (assuming you turn the button on and off in 100ms). This in turn sends 10 commands to the Arduino, which will then take 20 seconds to process (one second on, one second off).




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
0 Kudos
Message 5 of 8
(5,391 Views)

That is it.I just had to add a not block after the button set to latch until realease.

Thank you.

0 Kudos
Message 6 of 8
(5,331 Views)

How i can make the VI for this code i m confuse should i use if loop  ?

0 Kudos
Message 7 of 8
(3,485 Views)

Hello,

thank you for the nice question. Actually I am trying to run your code, but it runs for few ms and then the VI stops..why is that?

0 Kudos
Message 8 of 8
(3,100 Views)