LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

How to merge processing code for HMC5583L and MMA7361C

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!

0 Kudos
Message 1 of 32
(9,901 Views)

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);

     }

   }

  }

0 Kudos
Message 2 of 32
(4,952 Views)

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.

0 Kudos
Message 3 of 32
(4,952 Views)

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.

LabView magnetometer_accelerometer.png

0 Kudos
Message 4 of 32
(4,952 Views)

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.

0 Kudos
Message 5 of 32
(4,952 Views)

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));

}

0 Kudos
Message 6 of 32
(4,952 Views)

HMC5883L magnetometer.pngr

0 Kudos
Message 7 of 32
(4,952 Views)

You can't simply paste code into LIFA. You must integrate it into the LIFA architecture by creating custom command.

0 Kudos
Message 8 of 32
(4,952 Views)

These codes are working and giving accurate results when used individually.

0 Kudos
Message 9 of 32
(4,952 Views)

Yes because they are designed to work with a serial monitor, not LIFA.

Message 10 of 32
(4,952 Views)