LED

Dependencies:   mbed

Fork of LED2 by Charlie Bailey

Committer:
cbailey1994
Date:
Thu Oct 29 11:14:56 2015 +0000
Revision:
0:287361f0056d
LED;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cbailey1994 0:287361f0056d 1 /*Charlie Bailey
cbailey1994 0:287361f0056d 2 Hild Bede*/
cbailey1994 0:287361f0056d 3
cbailey1994 0:287361f0056d 4 #include "mbed.h"
cbailey1994 0:287361f0056d 5 #include "ADXL362.h"
cbailey1994 0:287361f0056d 6 /* This program fetches samples from the AXDL362 and prints the readings in various formats. */
cbailey1994 0:287361f0056d 7
cbailey1994 0:287361f0056d 8 /* Serial device declaration */
cbailey1994 0:287361f0056d 9 Serial pc(USBTX,USBRX); // serial tx, rx
cbailey1994 0:287361f0056d 10 ADXL362 adxl362(p11, p12, p13, p10); /* Accelerometer (mosi, miso, sclk, cs) */
cbailey1994 0:287361f0056d 11 DigitalOut led1(LED1); /* LED1 */
cbailey1994 0:287361f0056d 12 DigitalOut led2(LED2); /* LED2 */
cbailey1994 0:287361f0056d 13 DigitalOut led3(LED3); /* LED3 */
cbailey1994 0:287361f0056d 14 DigitalOut led4(LED4); /* LED4 */
cbailey1994 0:287361f0056d 15
cbailey1994 0:287361f0056d 16 /* Main loop */
cbailey1994 0:287361f0056d 17 int main()
cbailey1994 0:287361f0056d 18 {
cbailey1994 0:287361f0056d 19
cbailey1994 0:287361f0056d 20 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;
cbailey1994 0:287361f0056d 21 float xval[100]; /* array to store readings */
cbailey1994 0:287361f0056d 22 float yval[100];
cbailey1994 0:287361f0056d 23 float zval[100];
cbailey1994 0:287361f0056d 24 int8_t xdata, ydata, zdata;
cbailey1994 0:287361f0056d 25 float max_valx, max_valy, max_valz, min_valx, min_valy, min_valz, sumx, sumy, sumz;
cbailey1994 0:287361f0056d 26 int N=10;
cbailey1994 0:287361f0056d 27 float T=0.1;
cbailey1994 0:287361f0056d 28 float L=60;
cbailey1994 0:287361f0056d 29 int i;
cbailey1994 0:287361f0056d 30 int user_cmd; /* User command is stored in this variable */
cbailey1994 0:287361f0056d 31
cbailey1994 0:287361f0056d 32 pc.printf("\n\nThis program analyses readings from an accelerometer.");
cbailey1994 0:287361f0056d 33
cbailey1994 0:287361f0056d 34 adxl362.init_spi();/* set up SPI interface */
cbailey1994 0:287361f0056d 35 adxl362.init_adxl362();/* Set up accelerometer */
cbailey1994 0:287361f0056d 36 wait(0.1);/* wait 100ms for accelerometer to initialise */
cbailey1994 0:287361f0056d 37
cbailey1994 0:287361f0056d 38 while(1)
cbailey1994 0:287361f0056d 39
cbailey1994 0:287361f0056d 40 {
cbailey1994 0:287361f0056d 41
cbailey1994 0:287361f0056d 42 /* Print a simple menu */
cbailey1994 0:287361f0056d 43 pc.printf("\nMenu for anaysing accelerometer readings\n");
cbailey1994 0:287361f0056d 44 pc.printf("1: Take Readings\n");
cbailey1994 0:287361f0056d 45 pc.printf("2: Display maximum and minimum values\n");
cbailey1994 0:287361f0056d 46 pc.printf("3: Display mean values\n");
cbailey1994 0:287361f0056d 47 pc.printf("4: Display standard deviation of values\n");
cbailey1994 0:287361f0056d 48 pc.printf("5: Change sample rate \n");
cbailey1994 0:287361f0056d 49 pc.printf("6: Illuminate LEDs when acceleration exceeds your chosen value \n");
cbailey1994 0:287361f0056d 50 pc.printf("7: Exit Program\n");
cbailey1994 0:287361f0056d 51 pc.printf("Command: ");
cbailey1994 0:287361f0056d 52 pc.scanf("%i",&user_cmd); /* Get command from PuTTY terminal */
cbailey1994 0:287361f0056d 53
cbailey1994 0:287361f0056d 54 /* Process user command */
cbailey1994 0:287361f0056d 55 switch(user_cmd)
cbailey1994 0:287361f0056d 56 {
cbailey1994 0:287361f0056d 57 case(1):
cbailey1994 0:287361f0056d 58
cbailey1994 0:287361f0056d 59 pc.printf("Chose number of readings: ");
cbailey1994 0:287361f0056d 60 pc.scanf("%i",&N); /*takes user input */
cbailey1994 0:287361f0056d 61 pc.printf("Taking %i readings...\n",N);
cbailey1994 0:287361f0056d 62
cbailey1994 0:287361f0056d 63 for (i=0;i<100;i=i+1) /* clears all arrays */
cbailey1994 0:287361f0056d 64 {
cbailey1994 0:287361f0056d 65 xval[i]=0;
cbailey1994 0:287361f0056d 66 yval[i]=0;
cbailey1994 0:287361f0056d 67 zval[i]=0;
cbailey1994 0:287361f0056d 68 }
cbailey1994 0:287361f0056d 69 {
cbailey1994 0:287361f0056d 70 max_valx=-1000;
cbailey1994 0:287361f0056d 71 max_valy=-1000;
cbailey1994 0:287361f0056d 72 max_valz=-1000;
cbailey1994 0:287361f0056d 73 min_valx=1000;
cbailey1994 0:287361f0056d 74 min_valy=1000;
cbailey1994 0:287361f0056d 75 min_valz=1000;
cbailey1994 0:287361f0056d 76 sumx=0;
cbailey1994 0:287361f0056d 77 sumy=0;
cbailey1994 0:287361f0056d 78 sumz=0;
cbailey1994 0:287361f0056d 79 }
cbailey1994 0:287361f0056d 80 for(i=0;i<N;i=i+1)
cbailey1994 0:287361f0056d 81 {
cbailey1994 0:287361f0056d 82 {
cbailey1994 0:287361f0056d 83 adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */
cbailey1994 0:287361f0056d 84 xval[i]=float(xdata); /*store x reading */
cbailey1994 0:287361f0056d 85 yval[i]=float(ydata); /*store y reading */
cbailey1994 0:287361f0056d 86 zval[i]=float(zdata); /*store z reading */
cbailey1994 0:287361f0056d 87
cbailey1994 0:287361f0056d 88 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 */
cbailey1994 0:287361f0056d 89
cbailey1994 0:287361f0056d 90 if(max_valx<xval[i])max_valx=xdata;/* update x maximum if necessary */
cbailey1994 0:287361f0056d 91 if(max_valy<yval[i])max_valy=ydata;/* update y maximum if necessary */
cbailey1994 0:287361f0056d 92 if(max_valz<zval[i])max_valz=zdata;/* update z maximum if necessary */
cbailey1994 0:287361f0056d 93
cbailey1994 0:287361f0056d 94 if(min_valx>xval[i])min_valx=xdata;/* update x minimum if necessary */
cbailey1994 0:287361f0056d 95 if(min_valy>yval[i])min_valy=ydata;/* update y minimum if necessary */
cbailey1994 0:287361f0056d 96 if(min_valz>zval[i])min_valz=zdata;/* update z minimum if necessary */
cbailey1994 0:287361f0056d 97
cbailey1994 0:287361f0056d 98 {
cbailey1994 0:287361f0056d 99 sumx = sumx + xval[i]; /* sums all values */
cbailey1994 0:287361f0056d 100 sumy = sumy + yval[i];
cbailey1994 0:287361f0056d 101 sumz = sumz + zval[i];
cbailey1994 0:287361f0056d 102 }
cbailey1994 0:287361f0056d 103
cbailey1994 0:287361f0056d 104 {
cbailey1994 0:287361f0056d 105 meanx = sumx/N; /*calcualtes means of values*/
cbailey1994 0:287361f0056d 106 meany = sumy/N;
cbailey1994 0:287361f0056d 107 meanz = sumz/N;
cbailey1994 0:287361f0056d 108 }
cbailey1994 0:287361f0056d 109
cbailey1994 0:287361f0056d 110
cbailey1994 0:287361f0056d 111
cbailey1994 0:287361f0056d 112 wait(T);/* wait some period of time before next sample */
cbailey1994 0:287361f0056d 113
cbailey1994 0:287361f0056d 114 }
cbailey1994 0:287361f0056d 115 }
cbailey1994 0:287361f0056d 116
cbailey1994 0:287361f0056d 117 for(i=0;i<N;i=i+1) /* sums to allow calculation of standard deviation later */
cbailey1994 0:287361f0056d 118 {
cbailey1994 0:287361f0056d 119 sumvarx = sumvarx + (xval[i]-meanx)*(xval[i]-meanx) ;
cbailey1994 0:287361f0056d 120 sumvary = sumvary + (yval[i]-meany)*(yval[i]-meany) ;
cbailey1994 0:287361f0056d 121 sumvarz = sumvarz + (zval[i]-meanz)*(zval[i]-meanz) ;
cbailey1994 0:287361f0056d 122 }
cbailey1994 0:287361f0056d 123
cbailey1994 0:287361f0056d 124
cbailey1994 0:287361f0056d 125
cbailey1994 0:287361f0056d 126 break;
cbailey1994 0:287361f0056d 127 case(2):
cbailey1994 0:287361f0056d 128 pc.printf("Maximum and minimum values are:\n"); /* prints maximum and minimum x and y values*/
cbailey1994 0:287361f0056d 129
cbailey1994 0:287361f0056d 130 {
cbailey1994 0:287361f0056d 131 pc.printf("Maximum x reading was %+04f\n",max_valx);/* print max x result */
cbailey1994 0:287361f0056d 132 pc.printf("Maximum y reading was %+04f\n",max_valy);/* print max y result */
cbailey1994 0:287361f0056d 133 pc.printf("Maximum z reading was %+04f\n",max_valz);/* print max z result */
cbailey1994 0:287361f0056d 134 pc.printf("Minimum x reading was %+04f\n",min_valx);/* print min x result */
cbailey1994 0:287361f0056d 135 pc.printf("Minimum y reading was %+04f\n",min_valy);/* print min y result */
cbailey1994 0:287361f0056d 136 pc.printf("Minimum z reading was %+04f\n",min_valz);/* print min z result */
cbailey1994 0:287361f0056d 137 }
cbailey1994 0:287361f0056d 138 break;
cbailey1994 0:287361f0056d 139
cbailey1994 0:287361f0056d 140 case(3):
cbailey1994 0:287361f0056d 141 pc.printf("Mean values are:\n"); /* prints mean values */
cbailey1994 0:287361f0056d 142 {
cbailey1994 0:287361f0056d 143 pc.printf("Mean of x values was %+04f\n",meanx);/* print mean of x values */
cbailey1994 0:287361f0056d 144 pc.printf("Mean of y values was %+04f\n",meany);/* print mean of y values */
cbailey1994 0:287361f0056d 145 pc.printf("Mean of z values was %+04f\n",meanz);/* print mean of z values */
cbailey1994 0:287361f0056d 146 }
cbailey1994 0:287361f0056d 147 break;
cbailey1994 0:287361f0056d 148 case(4):
cbailey1994 0:287361f0056d 149 pc.printf("Calculating standard deviation...\n"); /* calcuates standard deviation */
cbailey1994 0:287361f0056d 150 {
cbailey1994 0:287361f0056d 151 variancex = sumvarx/(N-1);
cbailey1994 0:287361f0056d 152 variancey = sumvary/(N-1);
cbailey1994 0:287361f0056d 153 variancez = sumvarz/(N-1);
cbailey1994 0:287361f0056d 154 }
cbailey1994 0:287361f0056d 155
cbailey1994 0:287361f0056d 156 {
cbailey1994 0:287361f0056d 157 stddevx = sqrt( variancex );
cbailey1994 0:287361f0056d 158 stddevy = sqrt( variancey );
cbailey1994 0:287361f0056d 159 stddevz = sqrt( variancez );
cbailey1994 0:287361f0056d 160 }
cbailey1994 0:287361f0056d 161
cbailey1994 0:287361f0056d 162 {
cbailey1994 0:287361f0056d 163 pc.printf("Standard deviation of x values: %+04f\n",stddevx);/* print mean of x values */
cbailey1994 0:287361f0056d 164 pc.printf("Standard deviation of y values: %+04f\n",stddevy);/* print mean of y values */
cbailey1994 0:287361f0056d 165 pc.printf("Standard deviation of z values: %+04f\n",stddevz);/* print mean of z values */
cbailey1994 0:287361f0056d 166 }
cbailey1994 0:287361f0056d 167
cbailey1994 0:287361f0056d 168 break;
cbailey1994 0:287361f0056d 169
cbailey1994 0:287361f0056d 170 case(5):
cbailey1994 0:287361f0056d 171 pc.printf("Choose sample rate (readings/s), T: \n"); /*allows user to change sample rate*/
cbailey1994 0:287361f0056d 172 pc.scanf("%f",&T); /*takes users input*/
cbailey1994 0:287361f0056d 173 pc.printf("You chose to take one sample every %f seconds\n",T);
cbailey1994 0:287361f0056d 174 break;
cbailey1994 0:287361f0056d 175
cbailey1994 0:287361f0056d 176 case(6):
cbailey1994 0:287361f0056d 177 pc.printf("Chose threshold value:\n"); /*allows user to change threshold value*/
cbailey1994 0:287361f0056d 178 pc.scanf("%f",&L);
cbailey1994 0:287361f0056d 179 pc.printf("You chose %f as your threshold acceleration. \n",L);
cbailey1994 0:287361f0056d 180
cbailey1994 0:287361f0056d 181 while (2)
cbailey1994 0:287361f0056d 182 {
cbailey1994 0:287361f0056d 183 adxl362.ACC_GetXYZ8(&xdata, &ydata, &zdata);/* fetch readings */
cbailey1994 0:287361f0056d 184 /* Set LEDs depending upon a threshold value of acceleration */
cbailey1994 0:287361f0056d 185 if(xdata>=L)
cbailey1994 0:287361f0056d 186 {
cbailey1994 0:287361f0056d 187 led1=1;
cbailey1994 0:287361f0056d 188 }
cbailey1994 0:287361f0056d 189 else
cbailey1994 0:287361f0056d 190 {
cbailey1994 0:287361f0056d 191 led1=0;
cbailey1994 0:287361f0056d 192 }
cbailey1994 0:287361f0056d 193 if(ydata>=L)
cbailey1994 0:287361f0056d 194 {
cbailey1994 0:287361f0056d 195 led2=1;
cbailey1994 0:287361f0056d 196 }
cbailey1994 0:287361f0056d 197 else
cbailey1994 0:287361f0056d 198 {
cbailey1994 0:287361f0056d 199 led2=0;
cbailey1994 0:287361f0056d 200 }
cbailey1994 0:287361f0056d 201 if(zdata>=L)
cbailey1994 0:287361f0056d 202 {
cbailey1994 0:287361f0056d 203 led3=1;
cbailey1994 0:287361f0056d 204 }
cbailey1994 0:287361f0056d 205 else
cbailey1994 0:287361f0056d 206 {
cbailey1994 0:287361f0056d 207 led3=0;
cbailey1994 0:287361f0056d 208 }
cbailey1994 0:287361f0056d 209 wait(T);/* wait some period of time before next sample */
cbailey1994 0:287361f0056d 210 }
cbailey1994 0:287361f0056d 211
cbailey1994 0:287361f0056d 212
cbailey1994 0:287361f0056d 213
cbailey1994 0:287361f0056d 214 break;
cbailey1994 0:287361f0056d 215
cbailey1994 0:287361f0056d 216 case(7):
cbailey1994 0:287361f0056d 217
cbailey1994 0:287361f0056d 218 pc.printf("Program stopping...\n");
cbailey1994 0:287361f0056d 219 return(0); /* Return from main, ending the program */
cbailey1994 0:287361f0056d 220 break;
cbailey1994 0:287361f0056d 221
cbailey1994 0:287361f0056d 222 default:
cbailey1994 0:287361f0056d 223 pc.printf("Error, invalid command...\n");
cbailey1994 0:287361f0056d 224 }
cbailey1994 0:287361f0056d 225 }
cbailey1994 0:287361f0056d 226
cbailey1994 0:287361f0056d 227
cbailey1994 0:287361f0056d 228 return(0);/* normal exit */
cbailey1994 0:287361f0056d 229
cbailey1994 0:287361f0056d 230 }
cbailey1994 0:287361f0056d 231
cbailey1994 0:287361f0056d 232
cbailey1994 0:287361f0056d 233
cbailey1994 0:287361f0056d 234
cbailey1994 0:287361f0056d 235