10-23-2014 01:19 AM
Hi I am using a HMC5583L breakout board along with a logic converter and an MMA7361C breakout board as a magnetometer and accelerometer respectively, connected to an Arduino board. I have already created the labview interface for these 2 sensors to work together. However i have not been able to figure out how to merge the processing code for the 2 sensors to be uploaded into the driver. Any help would be great, please and thank you!
10-23-2014 03:15 AM
For HMC5883L:
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;
int error = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
compass = HMC5883L();
error = compass.SetScale(1.3);
error = compass.SetMeasurementMode(Measurement_Continuous);
}
void loop()
{
byte inChar;
MagnetometerScaled scaled = compass.ReadScaledAxis();
int Xscaled = scaled.XAxis;
int Yscaled = scaled.YAxis;
if(Serial.available() > 0)
{
inChar = Serial.read();
if(inChar=='A')
{
Serial.flush();
Serial.print("x");
Serial.println(Xscaled);
Serial.print("y");
Serial.println(Yscaled);
}
}
}
For MMA7361C:
int xpin = 1;
int ypin = 2;
int zpin = 3;
int x_unit_raw = 0;
int y_unit_raw = 0;
int z_unit_raw = 0;
int x_unit = 0;
int y_unit = 0;
int z_unit = 0;
long xtot = 0;
long ytot = 0;
long ztot = 0;
int xavg = 0;
int yavg = 0;
int zavg = 0;
void setup()
{
Serial.begin(9600);
analogReference(EXTERNAL);
int loops = 0;
while(loops < 500);
{
int xsample = analogRead(xpin);
int ysample = analogRead(ypin);
xtot = xtot + xsample;
ytot = ytot + ysample;
loops++;
}
xavg = xtot / 500;
yavg = ytot / 500;
zavg = (xavg+yavg)/ 2;
}
void loop()
{
byte inChar;
x_unit_raw = analogRead(xpin);
y_unit_raw = analogRead(ypin);
z_unit_raw = analogRead(zpin);
x_unit = x_unit_raw-xavg;
y_unit = y_unit_raw-yavg;
z_unit = z_unit_raw-zavg;
if(Serial.available() > 0)
{
inChar = Serial.read();
if(inChar=='A')
{
Serial.flush();
Serial.print("x");
Serial.println(x_unit);
Serial.print("y");
Serial.println(y_unit);
Serial.print("z");
Serial.println(z_unit);
}
}
}
10-23-2014 08:08 PM
If you have the LabVIEW code for both the sensors being implemented together then you have to make the Arduino code to match that which you are expecting in your LabVIEW code.
If you are not using LIFA or LINX then I wrote a basic Arduino communication setup here.
10-30-2014 04:34 AM
alright thank you!
I combined the labview for magnetometer and accelerometer to work independently of each other whilst using the same intialisation parameters running under the same while loop. Will this design function properly w the merged codes. None of the sensors share any pin on the arduino board. Ive attached an image. Any feedback would be really helpful as this is my first time.
10-30-2014 06:38 PM
You can't use the code that you posted with LIFA. You must use the LIFA firmware to use LIFA. If you want to use LIFA with those sensors, you need to either access the sensors through existing LIFA functions or create custom LIFA functions that run your code.
10-30-2014 10:27 PM
Oh yes im using another set of codes along w the LabVIEWInterface.pde and LabVIEWInterface.h files.
For accelerometer MMA7361L:
#include <Wire.h>
#include <SPI.h>
#include <Servo.h>
#include "LabVIEWInterface.h"
int xpin = 1;
int ypin = 2;
int zpin = 3;
int x_unit = 0;
int y_unit = 0;
int z_unit= 0;
int x_unit_adj = 0; //
int y_unit_adj = 0; //
int z_unit_adj = 0; //
unsigned long millis_readsample = 0;
unsigned long freq_readsample = 500;
long xtot =0; //
long ytot =0; //
long ztot =0; //
int xavg =0; //
int yavg =0; //
int zavg =0; //
void setup()
{
syncLV();
Wire.begin();
analogReference(EXTERNAL);
int loops=0; //
while(loops < 500) //
{
int xsample = analogRead(xpin); //
int ysample = analogRead(ypin); //
xtot = xtot + xsample; //
ytot = ytot + ysample; //
loops++; //
}
xavg = xtot / 500; //
yavg = ytot / 500; //
zavg = (xavg+yavg) / 2; //
millis_readsample = millis() + freq_readsample;
}
void loop()
{
float var;
checkForCommand();
if (millis() >= millis_readsample)
{
x_unit = analogRead(xpin);
y_unit = analogRead(ypin);
z_unit = analogRead(zpin);
x_unit_adj = x_unit-xavg; //
y_unit_adj = y_unit-yavg; //
z_unit_adj = z_unit-zavg; //
serialWriteFloat((float)x_unit_adj);
serialWriteFloat((float)y_unit_adj);
serialWriteFloat((float)z_unit_adj);
millis_readsample += freq_readsample;
}
}
void serialWriteFloat(float arg)
{
byte * data = (byte *) &arg;
Serial.write (data, sizeof (arg));
}
10-30-2014 10:31 PM
r
10-31-2014 11:47 AM
You can't simply paste code into LIFA. You must integrate it into the LIFA architecture by creating custom command.
11-01-2014 02:46 AM
These codes are working and giving accurate results when used individually.
11-01-2014 02:07 PM
Yes because they are designed to work with a serial monitor, not LIFA.