Hi all!
This is my first arduino project, so I am still learning everything.
My project requires me to read values from a HMC588L (from adafruit) as fast as possible in a computer.
So, I am using an arduino to receive an I2C signal from the magnetometer and send the values through serial to the computer.
I am currently using a slightly modified version of the code adafruit provided. As far as I understand, the magnetometer can produce more than 100 measurements per second. I can only read about 10-12.
I think that the major choking point is the serial communication. I've been trying to figure out the most efficient way to transfer the data, but everybody is saying different things on forums.
Here is my code right now. I am reading a digital pin, because that is how my synchronization happens with a labview code.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//timing variables
long interval = 50; // interval at which to send the serial signal
long previousMillis = 0; // will store last time LED was updated
//trigger variables
const int triggerPin = 2;
int triggerState = 0; // current state of the trigger
int lastTriggerState = 0; // previous state of the trigger
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
}
void setup(void)
{
// initialize the button pin as a input:
pinMode(triggerPin, INPUT);
//initialize Serial
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
void loop(void)
{
unsigned long currentMillis = millis();
// read the pushbutton input pin:
triggerState = digitalRead(triggerPin);
// compare the triggerState to its previous state
if (triggerState != lastTriggerState) {
// if the state has changed, send the signal
if (triggerState == HIGH) {
Serial.print("triggered ");
Serial.print("t: "); Serial.print(currentMillis); Serial.println(" ");
}
}
lastTriggerState = triggerState;
// check to see if it's time to send the signal; that is, if the
// difference between the current time and last time you sent the signal
// is bigger than the interval.
if(currentMillis - previousMillis > interval) {
// save the last time you sent the signal
previousMillis = currentMillis;
Serial.print("t: "); Serial.print(currentMillis); Serial.print(" ");
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
}
}
I am hoping you guys can help me optimize this code. Thanks!