Simple accelerometer and magnetometer program example for Hexiwear featuring UART

Dependencies:   FXOS8700

Fork of Hexi_Accelero_Magneto_Example by Hexiwear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FXOS8700.h"
00003 
00004 //  Check out the full featured example application for interfacing to the 
00005 //  Accelerometer/Magnetometer device at the following URL
00006 //  https://developer.mbed.org/users/trm/code/fxos8700cq_example/
00007 
00008 DigitalOut led1(LED_GREEN);
00009 
00010 // Initialize Serial port
00011 Serial pc(USBTX, USBRX);
00012 
00013 // Pin connections & address for Hexiwear
00014 FXOS8700 accel(PTC11, PTC10);
00015 FXOS8700 mag(PTC11, PTC10);
00016 
00017 // main() runs in its own thread in the OS
00018 // (note the calls to Thread::wait below for delays)
00019 int main() {
00020     
00021     // Configure Accelerometer FXOS8700, Magnetometer FXOS8700
00022     accel.accel_config();
00023     mag.mag_config();
00024 
00025     float accel_data[3]; float accel_rms=0.0;
00026     float mag_data[3];   float mag_rms=0.0;
00027 
00028     printf("Begin Data Acquisition from FXOS8700CQ sensor....\r\n\r\n");
00029     wait(0.5);
00030     
00031     while (1) {
00032         led1 = !led1;
00033         // Example data printing
00034       
00035       accel.acquire_accel_data_g(accel_data);
00036       accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
00037       printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",accel_data[0],accel_data[1],accel_data[2],accel_rms);
00038       wait(0.01);
00039       
00040       mag.acquire_mag_data_uT(mag_data);
00041       mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
00042       printf("Magnetometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\n\r",mag_data[0],mag_data[1],mag_data[2],mag_rms);
00043       wait(0.01);
00044         
00045         Thread::wait(500);
00046     }
00047 }