Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of LED2 by
BAILEY_CWS.cpp
00001 /*Charlie Bailey 00002 Hild Bede*/ 00003 00004 #include "mbed.h" 00005 #include "ADXL362.h" 00006 /* This program fetches samples from the AXDL362 and prints the readings in various formats. */ 00007 00008 /* Serial device declaration */ 00009 Serial pc(USBTX,USBRX); // serial tx, rx 00010 ADXL362 adxl362(p11, p12, p13, p10); /* Accelerometer (mosi, miso, sclk, cs) */ 00011 DigitalOut led1(LED1); /* LED1 */ 00012 DigitalOut led2(LED2); /* LED2 */ 00013 DigitalOut led3(LED3); /* LED3 */ 00014 DigitalOut led4(LED4); /* LED4 */ 00015 00016 /* Main loop */ 00017 int main() 00018 { 00019 00020 float meanx=0, meany=0, meanz=0, stddevx=0, stddevy=0, stddevz=0, sumvarx=0, sumvary=0, sumvarz=0, variancex=0, variancey=0, variancez =0; 00021 float xval[100]; /* array to store readings */ 00022 float yval[100]; 00023 float zval[100]; 00024 int8_t xdata, ydata, zdata; 00025 float max_valx, max_valy, max_valz, min_valx, min_valy, min_valz, sumx, sumy, sumz; 00026 int N=10; 00027 float T=0.1; 00028 float L=60; 00029 int i; 00030 int user_cmd; /* User command is stored in this variable */ 00031 00032 pc.printf("\n\nThis program analyses readings from an accelerometer."); 00033 00034 adxl362.init_spi();/* set up SPI interface */ 00035 adxl362.init_adxl362();/* Set up accelerometer */ 00036 wait(0.1);/* wait 100ms for accelerometer to initialise */ 00037 00038 while(1) 00039 00040 { 00041 00042 /* Print a simple menu */ 00043 pc.printf("\nMenu for anaysing accelerometer readings\n"); 00044 pc.printf("1: Take Readings\n"); 00045 pc.printf("2: Display maximum and minimum values\n"); 00046 pc.printf("3: Display mean values\n"); 00047 pc.printf("4: Display standard deviation of values\n"); 00048 pc.printf("5: Change sample rate \n"); 00049 pc.printf("6: Illuminate LEDs when acceleration exceeds your chosen value \n"); 00050 pc.printf("7: Exit Program\n"); 00051 pc.printf("Command: "); 00052 pc.scanf("%i",&user_cmd); /* Get command from PuTTY terminal */ 00053 00054 /* Process user command */ 00055 switch(user_cmd) 00056 { 00057 case(1): 00058 00059 pc.printf("Chose number of readings: "); 00060 pc.scanf("%i",&N); /*takes user input */ 00061 pc.printf("Taking %i readings...\n",N); 00062 00063 for (i=0;i<100;i=i+1) /* clears all arrays */ 00064 { 00065 xval[i]=0; 00066 yval[i]=0; 00067 zval[i]=0; 00068 } 00069 { 00070 max_valx=-1000; 00071 max_valy=-1000; 00072 max_valz=-1000; 00073 min_valx=1000; 00074 min_valy=1000; 00075 min_valz=1000; 00076 sumx=0; 00077 sumy=0; 00078 sumz=0; 00079 } 00080 for(i=0;i<N;i=i+1) 00081 { 00082 { 00083 adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */ 00084 xval[i]=float(xdata); /*store x reading */ 00085 yval[i]=float(ydata); /*store y reading */ 00086 zval[i]=float(zdata); /*store z reading */ 00087 00088 pc.printf("Reading no. %i\t\t x= %i\t\t y= %i\t\t z= %i\n",i+1 ,xdata, ydata, zdata);/* print X-reading */ 00089 00090 if(max_valx<xval[i])max_valx=xdata;/* update x maximum if necessary */ 00091 if(max_valy<yval[i])max_valy=ydata;/* update y maximum if necessary */ 00092 if(max_valz<zval[i])max_valz=zdata;/* update z maximum if necessary */ 00093 00094 if(min_valx>xval[i])min_valx=xdata;/* update x minimum if necessary */ 00095 if(min_valy>yval[i])min_valy=ydata;/* update y minimum if necessary */ 00096 if(min_valz>zval[i])min_valz=zdata;/* update z minimum if necessary */ 00097 00098 { 00099 sumx = sumx + xval[i]; /* sums all values */ 00100 sumy = sumy + yval[i]; 00101 sumz = sumz + zval[i]; 00102 } 00103 00104 { 00105 meanx = sumx/N; /*calcualtes means of values*/ 00106 meany = sumy/N; 00107 meanz = sumz/N; 00108 } 00109 00110 00111 00112 wait(T);/* wait some period of time before next sample */ 00113 00114 } 00115 } 00116 00117 for(i=0;i<N;i=i+1) /* sums to allow calculation of standard deviation later */ 00118 { 00119 sumvarx = sumvarx + (xval[i]-meanx)*(xval[i]-meanx) ; 00120 sumvary = sumvary + (yval[i]-meany)*(yval[i]-meany) ; 00121 sumvarz = sumvarz + (zval[i]-meanz)*(zval[i]-meanz) ; 00122 } 00123 00124 00125 00126 break; 00127 case(2): 00128 pc.printf("Maximum and minimum values are:\n"); /* prints maximum and minimum x and y values*/ 00129 00130 { 00131 pc.printf("Maximum x reading was %+04f\n",max_valx);/* print max x result */ 00132 pc.printf("Maximum y reading was %+04f\n",max_valy);/* print max y result */ 00133 pc.printf("Maximum z reading was %+04f\n",max_valz);/* print max z result */ 00134 pc.printf("Minimum x reading was %+04f\n",min_valx);/* print min x result */ 00135 pc.printf("Minimum y reading was %+04f\n",min_valy);/* print min y result */ 00136 pc.printf("Minimum z reading was %+04f\n",min_valz);/* print min z result */ 00137 } 00138 break; 00139 00140 case(3): 00141 pc.printf("Mean values are:\n"); /* prints mean values */ 00142 { 00143 pc.printf("Mean of x values was %+04f\n",meanx);/* print mean of x values */ 00144 pc.printf("Mean of y values was %+04f\n",meany);/* print mean of y values */ 00145 pc.printf("Mean of z values was %+04f\n",meanz);/* print mean of z values */ 00146 } 00147 break; 00148 case(4): 00149 pc.printf("Calculating standard deviation...\n"); /* calcuates standard deviation */ 00150 { 00151 variancex = sumvarx/(N-1); 00152 variancey = sumvary/(N-1); 00153 variancez = sumvarz/(N-1); 00154 } 00155 00156 { 00157 stddevx = sqrt( variancex ); 00158 stddevy = sqrt( variancey ); 00159 stddevz = sqrt( variancez ); 00160 } 00161 00162 { 00163 pc.printf("Standard deviation of x values: %+04f\n",stddevx);/* print mean of x values */ 00164 pc.printf("Standard deviation of y values: %+04f\n",stddevy);/* print mean of y values */ 00165 pc.printf("Standard deviation of z values: %+04f\n",stddevz);/* print mean of z values */ 00166 } 00167 00168 break; 00169 00170 case(5): 00171 pc.printf("Choose sample rate (readings/s), T: \n"); /*allows user to change sample rate*/ 00172 pc.scanf("%f",&T); /*takes users input*/ 00173 pc.printf("You chose to take one sample every %f seconds\n",T); 00174 break; 00175 00176 case(6): 00177 pc.printf("Chose threshold value:\n"); /*allows user to change threshold value*/ 00178 pc.scanf("%f",&L); 00179 pc.printf("You chose %f as your threshold acceleration. \n",L); 00180 00181 while (2) 00182 { 00183 adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */ 00184 /* Set LEDs depending upon a threshold value of acceleration */ 00185 if(xdata>=L) 00186 { 00187 led1=1; 00188 } 00189 else 00190 { 00191 led1=0; 00192 } 00193 if(ydata>=L) 00194 { 00195 led2=1; 00196 } 00197 else 00198 { 00199 led2=0; 00200 } 00201 if(zdata>=L) 00202 { 00203 led3=1; 00204 } 00205 else 00206 { 00207 led3=0; 00208 } 00209 wait(T);/* wait some period of time before next sample */ 00210 } 00211 00212 00213 00214 break; 00215 00216 case(7): 00217 00218 pc.printf("Program stopping...\n"); 00219 return(0); /* Return from main, ending the program */ 00220 break; 00221 00222 default: 00223 pc.printf("Error, invalid command...\n"); 00224 } 00225 } 00226 00227 00228 return(0);/* normal exit */ 00229 00230 } 00231 00232 00233 00234 00235
Generated on Tue Jul 12 2022 17:14:16 by
1.7.2
