08-05-2021 02:49 AM
Hi, I have some problems with connecting Arduinos in one project. They send information via com-port and labview processing it, now it's two potentiometers. When I connect one it works propertly, but when I connect second it crashes or works on of them... Maybe somebody know how to fix it?
08-05-2021 03:15 AM
How do you have them connected?
Also why are you using baud rate setting to read buffer?
08-05-2021 03:37 AM
They separately connected via usb. Arduinos with each other doesn't connected.
I saw someone connected it this way... How can I do it properly?
08-05-2021 06:16 AM
@vits1223 wrote:
I saw someone connected it this way... How can I do it properly?
That would depend on how your Arduinos are sending the data. Can you share some code showing how the data is being sent?
08-06-2021 03:17 AM
Yeah, sure.
1-st Arduino (potentiometer):
--------------------------
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(4800); // запускаем монитор порта
pinMode(A1, INPUT); // к входу A1 подключаем потенциометр
}
void loop() {
int val = analogRead(A1); // считываем данные с порта A1
lcd.setCursor(0, 0);
lcd.print(val);
Serial.println(val); // выводим данные на монитор порта
delay(10); // ставим задержку для удобства
}
--------------------------
Second Arduino read and send frequency
--------------------------
#include <LiquidCrystal.h> // add the necessary library
LiquidCrystal lcd (7, 6, 5, 4, 3, 2); // (RS, E, DB4, DB5, DB6, DB7)
#include <FreqMeasure.h>
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
FreqMeasure.begin();
}
double sum = 0;
int count = 0;
void loop() {
if (FreqMeasure.available()) {
// average several reading together
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
int frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
lcd.setCursor(0, 0);
lcd.print(frequency);
lcd.print(" Hz");
}
}
}
--------------------------
08-06-2021 03:35 AM
I don't know your VISA settings because you only provided block diagram image, but your baud controls are both named as 9600, while one of your arduinos uses 4800 and the other 9600.
08-06-2021 04:02 AM
I'm choosing mannually baud rate. Maybe you have some working example with two or more arduinos?
08-06-2021 04:48 AM
Reading two devices in one loop this way is problematic.
Unless two devices are trigged by the same event (or are sharing the same clock, which is unlikely (but maybe not impossible) with two arduinos) the two devices are asynchronic devices. The times their data is pushed will eventually start to shift phase.
After hours, maybe even days, one device's data will not be read fast enough (as the read waits for the other device). It will return old data, until eventually the serial buffer overflows.
It's asking for problems, IMHO.
Read in two loops, or pull data by doing a write and read (requesting the data).
08-06-2021 06:36 AM
Good, you are using the Serial.Println. So this function appends a Line Feed (I don't remember if a Carriage Return is also appended). This is what we call a Termination Character. Even better, it is the default from the VISA Configure Serial Port. So all you need to do is wire up the correct baud rates for each port. Every other setting should work. Now when you read, you just simple tell the VISA Read to read more bytes than you ever expect in a message. In this exact case, I would probably use 50 (no way a single value should be printing 50 characters). You do this since the VISA Read will stop reading when any of the following occur: 1) the desired number of bytes are read, 2) the termination character is read, 3) the read times out. So by setting the bytes to read to be more than a single message should ever be, we are then relying on the termination character to stop the read. This ensures you are getting a complete message.
I would also follow the advice of making a loop for each Arduino. I almost always put streams each in their own loop. This way, if one goes down it won't mess up the other.
08-06-2021 07:41 AM
This is a stretch, but here it goes...
I had cheap Arduinos come in with the wrong firmware. The details elude me, but they where identified as a different type. Everything worked fine, except that the CPU speed was 50% off. That included serial speed. IIRC, it was a quick fix, but without the fix I had to double all baud rates, waits, etc..