How we can read values of two arduinos at the same tim???????Please help!

Hi :stuck_out_tongue:

I have done one tutorial about magnetometer, but this code results only for one sensor.
Now i would like to use this code for two sensors with two diferentes arduinos conecting from the same computer.
How i should change the code to do that?Please help me,i have very difficulties in programming and it is my first time using arduino. :disappointed_relieved:
The code that i used its here:

#include <Wire.h>

#include <HMC5883L.h>

#include <SoftwareSerial.h>

// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portone(10,11);

// software serial #2: TX = digital pin 8, RX = digital pin 9
SoftwareSerial porttwo(8,9);


HMC5883L compass;

int error = 0;

void setup()
{
 
 Serial.begin(9600);
 Serial.println("Starting the I2C interface.");
 Wire.begin(); // Start the I2C interface.
 Serial.println("Constructing new HMC5883L");
 compass = HMC5883L(); // Construct a new HMC5883 compass.
 Serial.println("Setting scale to +/- 1.3 Ga");
 error = compass.SetScale(1.3); // Set the scale of the compass.
 if(error != 0) // If there is an error, print it out.
   Serial.println(compass.GetErrorText(error));
   Serial.println("Setting measurement mode to continous.");
 error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement  mode to Continuous
 if(error != 0) // If there is an error, print it out.
   Serial.println(compass.GetErrorText(error));
}

void loop()
{
 
 MagnetometerRaw raw = compass.ReadRawAxis();
 
 MagnetometerScaled scaled = compass.ReadScaledAxis();

 int MilliGauss_OnThe_XAxis = scaled.XAxis;// 

 float heading = atan2(scaled.YAxis, scaled.XAxis);
 
 float declinationAngle = 0.0457;
 heading += declinationAngle;
 // Correct for when signs are reversed.
 if(heading < 0)
   heading += 2*PI;
   // Check for wrap due to addition of declination.
 if(heading > 2*PI)
   heading -= 2*PI;
  // Convert radians to degrees for readability.
 float headingDegrees = heading * 180/M_PI; 
 // Output the data via the serial port.
 Output(raw, scaled, heading, headingDegrees);

}
// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float  headingDegrees)
{
  Serial.print("Raw:\t");
  Serial.print(raw.XAxis);
  Serial.print("   ");   
  Serial.print(raw.YAxis);
  Serial.print("   ");   
  Serial.print(raw.ZAxis);
  Serial.print("   \tScaled:\t");
  //
  Serial.print(scaled.XAxis);
  Serial.print("   ");   
  Serial.print(scaled.YAxis);
  Serial.print("   ");   
  Serial.print(scaled.ZAxis);
  //
  Serial.print("   \tHeading:\t");
  Serial.print(heading);
  Serial.print(" Radians   \t");
  Serial.print(headingDegrees);
  Serial.println(" Degrees   \t");
  
  delay(100);
  
}

Thank you for helping me.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I'm not familiar with Wire but I think it's only possible to connect to one device from an Arduino and I presume that's why you want two Arduinos to communicate with the PC.

I presume (I haven't yet tried it) that if you connect 2 Arduino's to one PC they will show up as two different serial connections - such as TTYUSB0 and TTYUSB1.

I also presume that the Arduino IDE can only communicate with one Arduino and may be confused if you have two of them on one computer.

However it would be pretty simple to write a program on the PC in Ruby (my preference) or Python that would read the serial data from the two Arduinos and allow you to do whatever you want with it.

...R

BiaM.:
Now i would like to use this code for two sensors with two diferentes arduinos conecting from the same computer.

That part is easy - you just upload the sketch onto two Arduinos and connect them both to USB ports on your computer.

The requirement to have both Arduinos output results at the same time is where the problem comes. What exactly do you mean by 'at the same time'? Is it enough to have both Arduinos outputting their measurements at regular intervals dependently from each other, or do they need to be synchronised? If so, how accurately do they need to be synchronised, and do they need to be synchronised with some external thing, or only with each other?

What are the SoftwareSerial instances created for? They are never used.

What is on the other end of the serial port? If you have the Serial Monitor receiving the data, there is no way that it can collect data from two Arduinos at the same time. If you have some other application listening to the serial port, it simply needs to listen to two serial ports.

Topics and replies merged.

@OP: DO NOT CROSS-POST, IT WASTES TIME.