From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino Mega Encoder VISA synchronization in Real Time

Solved!
Go to solution

Hello
I'm using arduino mega and i need to create some control system in Labview in real time.
I'm using a control and simulation loop and VISA to get a signal from arduino. My code in arduino:

 

volatile unsigned int counter = 0;  

void setup() {
  Serial.begin (9600);
 
  pinMode(2, INPUT);           
  pinMode(3, INPUT);           
  
  digitalWrite(2, HIGH);      
  digitalWrite(3, HIGH);       
 
 
  
  attachInterrupt(0, ai0, RISING);

  attachInterrupt(1, ai1, RISING);
}

void loop() {
  // Send the value of counter
  Serial.println (counter);
  delay(10);
}

void ai0() {
  // ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
  // Check pin 3 to determine the direction
  if(digitalRead(3)==LOW) {
    counter++;
  }else{
    counter--;
  }
}

void ai1() {
  // ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
  // Check with pin 2 to determine the direction
  if(digitalRead(2)==LOW) {
    counter--;
  }else{
    counter++;
  }
}


Then i made a sketch. But i can't understand how to get visa read signal and control simulation synchronously. I'm trying to vary delay() in arduino code but the precision is not good. Maybe someone have ideas about this?

Download All
0 Kudos
Message 1 of 7
(3,226 Views)

How can I read VISA signal with program sinusoidal signal in one loop to use them together later??

if I try to read them together, then the SINE signal is displayed in real time, but the VISA signal gives me something wrong with big delays.

But separately in 2 loops all ok.

0 Kudos
Message 2 of 7
(3,192 Views)

This is just a rough approximation, but still a very good one: With a baud-rate of 9600 the sending of 1 byte already takes about 1 ms, and that's without the additional delays from drivers on the PC AND your Arduino board. And you probably want to read more than 1 byte.

 

Regards, Jens

Kudos are welcome...
0 Kudos
Message 3 of 7
(3,186 Views)

OK. When I press 'Simulate acquision timing' in my Simulate sine when it displayes the sine in real time but the data from VISA does not change at all. But if i press 'Run as fast as possible' the data from VISA ok, but the sinus time is very fast. What i need to do?

0 Kudos
Message 4 of 7
(3,181 Views)
Solution
Accepted by topic author Alcamo1925

may be you have to use LIFA Encoder, with the attached file. Try it ..

upload "LIFA_Base.on" to you arduino. then you can get the readings using the attached vi.

Abdallah Farmalawi
Message 5 of 7
(3,130 Views)

Yes,it'sworking in one loop now. Thank you.

But in order to do this it was necessary not to use 2 and3 arduino pin and change version of arduino ide to 1.022

0 Kudos
Message 6 of 7
(3,113 Views)

please see my topic and help me

0 Kudos
Message 7 of 7
(2,747 Views)