Part2
Dependencies: MMA8451Q SLCD mbed
Fork of ACC_LCD_341_MID by
acc_341.cpp@3:bc69bca92c05, 2014-10-06 (annotated)
- Committer:
- sphasim
- Date:
- Mon Oct 06 07:54:05 2014 +0000
- Revision:
- 3:bc69bca92c05
- Parent:
- 2:6003ed409def
Part 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sphasim | 3:bc69bca92c05 | 1 | /* |
sphasim | 3:bc69bca92c05 | 2 | Test of the accelerometer, digital I/O, on-board LCD screen. |
sphasim | 3:bc69bca92c05 | 3 | RED: Until absolute vertical is reached |
sphasim | 3:bc69bca92c05 | 4 | GREEN: Until absolute horizontal is reached |
sphasim | 3:bc69bca92c05 | 5 | |
sphasim | 3:bc69bca92c05 | 6 | Author: Siphamandla P. Simelane |
sphasim | 3:bc69bca92c05 | 7 | Date: 10/6/2014 |
sphasim | 3:bc69bca92c05 | 8 | */ |
sphasim | 3:bc69bca92c05 | 9 | |
scohennm | 0:203b4129a213 | 10 | #include "mbed.h" |
scohennm | 0:203b4129a213 | 11 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 12 | #include "SLCD.h" |
sphasim | 3:bc69bca92c05 | 13 | #include "math.h" |
scohennm | 0:203b4129a213 | 14 | |
sphasim | 3:bc69bca92c05 | 15 | // Configuring Accelerometer/Magnetometer: |
scohennm | 0:203b4129a213 | 16 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 17 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 18 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 19 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 20 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 21 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 22 | #else |
scohennm | 0:203b4129a213 | 23 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 24 | #endif |
scohennm | 0:203b4129a213 | 25 | |
scohennm | 0:203b4129a213 | 26 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 27 | |
sphasim | 3:bc69bca92c05 | 28 | #define PI 3.14159265 |
sphasim | 3:bc69bca92c05 | 29 | #define DATATIME 0.150 |
sphasim | 3:bc69bca92c05 | 30 | #define PROGNAME "ACCLCD341VB\r/n" |
sphasim | 3:bc69bca92c05 | 31 | #define PRINTDBUG |
sphasim | 3:bc69bca92c05 | 32 | #define LEDON false |
sphasim | 3:bc69bca92c05 | 33 | #define LEDOFF true |
sphasim | 3:bc69bca92c05 | 34 | #define LCDLEN 10 |
sphasim | 3:bc69bca92c05 | 35 | #define LOW_POINT 0.0 |
sphasim | 3:bc69bca92c05 | 36 | #define LOW_LIMIT 0.1 |
sphasim | 3:bc69bca92c05 | 37 | #define HIGH_LIMIT 1.1 |
sphasim | 3:bc69bca92c05 | 38 | #define BLINKTIME 0.2// milliseconds |
sphasim | 3:bc69bca92c05 | 39 | |
sphasim | 3:bc69bca92c05 | 40 | |
sphasim | 3:bc69bca92c05 | 41 | enum orientStates {INTERMEDIATE, LANDSCAPE, PORTRAIT}; // define the states |
sphasim | 3:bc69bca92c05 | 42 | PwmOut greenLed(LED_GREEN); |
sphasim | 3:bc69bca92c05 | 43 | PwmOut redLed(LED_RED); |
scohennm | 0:203b4129a213 | 44 | SLCD slcd; //define LCD display |
scohennm | 0:203b4129a213 | 45 | |
scohennm | 0:203b4129a213 | 46 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 47 | Serial pc(USBTX, USBRX); |
scohennm | 0:203b4129a213 | 48 | |
sphasim | 3:bc69bca92c05 | 49 | float sqrt_newt(float arg) { |
sphasim | 3:bc69bca92c05 | 50 | int i = 0; |
sphasim | 3:bc69bca92c05 | 51 | float X1 = 0.0; |
sphasim | 3:bc69bca92c05 | 52 | int intMax = 30; |
sphasim | 3:bc69bca92c05 | 53 | int epsilon = 1e-7; |
sphasim | 3:bc69bca92c05 | 54 | float my_x = float(arg/2.0); |
sphasim | 3:bc69bca92c05 | 55 | int delta = 1; |
sphasim | 3:bc69bca92c05 | 56 | for(i=0; i<intMax; i++){ |
sphasim | 3:bc69bca92c05 | 57 | X1 = 0.5*(my_x + (arg/my_x)); |
sphasim | 3:bc69bca92c05 | 58 | delta = abs(X1-my_x); |
sphasim | 3:bc69bca92c05 | 59 | if (delta < epsilon) |
sphasim | 3:bc69bca92c05 | 60 | break; |
sphasim | 3:bc69bca92c05 | 61 | else |
sphasim | 3:bc69bca92c05 | 62 | my_x = X1; |
sphasim | 3:bc69bca92c05 | 63 | } |
sphasim | 3:bc69bca92c05 | 64 | return my_x; |
sphasim | 3:bc69bca92c05 | 65 | |
sphasim | 3:bc69bca92c05 | 66 | // return (sqrt(arg)); |
scohennm | 2:6003ed409def | 67 | } |
scohennm | 0:203b4129a213 | 68 | |
sphasim | 3:bc69bca92c05 | 69 | // Print using LCD screen |
scohennm | 0:203b4129a213 | 70 | void LCDMess(char *lMess, float dWait){ |
sphasim | 3:bc69bca92c05 | 71 | slcd.Home(); |
sphasim | 3:bc69bca92c05 | 72 | slcd.clear(); |
sphasim | 3:bc69bca92c05 | 73 | slcd.printf(lMess); |
sphasim | 3:bc69bca92c05 | 74 | wait(dWait); |
scohennm | 0:203b4129a213 | 75 | } |
scohennm | 0:203b4129a213 | 76 | |
scohennm | 0:203b4129a213 | 77 | |
scohennm | 0:203b4129a213 | 78 | int main() { |
scohennm | 0:203b4129a213 | 79 | float xAcc; |
scohennm | 0:203b4129a213 | 80 | float yAcc; |
sphasim | 3:bc69bca92c05 | 81 | float vector; |
sphasim | 3:bc69bca92c05 | 82 | float angle; |
sphasim | 3:bc69bca92c05 | 83 | char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon |
sphasim | 3:bc69bca92c05 | 84 | orientStates PGMState = INTERMEDIATE; |
sphasim | 3:bc69bca92c05 | 85 | |
sphasim | 3:bc69bca92c05 | 86 | #ifdef PRINTDBUG |
scohennm | 0:203b4129a213 | 87 | pc.printf(PROGNAME); |
sphasim | 3:bc69bca92c05 | 88 | #endif |
scohennm | 0:203b4129a213 | 89 | |
sphasim | 3:bc69bca92c05 | 90 | // main loop forever |
sphasim | 3:bc69bca92c05 | 91 | while(true) { |
sphasim | 3:bc69bca92c05 | 92 | switch (PGMState){ |
sphasim | 3:bc69bca92c05 | 93 | case INTERMEDIATE: |
sphasim | 3:bc69bca92c05 | 94 | //Get accelerometer data - tilt angles minus offset for zero mark. |
sphasim | 3:bc69bca92c05 | 95 | xAcc = abs(acc.getAccX()); |
sphasim | 3:bc69bca92c05 | 96 | yAcc = abs(acc.getAccY()); |
sphasim | 3:bc69bca92c05 | 97 | |
sphasim | 3:bc69bca92c05 | 98 | // Calulate vector sum and angle of x and y reading. |
sphasim | 3:bc69bca92c05 | 99 | vector = sqrt_newt(pow(xAcc,2) + pow(yAcc,2)); |
sphasim | 3:bc69bca92c05 | 100 | angle = atan(yAcc / xAcc)* 180 / PI; |
sphasim | 3:bc69bca92c05 | 101 | |
sphasim | 3:bc69bca92c05 | 102 | #ifdef PRINTDBUG |
sphasim | 3:bc69bca92c05 | 103 | pc.printf("xAcc = %f\r\n", xAcc); |
sphasim | 3:bc69bca92c05 | 104 | pc.printf("yAcc = %f\r\n", yAcc); |
sphasim | 3:bc69bca92c05 | 105 | pc.printf("vector = %f\r\n", vector); |
sphasim | 3:bc69bca92c05 | 106 | pc.printf("Angle = %f\r\n", angle); |
sphasim | 3:bc69bca92c05 | 107 | #endif |
sphasim | 3:bc69bca92c05 | 108 | sprintf (lcdData,"%4.3f", vector); |
sphasim | 3:bc69bca92c05 | 109 | LCDMess(lcdData, DATATIME); |
sphasim | 3:bc69bca92c05 | 110 | |
sphasim | 3:bc69bca92c05 | 111 | // Define the landscape and portrait position using x and y readings |
sphasim | 3:bc69bca92c05 | 112 | if(yAcc < LOW_LIMIT && xAcc > LOW_POINT && xAcc < HIGH_LIMIT){ |
sphasim | 3:bc69bca92c05 | 113 | PGMState = PORTRAIT; |
sphasim | 3:bc69bca92c05 | 114 | } else if (xAcc < LOW_LIMIT && yAcc > LOW_POINT && yAcc < HIGH_LIMIT){ |
sphasim | 3:bc69bca92c05 | 115 | PGMState = LANDSCAPE; |
sphasim | 3:bc69bca92c05 | 116 | } else { |
sphasim | 3:bc69bca92c05 | 117 | PGMState = INTERMEDIATE; |
sphasim | 3:bc69bca92c05 | 118 | } |
sphasim | 3:bc69bca92c05 | 119 | break; |
sphasim | 3:bc69bca92c05 | 120 | |
sphasim | 3:bc69bca92c05 | 121 | case PORTRAIT: |
sphasim | 3:bc69bca92c05 | 122 | // Green led ON and red OFF |
sphasim | 3:bc69bca92c05 | 123 | redLed.write(LEDON); |
sphasim | 3:bc69bca92c05 | 124 | greenLed.write(LEDOFF); |
sphasim | 3:bc69bca92c05 | 125 | PGMState = INTERMEDIATE; // go idle state |
sphasim | 3:bc69bca92c05 | 126 | break; |
sphasim | 3:bc69bca92c05 | 127 | |
sphasim | 3:bc69bca92c05 | 128 | case LANDSCAPE: |
sphasim | 3:bc69bca92c05 | 129 | // Green led OFF and red ON |
sphasim | 3:bc69bca92c05 | 130 | redLed.write(LEDOFF); |
sphasim | 3:bc69bca92c05 | 131 | greenLed.write(LEDON); |
sphasim | 3:bc69bca92c05 | 132 | PGMState = INTERMEDIATE; // go idle state |
sphasim | 3:bc69bca92c05 | 133 | break; |
sphasim | 3:bc69bca92c05 | 134 | |
sphasim | 3:bc69bca92c05 | 135 | default: |
sphasim | 3:bc69bca92c05 | 136 | PGMState = INTERMEDIATE; // go idle state |
sphasim | 3:bc69bca92c05 | 137 | break; |
sphasim | 3:bc69bca92c05 | 138 | |
sphasim | 3:bc69bca92c05 | 139 | } // end state machine |
scohennm | 0:203b4129a213 | 140 | } |
scohennm | 0:203b4129a213 | 141 | } |