Basic program for HW

Dependencies:   MMA8451Q SLCD mbed

Fork of SSD_341_535_finalv3 by Stanley Cohen

Committer:
scohennm
Date:
Fri Feb 10 19:28:50 2017 +0000
Revision:
4:41920817d234
Using base accelerometer library

Who changed what in which revision?

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