First draft of C file for Y2 project

Dependencies:   ADXL362 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Add required libraries
00002 #include "mbed.h"
00003 #include "stdio.h"
00004 #include "math.h"
00005 #include "ADXL362.h"
00006 
00007 //Set up serial communication
00008 Serial pc(USBTX, USBRX);
00009 //Set up accelerometer
00010 ADXL362 adxl362(p11, p12, p13, p10);
00011 
00012 //Initialise parameters
00013 int N = 50; //Number of samples
00014 float T = 0.1; //Sample period
00015 
00016 //Main function
00017 int main()
00018 {
00019     //Initialise variable used to exit/enter menu
00020     int exitFlag = 0;
00021     //Initialise variable to switch between menu options
00022     int choice;
00023 
00024     //Initialise accelerometer               
00025     adxl362.init_spi();
00026     adxl362.init_adxl362();
00027     wait(0.1);
00028     
00029     //Initialise variables to hold accelerometer data
00030     int8_t xdata, ydata, zdata;
00031     
00032     //Enter menu (not visible to user)
00033     while(exitFlag == 0) //Will repeat this loop until exitflag set to 1 by user (to end program)
00034     {
00035         //Scans serial connection for command from MATLAB
00036         pc.scanf("%i", &choice);
00037         switch(choice)
00038         {
00039             //Switches here if int value 1 is recieved
00040             case(1):
00041             {
00042                 //For loop runs data collection N times to ensure correct number of samples
00043                 for(int i=0; i<N; i++)
00044                 {
00045                     //Gets accelerometer data
00046                     adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);
00047                     //Sends accelerometer data to MATLAB via serial connection
00048                     pc.printf("%+04d\n", xdata);
00049                     pc.printf("%+04d\n", ydata);
00050                     pc.printf("%+04d\n", zdata);
00051                     //Waits time T to ensure sample rate
00052                     wait(T);
00053                 }
00054             }
00055             //Switches here if something other than menu options recieved from MATLAB   
00056             default:
00057             {
00058                 //Sends error message to MATLAB (will then be relayed to user)
00059                 pc.printf("ERROR");
00060             }
00061         }
00062     }
00063 }
00064 
00065