Final_Project

Dependencies:   MMA8451Q8a SLCD mbed

Fork of SP_LCD_punch_mtr_8_v5_class by Siphamandla Simelane

Committer:
scohennm
Date:
Thu Dec 11 00:40:20 2014 +0000
Revision:
4:89ae38dc05a9
Child:
5:6ec1dac45861
Example program measures acceleration of the board with g selection up to 8 g's

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 4:89ae38dc05a9 1 #include "mbed.h"
scohennm 4:89ae38dc05a9 2 #include "MMA8451Q8.h"
scohennm 4:89ae38dc05a9 3 #include "SLCD.h"
scohennm 4:89ae38dc05a9 4 /*
scohennm 4:89ae38dc05a9 5 Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC.
scohennm 4:89ae38dc05a9 6 Looing at vector product of the x-y components of the accelerometer.
scohennm 4:89ae38dc05a9 7 Works pretty well. Still rough, program wise - sc 140710
scohennm 4:89ae38dc05a9 8 Addiing touch sensor to replace potentiometer.
scohennm 4:89ae38dc05a9 9 Show x and y components
scohennm 4:89ae38dc05a9 10 */
scohennm 4:89ae38dc05a9 11
scohennm 4:89ae38dc05a9 12 #define BLINKTIME 0.7
scohennm 4:89ae38dc05a9 13 #define DATATIME 0.1
scohennm 4:89ae38dc05a9 14 #define DATADISPDWELL 0.2
scohennm 4:89ae38dc05a9 15 #define NUMLEDS 2
scohennm 4:89ae38dc05a9 16 #define LEDON 0
scohennm 4:89ae38dc05a9 17 #define LEDOFF 1
scohennm 4:89ae38dc05a9 18 #define SCALING 13
scohennm 4:89ae38dc05a9 19 #define LCDLEN 10
scohennm 4:89ae38dc05a9 20 #define RSTARTMESS "PNCH"
scohennm 4:89ae38dc05a9 21 #define MAXVECT "MAXV"
scohennm 4:89ae38dc05a9 22 #define XCOMP "XCMP"
scohennm 4:89ae38dc05a9 23 #define YCOMP "YCMP"
scohennm 4:89ae38dc05a9 24 #define ZCOMP "ZCMP"
scohennm 4:89ae38dc05a9 25 #define ANALTOVOLTS 3.3
scohennm 4:89ae38dc05a9 26 #define LEDDELAY 0.400
scohennm 4:89ae38dc05a9 27 //define states
scohennm 4:89ae38dc05a9 28 #define NUMSTATES 4
scohennm 4:89ae38dc05a9 29 #define XCOMPD 0
scohennm 4:89ae38dc05a9 30 #define YCOMPD 1
scohennm 4:89ae38dc05a9 31 #define ZCOMPD 2
scohennm 4:89ae38dc05a9 32 #define VMAXD 3
scohennm 4:89ae38dc05a9 33 #define MAXGS 8.0
scohennm 4:89ae38dc05a9 34
scohennm 4:89ae38dc05a9 35 #define REG_WHO_AM_I 0x0D
scohennm 4:89ae38dc05a9 36 #define XYZ_DATA_CFG 0x0E
scohennm 4:89ae38dc05a9 37
scohennm 4:89ae38dc05a9 38 #define MAX_2G 0x00
scohennm 4:89ae38dc05a9 39 #define MAX_4G 0x01
scohennm 4:89ae38dc05a9 40 #define MAX_8G 0x02
scohennm 4:89ae38dc05a9 41
scohennm 4:89ae38dc05a9 42
scohennm 4:89ae38dc05a9 43 #define PROGNAME "LCD_punch_mtr 8 v5\r/n"
scohennm 4:89ae38dc05a9 44 #define LCDNAME "PC.V5"
scohennm 4:89ae38dc05a9 45
scohennm 4:89ae38dc05a9 46 //#define PRINTDBUG
scohennm 4:89ae38dc05a9 47 // Accelerometer SPI pins
scohennm 4:89ae38dc05a9 48 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 4:89ae38dc05a9 49 PinName const SDA = PTE25;
scohennm 4:89ae38dc05a9 50 PinName const SCL = PTE24;
scohennm 4:89ae38dc05a9 51 #elif defined (TARGET_KL05Z)
scohennm 4:89ae38dc05a9 52 PinName const SDA = PTB4;
scohennm 4:89ae38dc05a9 53 PinName const SCL = PTB3;
scohennm 4:89ae38dc05a9 54 #else
scohennm 4:89ae38dc05a9 55 #error TARGET NOT DEFINED
scohennm 4:89ae38dc05a9 56 #endif
scohennm 4:89ae38dc05a9 57
scohennm 4:89ae38dc05a9 58 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 4:89ae38dc05a9 59
scohennm 4:89ae38dc05a9 60 SLCD slcd; //define LCD display
scohennm 4:89ae38dc05a9 61 Timer blinkTimer;
scohennm 4:89ae38dc05a9 62 Timer dataTimer;
scohennm 4:89ae38dc05a9 63 Timer displayTimer;
scohennm 4:89ae38dc05a9 64 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 4:89ae38dc05a9 65 AnalogIn TMP01(PTB1);
scohennm 4:89ae38dc05a9 66 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
scohennm 4:89ae38dc05a9 67 Serial pc(USBTX, USBRX);
scohennm 4:89ae38dc05a9 68 char displayTitles[NUMSTATES][LCDLEN] = {XCOMP,YCOMP,ZCOMP,MAXVECT};
scohennm 4:89ae38dc05a9 69 float accaxisdata[NUMSTATES];
scohennm 4:89ae38dc05a9 70 char displayformats[NUMSTATES][LCDLEN] = {"x.%3.2f","y.%3.2f","z.%3.2f","v.%3.2f"};
scohennm 4:89ae38dc05a9 71 //char displayformats[NUMSTATES][LCDLEN] = {"%4.3f","%4.3f","%4.3f","%4.3f"};
scohennm 4:89ae38dc05a9 72
scohennm 4:89ae38dc05a9 73 void LCDMess(char *lMess, float dWait){
scohennm 4:89ae38dc05a9 74 slcd.Home();
scohennm 4:89ae38dc05a9 75 slcd.clear();
scohennm 4:89ae38dc05a9 76 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 77 wait(dWait);
scohennm 4:89ae38dc05a9 78 }
scohennm 4:89ae38dc05a9 79 void LCDMessNoDwell(char *lMess){
scohennm 4:89ae38dc05a9 80 slcd.Home();
scohennm 4:89ae38dc05a9 81 slcd.clear();
scohennm 4:89ae38dc05a9 82 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 83 }
scohennm 4:89ae38dc05a9 84
scohennm 4:89ae38dc05a9 85
scohennm 4:89ae38dc05a9 86 void allLEDsOFF(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 87 int i;
scohennm 4:89ae38dc05a9 88 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 89 LEDs[i] = LEDOFF;
scohennm 4:89ae38dc05a9 90 }
scohennm 4:89ae38dc05a9 91
scohennm 4:89ae38dc05a9 92 }
scohennm 4:89ae38dc05a9 93
scohennm 4:89ae38dc05a9 94 void runLEDs(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 95 int i;
scohennm 4:89ae38dc05a9 96 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 97 LEDs[i] = LEDON;
scohennm 4:89ae38dc05a9 98 wait(0.2);
scohennm 4:89ae38dc05a9 99 }
scohennm 4:89ae38dc05a9 100 allLEDsOFF(numberOfLEDS);
scohennm 4:89ae38dc05a9 101 }
scohennm 4:89ae38dc05a9 102
scohennm 4:89ae38dc05a9 103
scohennm 4:89ae38dc05a9 104
scohennm 4:89ae38dc05a9 105 int main() {
scohennm 4:89ae38dc05a9 106
scohennm 4:89ae38dc05a9 107 int RButtonState;
scohennm 4:89ae38dc05a9 108 int LButtonState;
scohennm 4:89ae38dc05a9 109 DigitalIn RtButton(PTC12);
scohennm 4:89ae38dc05a9 110 DigitalIn LftButton(PTC3);
scohennm 4:89ae38dc05a9 111 int displayState = XCOMPD;
scohennm 4:89ae38dc05a9 112 float xAcc = 0.0;
scohennm 4:89ae38dc05a9 113 float yAcc = 0.0;
scohennm 4:89ae38dc05a9 114 float zAcc = 0.0;
scohennm 4:89ae38dc05a9 115 float vector;
scohennm 4:89ae38dc05a9 116 float vMax = 0.0;
scohennm 4:89ae38dc05a9 117 float DisplayTime = DATADISPDWELL;
scohennm 4:89ae38dc05a9 118 float LEDDwell = BLINKTIME;
scohennm 4:89ae38dc05a9 119 int outState = false;
scohennm 4:89ae38dc05a9 120 char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon
scohennm 4:89ae38dc05a9 121 uint8_t regData = MAX_4G; // test value must change after G setting
scohennm 4:89ae38dc05a9 122
scohennm 4:89ae38dc05a9 123 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 124 pc.printf(PROGNAME);
scohennm 4:89ae38dc05a9 125 #endif
scohennm 4:89ae38dc05a9 126 LCDMess(LCDNAME, BLINKTIME);
scohennm 4:89ae38dc05a9 127 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 4:89ae38dc05a9 128 allLEDsOFF(NUMLEDS);
scohennm 4:89ae38dc05a9 129 blinkTimer.start();
scohennm 4:89ae38dc05a9 130 blinkTimer.reset();
scohennm 4:89ae38dc05a9 131 displayTimer.start();
scohennm 4:89ae38dc05a9 132 displayTimer.reset();
scohennm 4:89ae38dc05a9 133 dataTimer.start();
scohennm 4:89ae38dc05a9 134 dataTimer.reset();
scohennm 4:89ae38dc05a9 135 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:89ae38dc05a9 136 // Set 8 g max limit
scohennm 4:89ae38dc05a9 137 acc.setGLimit(MAX_8G); // For now set to 8g
scohennm 4:89ae38dc05a9 138 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 4:89ae38dc05a9 139 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 140 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 141 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 4:89ae38dc05a9 142 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 143 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 144 // main loop forever
scohennm 4:89ae38dc05a9 145 while(true) {
scohennm 4:89ae38dc05a9 146
scohennm 4:89ae38dc05a9 147 // Handle user input for display selections
scohennm 4:89ae38dc05a9 148 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 4:89ae38dc05a9 149 if (RButtonState){
scohennm 4:89ae38dc05a9 150 vMax = 0.0; // Clear vMax
scohennm 4:89ae38dc05a9 151 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:89ae38dc05a9 152 }
scohennm 4:89ae38dc05a9 153 LButtonState = !LftButton;
scohennm 4:89ae38dc05a9 154 if (LButtonState) { //Change data that is displayed
scohennm 4:89ae38dc05a9 155 displayState = (displayState + 1) % NUMSTATES;
scohennm 4:89ae38dc05a9 156 // Change to switch/case soon.
scohennm 4:89ae38dc05a9 157 LCDMess(displayTitles[displayState],BLINKTIME);
scohennm 4:89ae38dc05a9 158 }
scohennm 4:89ae38dc05a9 159
scohennm 4:89ae38dc05a9 160 // --------------------------------------------
scohennm 4:89ae38dc05a9 161 while (dataTimer.read() > DATATIME){
scohennm 4:89ae38dc05a9 162 //Get accelerometer data - tilt angles minus offset for zero mark.
scohennm 4:89ae38dc05a9 163 // No offset
scohennm 4:89ae38dc05a9 164 xAcc = abs(acc.getAccX());
scohennm 4:89ae38dc05a9 165 yAcc = abs(acc.getAccY());
scohennm 4:89ae38dc05a9 166 zAcc = abs(acc.getAccZ());
scohennm 4:89ae38dc05a9 167 // Calulate vector sum of x,y and z reading.
scohennm 4:89ae38dc05a9 168 vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 4:89ae38dc05a9 169 vector = zAcc;
scohennm 4:89ae38dc05a9 170 if (vector > vMax) {
scohennm 4:89ae38dc05a9 171 vMax = vector;
scohennm 4:89ae38dc05a9 172 }
scohennm 4:89ae38dc05a9 173 accaxisdata[XCOMPD] = xAcc;
scohennm 4:89ae38dc05a9 174 accaxisdata[YCOMPD] = yAcc;
scohennm 4:89ae38dc05a9 175 accaxisdata[ZCOMPD] = zAcc;
scohennm 4:89ae38dc05a9 176 accaxisdata[VMAXD] = vMax;
scohennm 4:89ae38dc05a9 177 dataTimer.reset();
scohennm 4:89ae38dc05a9 178 LEDDwell = 1.1 - vMax/MAXGS;
scohennm 4:89ae38dc05a9 179 }
scohennm 4:89ae38dc05a9 180 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 181 pc.printf("vector = %f\r\n", xAcc);
scohennm 4:89ae38dc05a9 182 pc.printf("scaling = %f\r\n", scaleExpansion);
scohennm 4:89ae38dc05a9 183 pc.printf("RawTemp = %f\r\n", FDeg);
scohennm 4:89ae38dc05a9 184 #endif
scohennm 4:89ae38dc05a9 185 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 4:89ae38dc05a9 186 while (displayTimer.read() > DisplayTime){
scohennm 4:89ae38dc05a9 187 sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]);
scohennm 4:89ae38dc05a9 188 LCDMessNoDwell(lcdData);
scohennm 4:89ae38dc05a9 189 displayTimer.reset();
scohennm 4:89ae38dc05a9 190 } // displaytimer
scohennm 4:89ae38dc05a9 191 // Wait then do the whole thing again.
scohennm 4:89ae38dc05a9 192 // Alive LEDs
scohennm 4:89ae38dc05a9 193 while(blinkTimer.read()> LEDDwell) {
scohennm 4:89ae38dc05a9 194 LEDs[0].write(outState);
scohennm 4:89ae38dc05a9 195 LEDs[1].write(!outState);
scohennm 4:89ae38dc05a9 196 outState = !outState;
scohennm 4:89ae38dc05a9 197 blinkTimer.reset();
scohennm 4:89ae38dc05a9 198 }
scohennm 4:89ae38dc05a9 199 }//forever loop
scohennm 4:89ae38dc05a9 200 }// main