[text] code

Viewer

  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Servo.h>
  4. #include <HMC5883L.h>
  5. #include "LabVIEWInterface.h" 
  6.  
  7. HMC5883L compass;
  8. int error = 0;
  9.  
  10. unsigned long millis_readsample = 0;    
  11. unsigned long freq_readsample = 100; 
  12.  
  13. /*********************************************************************************
  14.  **  setup()
  15. *********************************************************************************/
  16. void setup()
  17. {  
  18.   // Initialize Serial Port With The Default Baud Rate
  19.   syncLV();
  20.  
  21.   // Place your custom setup code here
  22.   Wire.begin();
  23.   
  24.   compass = HMC5883L(); 
  25.   error = compass.SetScale(1.3); 
  26.   error = compass.SetMeasurementMode(Measurement_Continuous);
  27.   millis_readsample = millis() + freq_readsample;
  28. }
  29.  
  30.  
  31. /*********************************************************************************
  32.  **  loop()
  33. *********************************************************************************/
  34.  
  35. void loop()
  36. {   
  37.   float var;
  38.   
  39.   // Check for commands from LabVIEW and process them.
  40.   checkForCommand();
  41.   
  42.   // Place your custom loop code here (this may slow down communication with LabVIEW)
  43.    if (millis() >= millis_readsample)
  44.   {
  45.     MagnetometerScaled scaled = compass.ReadScaledAxis(); 
  46.     int xScaled = scaled.XAxis;  
  47.     int yScaled = scaled.YAxis;  
  48.        
  49.     serialWriteFloat((float)xScaled);
  50.     serialWriteFloat((float)yScaled);
  51.  
  52.     millis_readsample += freq_readsample;  
  53.   }
  54. }
  55.  
  56. void serialWriteFloat(float arg) 
  57. {
  58.   byte * data = (byte *) &arg; 
  59.   Serial.write (data, sizeof (arg));
  60. }
  61. #[color=#5E6D03]include[/color] <Wire.h>
  62. #[color=#5E6D03]include[/color] <SPI.h>
  63. #[color=#5E6D03]include[/color] <[b][color=#d35400]Servo[/color][/b].h>
  64. #[color=#5E6D03]include[/color] <HMC5883L.h>
  65. #[color=#5E6D03]include[/color] [color=#005C5F]"LabVIEWInterface.h"[/color] 
  66.  
  67. HMC5883L compass;
  68. [color=#00979C]int[/color] [color=#5E6D03]error[/color] = 0;
  69.  
  70. [color=#00979C]unsigned[/color] [color=#00979C]long[/color] millis_readsample = 0;    
  71. [color=#00979C]unsigned[/color] [color=#00979C]long[/color] freq_readsample = 100; 
  72.  
  73. [color=#95a5a6]/*********************************************************************************[/color]
  74. [color=#95a5a6] **  setup()[/color]
  75. [color=#95a5a6]*********************************************************************************/[/color]
  76. [color=#00979C]void[/color] [color=#5E6D03]setup[/color]()
  77. {  
  78.   [color=#434F54]// Initialize Serial Port With The Default Baud Rate[/color]
  79.   syncLV();
  80.  
  81.   [color=#434F54]// Place your custom setup code here[/color]
  82.   Wire.[color=#D35400]begin[/color]();
  83.   
  84.   compass = HMC5883L(); 
  85.   [color=#5E6D03]error[/color] = compass.SetScale(1.3); 
  86.   [color=#5E6D03]error[/color] = compass.SetMeasurementMode(Measurement_Continuous);
  87.   millis_readsample = [color=#D35400]millis[/color]() + freq_readsample;
  88. }
  89.  
  90.  
  91. [color=#95a5a6]/*********************************************************************************[/color]
  92. [color=#95a5a6] **  loop()[/color]
  93. [color=#95a5a6]*********************************************************************************/[/color]
  94.  
  95. [color=#00979C]void[/color] [color=#5E6D03]loop[/color]()
  96. {   
  97.   [color=#00979C]float[/color] var;
  98.   
  99.   [color=#434F54]// Check for commands from LabVIEW and process them.[/color]
  100.   checkForCommand();
  101.   
  102.   [color=#434F54]// Place your custom loop code here (this may slow down communication with LabVIEW)[/color]
  103.    [color=#5E6D03]if[/color] ([color=#D35400]millis[/color]() >= millis_readsample)
  104.   {
  105.     MagnetometerScaled scaled = compass.ReadScaledAxis(); 
  106.     [color=#00979C]int[/color] xScaled = scaled.XAxis;  
  107.     [color=#00979C]int[/color] yScaled = scaled.YAxis;  
  108.        
  109.     serialWriteFloat(([color=#00979C]float[/color])xScaled);
  110.     serialWriteFloat(([color=#00979C]float[/color])yScaled);
  111.  
  112.     millis_readsample += freq_readsample;  
  113.   }
  114. }
  115.  
  116. [color=#00979C]void[/color] serialWriteFloat([color=#00979C]float[/color] arg) 
  117. {
  118.   [color=#00979C]byte[/color] * data = ([color=#00979C]byte[/color] *) &arg; 
  119.   [b][color=#d35400]Serial[/color][/b].[color=#D35400]write[/color] (data, [color=#00979C]sizeof[/color] (arg));
  120. }

Editor

You can edit this paste and save as new:


File Description
  • code
  • Paste Code
  • 24 Jan-2022
  • 4.08 Kb
You can Share it: