![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Part of Final Questions for SSD 341 NMNU Fall 2014
Dependencies: MMA8451Q SLCD mbed
Fork of LCD_ACC_46_v3 by
lcd_acc_46_v3.cpp@1:6c2ec7b0e1c9, 2014-11-30 (annotated)
- Committer:
- scohennm
- Date:
- Sun Nov 30 01:04:47 2014 +0000
- Revision:
- 1:6c2ec7b0e1c9
- Parent:
- 0:88ddbd76b455
- Child:
- 2:8cdbe8a96b59
Changed timer no wait()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:88ddbd76b455 | 1 | #include "mbed.h" |
scohennm | 0:88ddbd76b455 | 2 | #include "MMA8451Q.h" |
scohennm | 0:88ddbd76b455 | 3 | #include "SLCD.h" |
scohennm | 0:88ddbd76b455 | 4 | /* |
scohennm | 0:88ddbd76b455 | 5 | Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC. |
scohennm | 0:88ddbd76b455 | 6 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:88ddbd76b455 | 7 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:88ddbd76b455 | 8 | Addiing touch sensor to replace potentiometer. |
scohennm | 0:88ddbd76b455 | 9 | Show x and y components |
scohennm | 0:88ddbd76b455 | 10 | */ |
scohennm | 0:88ddbd76b455 | 11 | |
scohennm | 1:6c2ec7b0e1c9 | 12 | #define BLINKTIME 0.7 |
scohennm | 0:88ddbd76b455 | 13 | #define DATATIME 0.1 |
scohennm | 1:6c2ec7b0e1c9 | 14 | #define DATADISPDWELL 0.2 |
scohennm | 0:88ddbd76b455 | 15 | #define NUMLEDS 2 |
scohennm | 0:88ddbd76b455 | 16 | #define LEDON 0 |
scohennm | 0:88ddbd76b455 | 17 | #define LEDOFF 1 |
scohennm | 0:88ddbd76b455 | 18 | #define SCALING 13 |
scohennm | 0:88ddbd76b455 | 19 | #define RSTARTMESS "RSET" |
scohennm | 0:88ddbd76b455 | 20 | #define MAXVECT "MAXV" |
scohennm | 0:88ddbd76b455 | 21 | #define YCOMP "YCMP" |
scohennm | 0:88ddbd76b455 | 22 | #define ANALTOVOLTS 3.3 |
scohennm | 0:88ddbd76b455 | 23 | #define LEDDELAY 0.400 |
scohennm | 0:88ddbd76b455 | 24 | |
scohennm | 0:88ddbd76b455 | 25 | #define PROGNAME "LCD_ACC_LCDv3 46\r/n" |
scohennm | 0:88ddbd76b455 | 26 | |
scohennm | 0:88ddbd76b455 | 27 | //#define PRINTDBUG |
scohennm | 0:88ddbd76b455 | 28 | // Accelerometer SPI pins |
scohennm | 0:88ddbd76b455 | 29 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 0:88ddbd76b455 | 30 | PinName const SDA = PTE25; |
scohennm | 0:88ddbd76b455 | 31 | PinName const SCL = PTE24; |
scohennm | 0:88ddbd76b455 | 32 | #elif defined (TARGET_KL05Z) |
scohennm | 0:88ddbd76b455 | 33 | PinName const SDA = PTB4; |
scohennm | 0:88ddbd76b455 | 34 | PinName const SCL = PTB3; |
scohennm | 0:88ddbd76b455 | 35 | #else |
scohennm | 0:88ddbd76b455 | 36 | #error TARGET NOT DEFINED |
scohennm | 0:88ddbd76b455 | 37 | #endif |
scohennm | 0:88ddbd76b455 | 38 | |
scohennm | 0:88ddbd76b455 | 39 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:88ddbd76b455 | 40 | |
scohennm | 0:88ddbd76b455 | 41 | SLCD slcd; //define LCD display |
scohennm | 0:88ddbd76b455 | 42 | Timer blinkTimer; |
scohennm | 0:88ddbd76b455 | 43 | Timer dataTimer; |
scohennm | 0:88ddbd76b455 | 44 | Timer displayTimer; |
scohennm | 0:88ddbd76b455 | 45 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:88ddbd76b455 | 46 | AnalogIn TMP01(PTB1); |
scohennm | 0:88ddbd76b455 | 47 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
scohennm | 0:88ddbd76b455 | 48 | Serial pc(USBTX, USBRX); |
scohennm | 0:88ddbd76b455 | 49 | |
scohennm | 0:88ddbd76b455 | 50 | |
scohennm | 0:88ddbd76b455 | 51 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:88ddbd76b455 | 52 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 53 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 54 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 55 | wait(dWait); |
scohennm | 0:88ddbd76b455 | 56 | } |
scohennm | 0:88ddbd76b455 | 57 | void LCDMessNoDwell(char *lMess){ |
scohennm | 0:88ddbd76b455 | 58 | slcd.Home(); |
scohennm | 0:88ddbd76b455 | 59 | slcd.clear(); |
scohennm | 0:88ddbd76b455 | 60 | slcd.printf(lMess); |
scohennm | 0:88ddbd76b455 | 61 | } |
scohennm | 0:88ddbd76b455 | 62 | void GetOffsets(float *xo, float *yo){ |
scohennm | 0:88ddbd76b455 | 63 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 0:88ddbd76b455 | 64 | *xo = acc.getAccX(); |
scohennm | 0:88ddbd76b455 | 65 | *yo = acc.getAccY(); |
scohennm | 0:88ddbd76b455 | 66 | return; |
scohennm | 0:88ddbd76b455 | 67 | } |
scohennm | 0:88ddbd76b455 | 68 | |
scohennm | 0:88ddbd76b455 | 69 | |
scohennm | 0:88ddbd76b455 | 70 | void allLEDsOFF(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 71 | int i; |
scohennm | 0:88ddbd76b455 | 72 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 73 | LEDs[i] = LEDOFF; |
scohennm | 0:88ddbd76b455 | 74 | } |
scohennm | 0:88ddbd76b455 | 75 | |
scohennm | 0:88ddbd76b455 | 76 | } |
scohennm | 0:88ddbd76b455 | 77 | |
scohennm | 0:88ddbd76b455 | 78 | void runLEDs(int numberOfLEDS) { |
scohennm | 0:88ddbd76b455 | 79 | int i; |
scohennm | 0:88ddbd76b455 | 80 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 0:88ddbd76b455 | 81 | LEDs[i] = LEDON; |
scohennm | 0:88ddbd76b455 | 82 | wait(0.2); |
scohennm | 0:88ddbd76b455 | 83 | } |
scohennm | 0:88ddbd76b455 | 84 | allLEDsOFF(numberOfLEDS); |
scohennm | 0:88ddbd76b455 | 85 | } |
scohennm | 0:88ddbd76b455 | 86 | |
scohennm | 0:88ddbd76b455 | 87 | |
scohennm | 0:88ddbd76b455 | 88 | |
scohennm | 0:88ddbd76b455 | 89 | int main() { |
scohennm | 0:88ddbd76b455 | 90 | |
scohennm | 0:88ddbd76b455 | 91 | int RButtonState; |
scohennm | 0:88ddbd76b455 | 92 | int LButtonState; |
scohennm | 0:88ddbd76b455 | 93 | DigitalIn RtButton(PTC12); |
scohennm | 0:88ddbd76b455 | 94 | DigitalIn LftButton(PTC3); |
scohennm | 0:88ddbd76b455 | 95 | float xOffset; |
scohennm | 0:88ddbd76b455 | 96 | float yOffset; |
scohennm | 0:88ddbd76b455 | 97 | int displayState = true; |
scohennm | 1:6c2ec7b0e1c9 | 98 | float xAcc = 0.0; |
scohennm | 1:6c2ec7b0e1c9 | 99 | float yAcc = 0.0; |
scohennm | 1:6c2ec7b0e1c9 | 100 | // float zAcc; |
scohennm | 0:88ddbd76b455 | 101 | float vector; |
scohennm | 0:88ddbd76b455 | 102 | float vMax = 0.0; |
scohennm | 1:6c2ec7b0e1c9 | 103 | float DisplayTime = DATADISPDWELL; |
scohennm | 0:88ddbd76b455 | 104 | int outState = false; |
scohennm | 0:88ddbd76b455 | 105 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 0:88ddbd76b455 | 106 | |
scohennm | 0:88ddbd76b455 | 107 | |
scohennm | 0:88ddbd76b455 | 108 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 109 | pc.printf(PROGNAME); |
scohennm | 0:88ddbd76b455 | 110 | #endif |
scohennm | 0:88ddbd76b455 | 111 | // Initialze readings and sequence the LED's for dramtic effect. |
scohennm | 0:88ddbd76b455 | 112 | allLEDsOFF(NUMLEDS); |
scohennm | 0:88ddbd76b455 | 113 | GetOffsets(&xOffset, &yOffset); |
scohennm | 0:88ddbd76b455 | 114 | // runLEDs(NUMLEDS); |
scohennm | 0:88ddbd76b455 | 115 | blinkTimer.start(); |
scohennm | 0:88ddbd76b455 | 116 | blinkTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 117 | displayTimer.start(); |
scohennm | 1:6c2ec7b0e1c9 | 118 | displayTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 119 | dataTimer.start(); |
scohennm | 1:6c2ec7b0e1c9 | 120 | dataTimer.reset(); |
scohennm | 0:88ddbd76b455 | 121 | |
scohennm | 0:88ddbd76b455 | 122 | // main loop forever |
scohennm | 0:88ddbd76b455 | 123 | while(true) { |
scohennm | 0:88ddbd76b455 | 124 | // Alive LEDs |
scohennm | 0:88ddbd76b455 | 125 | while(blinkTimer.read()> LEDDELAY) { |
scohennm | 0:88ddbd76b455 | 126 | LEDs[0].write(outState); |
scohennm | 0:88ddbd76b455 | 127 | LEDs[1].write(!outState); |
scohennm | 0:88ddbd76b455 | 128 | outState = !outState; |
scohennm | 0:88ddbd76b455 | 129 | blinkTimer.reset(); |
scohennm | 0:88ddbd76b455 | 130 | } |
scohennm | 0:88ddbd76b455 | 131 | // Handle user input for display selections |
scohennm | 0:88ddbd76b455 | 132 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
scohennm | 0:88ddbd76b455 | 133 | if (RButtonState){ |
scohennm | 0:88ddbd76b455 | 134 | vMax = 0.0; // Clear vMax |
scohennm | 0:88ddbd76b455 | 135 | GetOffsets(&xOffset, &yOffset); // set zero baseling for position |
scohennm | 0:88ddbd76b455 | 136 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 137 | pc.printf("xOffset = %f\r\n", xOffset); |
scohennm | 0:88ddbd76b455 | 138 | pc.printf("yOffset = %f\r\n", yOffset); |
scohennm | 0:88ddbd76b455 | 139 | #endif |
scohennm | 0:88ddbd76b455 | 140 | } |
scohennm | 0:88ddbd76b455 | 141 | LButtonState = !LftButton; |
scohennm | 0:88ddbd76b455 | 142 | if (LButtonState) { //Change data that is displayed |
scohennm | 0:88ddbd76b455 | 143 | displayState = !displayState; |
scohennm | 0:88ddbd76b455 | 144 | // Change to switch/case soon. |
scohennm | 0:88ddbd76b455 | 145 | if (displayState) { |
scohennm | 0:88ddbd76b455 | 146 | LCDMess(YCOMP,BLINKTIME); |
scohennm | 0:88ddbd76b455 | 147 | }else { |
scohennm | 0:88ddbd76b455 | 148 | LCDMess(MAXVECT,BLINKTIME); |
scohennm | 0:88ddbd76b455 | 149 | } |
scohennm | 0:88ddbd76b455 | 150 | } |
scohennm | 1:6c2ec7b0e1c9 | 151 | |
scohennm | 1:6c2ec7b0e1c9 | 152 | // -------------------------------------------- |
scohennm | 1:6c2ec7b0e1c9 | 153 | while (dataTimer.read() > DATATIME){ |
scohennm | 0:88ddbd76b455 | 154 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 0:88ddbd76b455 | 155 | // No offset |
scohennm | 0:88ddbd76b455 | 156 | xAcc = abs(acc.getAccX()); |
scohennm | 0:88ddbd76b455 | 157 | yAcc = abs(acc.getAccY()); |
scohennm | 1:6c2ec7b0e1c9 | 158 | // zAcc = abs(acc.getAccZ()); |
scohennm | 0:88ddbd76b455 | 159 | // Calulate vector sum of x,y and z reading. |
scohennm | 1:6c2ec7b0e1c9 | 160 | vector = sqrt(pow(xAcc,2) + pow(yAcc,2)); |
scohennm | 0:88ddbd76b455 | 161 | if (vector > vMax) { |
scohennm | 0:88ddbd76b455 | 162 | vMax = vector; |
scohennm | 0:88ddbd76b455 | 163 | } |
scohennm | 1:6c2ec7b0e1c9 | 164 | dataTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 165 | } |
scohennm | 0:88ddbd76b455 | 166 | #ifdef PRINTDBUG |
scohennm | 0:88ddbd76b455 | 167 | pc.printf("vector = %f\r\n", yAcc); |
scohennm | 0:88ddbd76b455 | 168 | pc.printf("scaling = %f\r\n", scaleExpansion); |
scohennm | 0:88ddbd76b455 | 169 | pc.printf("RawTemp = %f\r\n", FDeg); |
scohennm | 0:88ddbd76b455 | 170 | #endif |
scohennm | 0:88ddbd76b455 | 171 | // Display the appropriate data on the LCD based upon what mode was chosen |
scohennm | 1:6c2ec7b0e1c9 | 172 | while (displayTimer.read() > DisplayTime){ |
scohennm | 0:88ddbd76b455 | 173 | if (displayState) { |
scohennm | 0:88ddbd76b455 | 174 | sprintf (lcdData,"%4.3f",yAcc); |
scohennm | 0:88ddbd76b455 | 175 | }else { |
scohennm | 0:88ddbd76b455 | 176 | sprintf (lcdData,"%4.3f",vMax); |
scohennm | 0:88ddbd76b455 | 177 | } |
scohennm | 1:6c2ec7b0e1c9 | 178 | // LCDMess(lcdData,DisplayTime); |
scohennm | 1:6c2ec7b0e1c9 | 179 | LCDMessNoDwell(lcdData); |
scohennm | 1:6c2ec7b0e1c9 | 180 | displayTimer.reset(); |
scohennm | 1:6c2ec7b0e1c9 | 181 | } // displaytimer |
scohennm | 0:88ddbd76b455 | 182 | // Wait then do the whole thing again. |
scohennm | 0:88ddbd76b455 | 183 | }//forever loop |
scohennm | 0:88ddbd76b455 | 184 | }// main |