Build upon MMA7660_HelloWorld to pull out x, y, z axes from device and print to LCD on mbed Application Board

Dependencies:   C12832_lcd MMA7660 mbed

Fork of MMA7660_HelloWorld by Erik -

Here reside bits and pieces of coding that is mostly derivative of the work of others. Mostly extensions and other modifications.

The proprioception board project.

Board design images follow.

/media/uploads/chapfohn/260px-sphere-and-ring_balance_board_underside.jpg /media/uploads/chapfohn/obroc2.gif

/media/uploads/chapfohn/coolboard-balance-board-ultimate-package-medium-bot02-03-w450.png

Committer:
chapfohn
Date:
Wed Jun 05 01:27:07 2013 +0000
Revision:
5:ba17585f3a2a
Parent:
4:10426f54d388
Child:
6:62095a0c2429
Take x, y data into array, summation and average.
; Prints data to LCD.
; Tests stability in x, y plane, prints STABLE/UNSTABLE flag to LCD.
; Interest statement included.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chapfohn 1:0a7a84edc8e5 1 //Iteration for 3 axis, ...
chapfohn 5:ba17585f3a2a 2 //This code is being developed for use with
chapfohn 5:ba17585f3a2a 3 //Sphere and Ring type Physiotherapy Balance Boards
chapfohn 5:ba17585f3a2a 4 //example images at -
chapfohn 5:ba17585f3a2a 5 //http://www.balance360.com/servlet/the-Balance-360-Boards/Categories
chapfohn 5:ba17585f3a2a 6 //The author has no commercial, or other, relationship with [Balance 360] or other manufacturers
chapfohn 5:ba17585f3a2a 7 //The author is simply having fun
chapfohn 5:ba17585f3a2a 8 //C B P Chapman
Sissors 0:bd0546063b0a 9
Sissors 0:bd0546063b0a 10 #include "mbed.h"
Sissors 0:bd0546063b0a 11 #include "MMA7660.h"
chapfohn 1:0a7a84edc8e5 12 #include "C12832_lcd.h"
Sissors 0:bd0546063b0a 13
chapfohn 1:0a7a84edc8e5 14 C12832_LCD lcd;
Sissors 0:bd0546063b0a 15 MMA7660 MMA(p28, p27);
Sissors 0:bd0546063b0a 16
chapfohn 2:b0a8d3b7a6dd 17 DigitalOut connectionLed(LED1);//for later debug
Sissors 0:bd0546063b0a 18
chapfohn 3:0d76aaff55b8 19 const int n = 10; //number of readings to be averaged, change globally here
chapfohn 3:0d76aaff55b8 20 int score = 0; //reserved for later
chapfohn 3:0d76aaff55b8 21 int angle = 0; //reserved for later
chapfohn 3:0d76aaff55b8 22 float pulseXT =0, pulseYT = 0; //Total holders for summation from axis arrays
chapfohn 3:0d76aaff55b8 23
chapfohn 3:0d76aaff55b8 24 float pulseXa, pulseYa; //averaged values for each axis over n
chapfohn 3:0d76aaff55b8 25 float pulseX[n], pulseY[n]; //arrays to hold n readings for each axis
chapfohn 3:0d76aaff55b8 26 int i, j; //indexing variables
chapfohn 3:0d76aaff55b8 27
chapfohn 3:0d76aaff55b8 28 int main()
chapfohn 3:0d76aaff55b8 29 {
chapfohn 3:0d76aaff55b8 30
Sissors 0:bd0546063b0a 31 while(1) {
chapfohn 3:0d76aaff55b8 32
chapfohn 3:0d76aaff55b8 33 for (i = 0; i < n; i = i + 1) { //read n values into each axis array
chapfohn 3:0d76aaff55b8 34 pulseX[i] = MMA.x();
chapfohn 3:0d76aaff55b8 35 pulseY[i] = MMA.y();
chapfohn 3:0d76aaff55b8 36 }
chapfohn 3:0d76aaff55b8 37 pulseXT = 0; //reset Totala
chapfohn 3:0d76aaff55b8 38 pulseYT = 0; //reset Totala
chapfohn 3:0d76aaff55b8 39 for (j = 0; j < n; j = j + 1) { //summation of the contents of each array into axis Totals
chapfohn 3:0d76aaff55b8 40 pulseXT = pulseXT+pulseX[j];
chapfohn 3:0d76aaff55b8 41 pulseYT = pulseYT+pulseY[j];
chapfohn 3:0d76aaff55b8 42 }
chapfohn 3:0d76aaff55b8 43 pulseXa = pulseXT/n; //axis average over n
chapfohn 3:0d76aaff55b8 44
chapfohn 3:0d76aaff55b8 45 pulseYa = pulseYT/n; //axis average over n
chapfohn 3:0d76aaff55b8 46
chapfohn 3:0d76aaff55b8 47 if (MMA.testConnection())
chapfohn 3:0d76aaff55b8 48 connectionLed = 1;
chapfohn 3:0d76aaff55b8 49
chapfohn 5:ba17585f3a2a 50 if (pulseXa > (-0.2) && pulseXa < (0.2) && pulseYa > (-0.2) && pulseYa < (0.2)) {//average result within stability range; x, y
chapfohn 5:ba17585f3a2a 51 lcd.cls();//clear LCD for next reading round
chapfohn 5:ba17585f3a2a 52 lcd.locate(3,3);//first LCD column label
chapfohn 5:ba17585f3a2a 53 lcd.printf("x-axis | ");//label column
chapfohn 5:ba17585f3a2a 54 lcd.locate(3,12);//xdata location
chapfohn 5:ba17585f3a2a 55 lcd.printf("%.2f\n",pulseXa);//print x to LCD
chapfohn 5:ba17585f3a2a 56 lcd.locate(40,3);//second LCD column label
chapfohn 5:ba17585f3a2a 57 lcd.printf("y-axis | ");//label column
chapfohn 5:ba17585f3a2a 58 lcd.locate(40,12);//ydata location
chapfohn 5:ba17585f3a2a 59 lcd.printf("%.2f\n",pulseYa);//print y to LCD
chapfohn 5:ba17585f3a2a 60 lcd.locate(77,3);//initial LCD location
chapfohn 5:ba17585f3a2a 61 lcd.printf("z-axis");//label column
chapfohn 5:ba17585f3a2a 62 lcd.locate(77,12);//zdata location
chapfohn 5:ba17585f3a2a 63 lcd.printf("%.2f\n",MMA.z());//print z to LCD
chapfohn 5:ba17585f3a2a 64 lcd.locate(3,21);//flag location
chapfohn 5:ba17585f3a2a 65 lcd.printf("STABLE");//flag
chapfohn 5:ba17585f3a2a 66 //wait (2);
chapfohn 5:ba17585f3a2a 67 }
chapfohn 3:0d76aaff55b8 68
chapfohn 5:ba17585f3a2a 69 else {////average result not within stability range; x, y
chapfohn 5:ba17585f3a2a 70 lcd.cls();//clear LCD for next reading round
chapfohn 5:ba17585f3a2a 71 lcd.locate(3,3);//first LCD column label
chapfohn 5:ba17585f3a2a 72 lcd.printf("x-axis | ");//label column
chapfohn 5:ba17585f3a2a 73 lcd.locate(3,12);//xdata location
chapfohn 5:ba17585f3a2a 74 lcd.printf("%.2f\n",pulseXa);//print x to LCD
chapfohn 5:ba17585f3a2a 75 lcd.locate(40,3);//second LCD column label
chapfohn 5:ba17585f3a2a 76 lcd.printf("y-axis | ");//label column
chapfohn 5:ba17585f3a2a 77 lcd.locate(40,12);//ydata location
chapfohn 5:ba17585f3a2a 78 lcd.printf("%.2f\n",pulseYa);//print y to LCD
chapfohn 5:ba17585f3a2a 79 lcd.locate(77,3);//initial LCD location
chapfohn 5:ba17585f3a2a 80 lcd.printf("z-axis");//label column
chapfohn 5:ba17585f3a2a 81 lcd.locate(77,12);//zdata location
chapfohn 5:ba17585f3a2a 82 lcd.printf("%.2f\n",MMA.z());//print z to LCD
chapfohn 5:ba17585f3a2a 83 lcd.locate(3,21);//flag location
chapfohn 5:ba17585f3a2a 84 lcd.printf("UNSTABLE");//flag
chapfohn 5:ba17585f3a2a 85 //wait (2);
chapfohn 5:ba17585f3a2a 86 }
Sissors 0:bd0546063b0a 87 }
chapfohn 3:0d76aaff55b8 88 }