Part of Final Questions for SSD 341 NMNU Fall 2014

Dependencies:   MMA8451Q SLCD mbed

Fork of LCD_ACC_46_v3 by Stanley Cohen

Committer:
scohennm
Date:
Mon Dec 01 15:55:53 2014 +0000
Revision:
3:c575eee9fff2
Parent:
2:8cdbe8a96b59
Final Questions for SSD 341 NMNU Fall 2014

Who changed what in which revision?

UserRevisionLine numberNew 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 3:c575eee9fff2 6
scohennm 0:88ddbd76b455 7 */
scohennm 0:88ddbd76b455 8
scohennm 1:6c2ec7b0e1c9 9 #define BLINKTIME 0.7
scohennm 0:88ddbd76b455 10 #define DATATIME 0.1
scohennm 1:6c2ec7b0e1c9 11 #define DATADISPDWELL 0.2
scohennm 0:88ddbd76b455 12 #define NUMLEDS 2
scohennm 0:88ddbd76b455 13 #define LEDON 0
scohennm 0:88ddbd76b455 14 #define LEDOFF 1
scohennm 0:88ddbd76b455 15 #define SCALING 13
scohennm 0:88ddbd76b455 16 #define RSTARTMESS "RSET"
scohennm 0:88ddbd76b455 17 #define MAXVECT "MAXV"
scohennm 2:8cdbe8a96b59 18 #define XCOMP "XCMP"
scohennm 0:88ddbd76b455 19 #define YCOMP "YCMP"
scohennm 2:8cdbe8a96b59 20 #define ZCOMP "ZCMP"
scohennm 0:88ddbd76b455 21 #define ANALTOVOLTS 3.3
scohennm 0:88ddbd76b455 22 #define LEDDELAY 0.400
scohennm 2:8cdbe8a96b59 23 //define states
scohennm 2:8cdbe8a96b59 24 #define NUMSTATES 4
scohennm 2:8cdbe8a96b59 25 #define XCOMPD 0
scohennm 2:8cdbe8a96b59 26 #define YCOMPD 1
scohennm 2:8cdbe8a96b59 27 #define ZCOMPD 2
scohennm 2:8cdbe8a96b59 28 #define VMAXD 3
scohennm 0:88ddbd76b455 29
scohennm 0:88ddbd76b455 30 #define PROGNAME "LCD_ACC_LCDv3 46\r/n"
scohennm 0:88ddbd76b455 31
scohennm 0:88ddbd76b455 32 //#define PRINTDBUG
scohennm 0:88ddbd76b455 33 // Accelerometer SPI pins
scohennm 0:88ddbd76b455 34 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 0:88ddbd76b455 35 PinName const SDA = PTE25;
scohennm 0:88ddbd76b455 36 PinName const SCL = PTE24;
scohennm 0:88ddbd76b455 37 #elif defined (TARGET_KL05Z)
scohennm 0:88ddbd76b455 38 PinName const SDA = PTB4;
scohennm 0:88ddbd76b455 39 PinName const SCL = PTB3;
scohennm 0:88ddbd76b455 40 #else
scohennm 0:88ddbd76b455 41 #error TARGET NOT DEFINED
scohennm 0:88ddbd76b455 42 #endif
scohennm 0:88ddbd76b455 43
scohennm 0:88ddbd76b455 44 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 0:88ddbd76b455 45
scohennm 0:88ddbd76b455 46 SLCD slcd; //define LCD display
scohennm 0:88ddbd76b455 47 Timer blinkTimer;
scohennm 0:88ddbd76b455 48 Timer dataTimer;
scohennm 0:88ddbd76b455 49 Timer displayTimer;
scohennm 0:88ddbd76b455 50 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 0:88ddbd76b455 51 AnalogIn TMP01(PTB1);
scohennm 0:88ddbd76b455 52 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
scohennm 0:88ddbd76b455 53 Serial pc(USBTX, USBRX);
scohennm 0:88ddbd76b455 54
scohennm 0:88ddbd76b455 55
scohennm 0:88ddbd76b455 56 void LCDMess(char *lMess, float dWait){
scohennm 0:88ddbd76b455 57 slcd.Home();
scohennm 0:88ddbd76b455 58 slcd.clear();
scohennm 0:88ddbd76b455 59 slcd.printf(lMess);
scohennm 0:88ddbd76b455 60 wait(dWait);
scohennm 0:88ddbd76b455 61 }
scohennm 0:88ddbd76b455 62 void LCDMessNoDwell(char *lMess){
scohennm 0:88ddbd76b455 63 slcd.Home();
scohennm 0:88ddbd76b455 64 slcd.clear();
scohennm 0:88ddbd76b455 65 slcd.printf(lMess);
scohennm 0:88ddbd76b455 66 }
scohennm 0:88ddbd76b455 67
scohennm 2:8cdbe8a96b59 68
scohennm 0:88ddbd76b455 69 void allLEDsOFF(int numberOfLEDS) {
scohennm 0:88ddbd76b455 70 int i;
scohennm 0:88ddbd76b455 71 for (i=0;i<numberOfLEDS; i++){
scohennm 0:88ddbd76b455 72 LEDs[i] = LEDOFF;
scohennm 0:88ddbd76b455 73 }
scohennm 0:88ddbd76b455 74
scohennm 0:88ddbd76b455 75 }
scohennm 0:88ddbd76b455 76
scohennm 0:88ddbd76b455 77
scohennm 0:88ddbd76b455 78
scohennm 0:88ddbd76b455 79 int main() {
scohennm 0:88ddbd76b455 80
scohennm 0:88ddbd76b455 81 int RButtonState;
scohennm 0:88ddbd76b455 82 int LButtonState;
scohennm 0:88ddbd76b455 83 DigitalIn RtButton(PTC12);
scohennm 0:88ddbd76b455 84 DigitalIn LftButton(PTC3);
scohennm 2:8cdbe8a96b59 85 int displayState = XCOMPD;
scohennm 1:6c2ec7b0e1c9 86 float xAcc = 0.0;
scohennm 1:6c2ec7b0e1c9 87 float yAcc = 0.0;
scohennm 2:8cdbe8a96b59 88 float zAcc = 0.0;
scohennm 0:88ddbd76b455 89 float vector;
scohennm 0:88ddbd76b455 90 float vMax = 0.0;
scohennm 1:6c2ec7b0e1c9 91 float DisplayTime = DATADISPDWELL;
scohennm 0:88ddbd76b455 92 int outState = false;
scohennm 3:c575eee9fff2 93 char lcdData[10]; //buffer needs places for decimal pt and colon
scohennm 0:88ddbd76b455 94
scohennm 0:88ddbd76b455 95
scohennm 0:88ddbd76b455 96 #ifdef PRINTDBUG
scohennm 0:88ddbd76b455 97 pc.printf(PROGNAME);
scohennm 0:88ddbd76b455 98 #endif
scohennm 2:8cdbe8a96b59 99 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 0:88ddbd76b455 100 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 0:88ddbd76b455 101 allLEDsOFF(NUMLEDS);
scohennm 0:88ddbd76b455 102 blinkTimer.start();
scohennm 0:88ddbd76b455 103 blinkTimer.reset();
scohennm 1:6c2ec7b0e1c9 104 displayTimer.start();
scohennm 1:6c2ec7b0e1c9 105 displayTimer.reset();
scohennm 1:6c2ec7b0e1c9 106 dataTimer.start();
scohennm 1:6c2ec7b0e1c9 107 dataTimer.reset();
scohennm 0:88ddbd76b455 108
scohennm 0:88ddbd76b455 109 // main loop forever
scohennm 0:88ddbd76b455 110 while(true) {
scohennm 2:8cdbe8a96b59 111
scohennm 0:88ddbd76b455 112 // Handle user input for display selections
scohennm 0:88ddbd76b455 113 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 0:88ddbd76b455 114 if (RButtonState){
scohennm 0:88ddbd76b455 115 vMax = 0.0; // Clear vMax
scohennm 2:8cdbe8a96b59 116 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 0:88ddbd76b455 117 }
scohennm 0:88ddbd76b455 118 LButtonState = !LftButton;
scohennm 0:88ddbd76b455 119 if (LButtonState) { //Change data that is displayed
scohennm 2:8cdbe8a96b59 120 displayState = (displayState + 1) % NUMSTATES;
scohennm 0:88ddbd76b455 121 // Change to switch/case soon.
scohennm 2:8cdbe8a96b59 122 switch (displayState){
scohennm 2:8cdbe8a96b59 123 case XCOMPD: {
scohennm 2:8cdbe8a96b59 124 LCDMess(XCOMP,BLINKTIME);
scohennm 2:8cdbe8a96b59 125 break;
scohennm 2:8cdbe8a96b59 126 }
scohennm 2:8cdbe8a96b59 127 case YCOMPD: {
scohennm 2:8cdbe8a96b59 128 LCDMess(YCOMP,BLINKTIME);
scohennm 2:8cdbe8a96b59 129 break;
scohennm 2:8cdbe8a96b59 130 }
scohennm 2:8cdbe8a96b59 131 case ZCOMPD: {
scohennm 2:8cdbe8a96b59 132 LCDMess(ZCOMP,BLINKTIME);
scohennm 2:8cdbe8a96b59 133 break;
scohennm 2:8cdbe8a96b59 134 }
scohennm 2:8cdbe8a96b59 135 case VMAXD:{
scohennm 2:8cdbe8a96b59 136 LCDMess(MAXVECT,BLINKTIME);
scohennm 2:8cdbe8a96b59 137 break;
scohennm 2:8cdbe8a96b59 138 }
scohennm 2:8cdbe8a96b59 139 }// switch
scohennm 0:88ddbd76b455 140 }
scohennm 1:6c2ec7b0e1c9 141
scohennm 1:6c2ec7b0e1c9 142 // --------------------------------------------
scohennm 1:6c2ec7b0e1c9 143 while (dataTimer.read() > DATATIME){
scohennm 0:88ddbd76b455 144 // No offset
scohennm 0:88ddbd76b455 145 xAcc = abs(acc.getAccX());
scohennm 0:88ddbd76b455 146 yAcc = abs(acc.getAccY());
scohennm 2:8cdbe8a96b59 147 zAcc = abs(acc.getAccZ());
scohennm 0:88ddbd76b455 148 // Calulate vector sum of x,y and z reading.
scohennm 2:8cdbe8a96b59 149 vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 0:88ddbd76b455 150 if (vector > vMax) {
scohennm 0:88ddbd76b455 151 vMax = vector;
scohennm 0:88ddbd76b455 152 }
scohennm 1:6c2ec7b0e1c9 153 dataTimer.reset();
scohennm 1:6c2ec7b0e1c9 154 }
scohennm 3:c575eee9fff2 155
scohennm 0:88ddbd76b455 156 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 1:6c2ec7b0e1c9 157 while (displayTimer.read() > DisplayTime){
scohennm 2:8cdbe8a96b59 158 switch (displayState) {
scohennm 2:8cdbe8a96b59 159 case XCOMPD: {
scohennm 2:8cdbe8a96b59 160 sprintf (lcdData,"%4.3f",xAcc);
scohennm 2:8cdbe8a96b59 161 break;
scohennm 2:8cdbe8a96b59 162 }
scohennm 2:8cdbe8a96b59 163 case YCOMPD: {
scohennm 2:8cdbe8a96b59 164 sprintf (lcdData,"%4.3f",yAcc);
scohennm 2:8cdbe8a96b59 165 break;
scohennm 2:8cdbe8a96b59 166 }
scohennm 2:8cdbe8a96b59 167 case ZCOMPD: {
scohennm 2:8cdbe8a96b59 168 sprintf (lcdData,"%4.3f",zAcc);
scohennm 2:8cdbe8a96b59 169 break;
scohennm 2:8cdbe8a96b59 170 }
scohennm 2:8cdbe8a96b59 171 case VMAXD:{
scohennm 2:8cdbe8a96b59 172 sprintf (lcdData,"%4.3f",vMax);
scohennm 2:8cdbe8a96b59 173 break;
scohennm 2:8cdbe8a96b59 174 }
scohennm 0:88ddbd76b455 175 }
scohennm 1:6c2ec7b0e1c9 176 LCDMessNoDwell(lcdData);
scohennm 1:6c2ec7b0e1c9 177 displayTimer.reset();
scohennm 1:6c2ec7b0e1c9 178 } // displaytimer
scohennm 2:8cdbe8a96b59 179 // Wait then do the whole thing again.
scohennm 2:8cdbe8a96b59 180 // Alive LEDs
scohennm 2:8cdbe8a96b59 181 while(blinkTimer.read()> LEDDELAY) {
scohennm 2:8cdbe8a96b59 182 LEDs[0].write(outState);
scohennm 2:8cdbe8a96b59 183 LEDs[1].write(!outState);
scohennm 2:8cdbe8a96b59 184 outState = !outState;
scohennm 2:8cdbe8a96b59 185 blinkTimer.reset();
scohennm 2:8cdbe8a96b59 186 }
scohennm 0:88ddbd76b455 187 }//forever loop
scohennm 0:88ddbd76b455 188 }// main