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
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.
main.cpp@6:62095a0c2429, 2013-06-27 (annotated)
- Committer:
- chapfohn
- Date:
- Thu Jun 27 05:26:26 2013 +0000
- Revision:
- 6:62095a0c2429
- Parent:
- 5:ba17585f3a2a
Takes readings of stability, assigns to score [+1] for each success.
; If ten consecutive readings are stable [score 10] prints 'NEXT LEVEL',
; LED2 HIGH for ten seconds, score assignment reset.
; Any unstable reading resets score assignment.
;
Who changed what in which revision?
User | Revision | Line number | New 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 | 6:62095a0c2429 | 17 | DigitalOut connectionLed(LED1);//operation confirmed |
chapfohn | 6:62095a0c2429 | 18 | DigitalOut servoProxyLed(LED2);//score outcome, proxy for servo |
Sissors | 0:bd0546063b0a | 19 | |
chapfohn | 3:0d76aaff55b8 | 20 | const int n = 10; //number of readings to be averaged, change globally here |
chapfohn | 6:62095a0c2429 | 21 | float score = 0; //reserved for later |
chapfohn | 3:0d76aaff55b8 | 22 | int angle = 0; //reserved for later |
chapfohn | 3:0d76aaff55b8 | 23 | float pulseXT =0, pulseYT = 0; //Total holders for summation from axis arrays |
chapfohn | 3:0d76aaff55b8 | 24 | |
chapfohn | 3:0d76aaff55b8 | 25 | float pulseXa, pulseYa; //averaged values for each axis over n |
chapfohn | 3:0d76aaff55b8 | 26 | float pulseX[n], pulseY[n]; //arrays to hold n readings for each axis |
chapfohn | 3:0d76aaff55b8 | 27 | int i, j; //indexing variables |
chapfohn | 3:0d76aaff55b8 | 28 | |
chapfohn | 3:0d76aaff55b8 | 29 | int main() |
chapfohn | 3:0d76aaff55b8 | 30 | { |
chapfohn | 3:0d76aaff55b8 | 31 | |
Sissors | 0:bd0546063b0a | 32 | while(1) { |
chapfohn | 3:0d76aaff55b8 | 33 | |
chapfohn | 3:0d76aaff55b8 | 34 | for (i = 0; i < n; i = i + 1) { //read n values into each axis array |
chapfohn | 3:0d76aaff55b8 | 35 | pulseX[i] = MMA.x(); |
chapfohn | 3:0d76aaff55b8 | 36 | pulseY[i] = MMA.y(); |
chapfohn | 3:0d76aaff55b8 | 37 | } |
chapfohn | 3:0d76aaff55b8 | 38 | pulseXT = 0; //reset Totala |
chapfohn | 3:0d76aaff55b8 | 39 | pulseYT = 0; //reset Totala |
chapfohn | 3:0d76aaff55b8 | 40 | for (j = 0; j < n; j = j + 1) { //summation of the contents of each array into axis Totals |
chapfohn | 3:0d76aaff55b8 | 41 | pulseXT = pulseXT+pulseX[j]; |
chapfohn | 3:0d76aaff55b8 | 42 | pulseYT = pulseYT+pulseY[j]; |
chapfohn | 3:0d76aaff55b8 | 43 | } |
chapfohn | 3:0d76aaff55b8 | 44 | pulseXa = pulseXT/n; //axis average over n |
chapfohn | 3:0d76aaff55b8 | 45 | |
chapfohn | 3:0d76aaff55b8 | 46 | pulseYa = pulseYT/n; //axis average over n |
chapfohn | 3:0d76aaff55b8 | 47 | |
chapfohn | 3:0d76aaff55b8 | 48 | if (MMA.testConnection()) |
chapfohn | 3:0d76aaff55b8 | 49 | connectionLed = 1; |
chapfohn | 3:0d76aaff55b8 | 50 | |
chapfohn | 5:ba17585f3a2a | 51 | if (pulseXa > (-0.2) && pulseXa < (0.2) && pulseYa > (-0.2) && pulseYa < (0.2)) {//average result within stability range; x, y |
chapfohn | 5:ba17585f3a2a | 52 | lcd.cls();//clear LCD for next reading round |
chapfohn | 5:ba17585f3a2a | 53 | lcd.locate(3,3);//first LCD column label |
chapfohn | 5:ba17585f3a2a | 54 | lcd.printf("x-axis | ");//label column |
chapfohn | 5:ba17585f3a2a | 55 | lcd.locate(3,12);//xdata location |
chapfohn | 5:ba17585f3a2a | 56 | lcd.printf("%.2f\n",pulseXa);//print x to LCD |
chapfohn | 5:ba17585f3a2a | 57 | lcd.locate(40,3);//second LCD column label |
chapfohn | 5:ba17585f3a2a | 58 | lcd.printf("y-axis | ");//label column |
chapfohn | 5:ba17585f3a2a | 59 | lcd.locate(40,12);//ydata location |
chapfohn | 5:ba17585f3a2a | 60 | lcd.printf("%.2f\n",pulseYa);//print y to LCD |
chapfohn | 5:ba17585f3a2a | 61 | lcd.locate(77,3);//initial LCD location |
chapfohn | 5:ba17585f3a2a | 62 | lcd.printf("z-axis");//label column |
chapfohn | 5:ba17585f3a2a | 63 | lcd.locate(77,12);//zdata location |
chapfohn | 5:ba17585f3a2a | 64 | lcd.printf("%.2f\n",MMA.z());//print z to LCD |
chapfohn | 5:ba17585f3a2a | 65 | lcd.locate(3,21);//flag location |
chapfohn | 5:ba17585f3a2a | 66 | lcd.printf("STABLE");//flag |
chapfohn | 6:62095a0c2429 | 67 | if (score != 10) {//if score has not reached 10 |
chapfohn | 6:62095a0c2429 | 68 | ++score;//add 1 to score |
chapfohn | 6:62095a0c2429 | 69 | lcd.locate(70,21);//score location |
chapfohn | 6:62095a0c2429 | 70 | lcd.printf("SCORE = "); |
chapfohn | 6:62095a0c2429 | 71 | lcd.printf("%.0f\n",score);//print score |
chapfohn | 6:62095a0c2429 | 72 | } else { |
chapfohn | 6:62095a0c2429 | 73 | servoProxyLed = 1;//LED2 HIGH |
chapfohn | 6:62095a0c2429 | 74 | lcd.locate(70,21);//notice location |
chapfohn | 6:62095a0c2429 | 75 | lcd.printf("NEXT LEVEL"); |
chapfohn | 6:62095a0c2429 | 76 | wait (10); |
chapfohn | 6:62095a0c2429 | 77 | servoProxyLed = 0;//LED@ LOW |
chapfohn | 6:62095a0c2429 | 78 | score = 0;//reset score |
chapfohn | 6:62095a0c2429 | 79 | |
chapfohn | 6:62095a0c2429 | 80 | } |
chapfohn | 5:ba17585f3a2a | 81 | } |
chapfohn | 3:0d76aaff55b8 | 82 | |
chapfohn | 5:ba17585f3a2a | 83 | else {////average result not within stability range; x, y |
chapfohn | 5:ba17585f3a2a | 84 | lcd.cls();//clear LCD for next reading round |
chapfohn | 5:ba17585f3a2a | 85 | lcd.locate(3,3);//first LCD column label |
chapfohn | 5:ba17585f3a2a | 86 | lcd.printf("x-axis | ");//label column |
chapfohn | 5:ba17585f3a2a | 87 | lcd.locate(3,12);//xdata location |
chapfohn | 5:ba17585f3a2a | 88 | lcd.printf("%.2f\n",pulseXa);//print x to LCD |
chapfohn | 5:ba17585f3a2a | 89 | lcd.locate(40,3);//second LCD column label |
chapfohn | 5:ba17585f3a2a | 90 | lcd.printf("y-axis | ");//label column |
chapfohn | 5:ba17585f3a2a | 91 | lcd.locate(40,12);//ydata location |
chapfohn | 5:ba17585f3a2a | 92 | lcd.printf("%.2f\n",pulseYa);//print y to LCD |
chapfohn | 5:ba17585f3a2a | 93 | lcd.locate(77,3);//initial LCD location |
chapfohn | 5:ba17585f3a2a | 94 | lcd.printf("z-axis");//label column |
chapfohn | 5:ba17585f3a2a | 95 | lcd.locate(77,12);//zdata location |
chapfohn | 5:ba17585f3a2a | 96 | lcd.printf("%.2f\n",MMA.z());//print z to LCD |
chapfohn | 5:ba17585f3a2a | 97 | lcd.locate(3,21);//flag location |
chapfohn | 5:ba17585f3a2a | 98 | lcd.printf("UNSTABLE");//flag |
chapfohn | 6:62095a0c2429 | 99 | score = 0;//reset score |
chapfohn | 6:62095a0c2429 | 100 | lcd.locate(70,21);//score location |
chapfohn | 6:62095a0c2429 | 101 | lcd.printf("SCORE = "); |
chapfohn | 6:62095a0c2429 | 102 | lcd.printf("%.0f\n",score);//print score |
chapfohn | 6:62095a0c2429 | 103 | |
chapfohn | 5:ba17585f3a2a | 104 | } |
Sissors | 0:bd0546063b0a | 105 | } |
chapfohn | 3:0d76aaff55b8 | 106 | } |