03-26-2015 06:19 PM
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.
03-26-2015
08:52 PM
- last edited on
05-13-2025
02:31 PM
by
Content Cleaner
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); } }
03-29-2015 08:33 AM
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'; }
03-29-2015 04:00 PM - edited 03-29-2015 04:01 PM
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.
03-29-2015 09:08 PM
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:
Now what is actually happening is probably more like this:
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).
04-04-2015 10:29 AM
That is it.I just had to add a not block after the button set to latch until realease.
Thank you.
09-05-2019 09:17 AM
How i can make the VI for this code i m confuse should i use if loop ?
08-30-2020 05:41 AM
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?