Final Project DavidEstrada

Dependencies:   MMA8451Q SLCD TSI mbed

Fork of MIDTERM_v3_Accel_withFloats_essentials by David Estrada

Committer:
destradafilm
Date:
Wed Dec 09 19:49:26 2015 +0000
Revision:
4:fd6afcd8b705
Parent:
midterm_main.cpp@3:c6e262633952
David Estrada_Algorithms_; Final project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
destradafilm 3:c6e262633952 1 #include "mbed.h"
destradafilm 3:c6e262633952 2 #include "MMA8451Q.h"
destradafilm 3:c6e262633952 3 #include "SLCD.h"
destradafilm 4:fd6afcd8b705 4 #include "TSISensor.h"
destradafilm 3:c6e262633952 5
destradafilm 3:c6e262633952 6 // delays
destradafilm 3:c6e262633952 7 #define BLINKTIME 0.7
destradafilm 3:c6e262633952 8 #define DATATIME 0.1
destradafilm 3:c6e262633952 9 #define LEDDELAY 0.4
destradafilm 3:c6e262633952 10 #define DATADISPDWELL 0.2
destradafilm 3:c6e262633952 11
destradafilm 3:c6e262633952 12 //define states
destradafilm 3:c6e262633952 13 #define NUMSTATES 3
destradafilm 3:c6e262633952 14 #define XACTIVE 0
destradafilm 3:c6e262633952 15 #define YACTIVE 1
destradafilm 3:c6e262633952 16 #define ZACTIVE 2
destradafilm 3:c6e262633952 17
destradafilm 3:c6e262633952 18 // LEDS
destradafilm 3:c6e262633952 19 #define NUMLEDS 2
destradafilm 3:c6e262633952 20 #define LEDON 0
destradafilm 3:c6e262633952 21 #define LEDOFF 1
destradafilm 3:c6e262633952 22
destradafilm 3:c6e262633952 23 // LCD messages
destradafilm 3:c6e262633952 24 #define PROGNAME "DEstra_Midterm_XYZ to RGB\r/n"
destradafilm 3:c6e262633952 25 #define LCDSTRT1 "ACC"
destradafilm 3:c6e262633952 26 #define LCDSTRT2 "TO"
destradafilm 3:c6e262633952 27 #define LCDSTRT3 "COLR"
destradafilm 3:c6e262633952 28 #define REDMESS "RED"
destradafilm 3:c6e262633952 29 #define GRNMESS "GREN"
destradafilm 3:c6e262633952 30 #define BLUMESS "BLUE"
destradafilm 3:c6e262633952 31
destradafilm 4:fd6afcd8b705 32 //TOTAL IMAGES
destradafilm 4:fd6afcd8b705 33 #define TOTALIMAGES 4
destradafilm 4:fd6afcd8b705 34
destradafilm 3:c6e262633952 35
destradafilm 3:c6e262633952 36 //#define PRINTDBUG
destradafilm 3:c6e262633952 37 // Accelerometer SPI pins
destradafilm 3:c6e262633952 38 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
destradafilm 3:c6e262633952 39 PinName const SDA = PTE25;
destradafilm 3:c6e262633952 40 PinName const SCL = PTE24;
destradafilm 3:c6e262633952 41 #elif defined (TARGET_KL05Z)
destradafilm 3:c6e262633952 42 PinName const SDA = PTB4;
destradafilm 3:c6e262633952 43 PinName const SCL = PTB3;
destradafilm 3:c6e262633952 44 #else
destradafilm 3:c6e262633952 45 #error TARGET NOT DEFINED
destradafilm 3:c6e262633952 46 #endif
destradafilm 3:c6e262633952 47
destradafilm 3:c6e262633952 48 #define MMA8451_I2C_ADDRESS (0x1d<<1)
destradafilm 3:c6e262633952 49
destradafilm 4:fd6afcd8b705 50 TSISensor tsiScaling; // Capacitive sensor/slider
destradafilm 4:fd6afcd8b705 51
destradafilm 3:c6e262633952 52 SLCD slcd; //define LCD display
destradafilm 3:c6e262633952 53
destradafilm 3:c6e262633952 54 // Timers
destradafilm 3:c6e262633952 55 Timer blinkTimer;
destradafilm 3:c6e262633952 56 Timer dataTimer;
destradafilm 3:c6e262633952 57 Timer displayTimer;
destradafilm 3:c6e262633952 58
destradafilm 3:c6e262633952 59 //------------------
destradafilm 3:c6e262633952 60 // KL46Z INTERFACE
destradafilm 3:c6e262633952 61
destradafilm 3:c6e262633952 62 // Accelerometer
destradafilm 3:c6e262633952 63 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
destradafilm 3:c6e262633952 64 AnalogIn TMP01(PTB1);
destradafilm 3:c6e262633952 65
destradafilm 3:c6e262633952 66 // LED PINS
destradafilm 3:c6e262633952 67 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
destradafilm 3:c6e262633952 68
destradafilm 3:c6e262633952 69 // SERIAL OUT TO PC
destradafilm 3:c6e262633952 70 Serial pc(USBTX, USBRX);
destradafilm 3:c6e262633952 71
destradafilm 3:c6e262633952 72
destradafilm 3:c6e262633952 73 // LCD MESSAGE
destradafilm 3:c6e262633952 74 void LCDMess(char *lMess, float dWait){
destradafilm 3:c6e262633952 75 slcd.Home();
destradafilm 3:c6e262633952 76 slcd.clear();
destradafilm 3:c6e262633952 77 slcd.printf(lMess);
destradafilm 3:c6e262633952 78 wait(dWait);
destradafilm 3:c6e262633952 79 }
destradafilm 3:c6e262633952 80 void LCDMessNoDwell(char *lMess){
destradafilm 3:c6e262633952 81 slcd.Home();
destradafilm 3:c6e262633952 82 slcd.clear();
destradafilm 3:c6e262633952 83 slcd.printf(lMess);
destradafilm 3:c6e262633952 84 }
destradafilm 3:c6e262633952 85
destradafilm 3:c6e262633952 86
destradafilm 3:c6e262633952 87 // RESET LED BLINK
destradafilm 3:c6e262633952 88 void allLEDsOFF(int numberOfLEDS) {
destradafilm 3:c6e262633952 89 int i;
destradafilm 3:c6e262633952 90 for (i=0;i<numberOfLEDS; i++){
destradafilm 3:c6e262633952 91 LEDs[i] = LEDOFF;
destradafilm 3:c6e262633952 92 }
destradafilm 3:c6e262633952 93
destradafilm 3:c6e262633952 94 }
destradafilm 3:c6e262633952 95
destradafilm 3:c6e262633952 96 // TURN CURRENT LED ON
destradafilm 3:c6e262633952 97 void runLEDs(int numberOfLEDS) {
destradafilm 3:c6e262633952 98 int i;
destradafilm 3:c6e262633952 99 for (i=0;i<numberOfLEDS; i++){
destradafilm 3:c6e262633952 100 LEDs[i] = LEDON;
destradafilm 3:c6e262633952 101 wait(0.2);
destradafilm 3:c6e262633952 102 }
destradafilm 3:c6e262633952 103 allLEDsOFF(numberOfLEDS);
destradafilm 3:c6e262633952 104 }
destradafilm 3:c6e262633952 105
destradafilm 3:c6e262633952 106
destradafilm 3:c6e262633952 107
destradafilm 3:c6e262633952 108 int main() {
destradafilm 3:c6e262633952 109
destradafilm 3:c6e262633952 110 int RButtonState;
destradafilm 3:c6e262633952 111 int LButtonState;
destradafilm 4:fd6afcd8b705 112 DigitalIn RtButton(PTC3);
destradafilm 4:fd6afcd8b705 113 DigitalIn LftButton(PTC12);
destradafilm 3:c6e262633952 114
destradafilm 3:c6e262633952 115
destradafilm 3:c6e262633952 116 int displayState = XACTIVE;
destradafilm 3:c6e262633952 117
destradafilm 4:fd6afcd8b705 118 // X Accelerometer ( 0 - 1.0)
destradafilm 3:c6e262633952 119 float xAcc = 0.0;
destradafilm 3:c6e262633952 120
destradafilm 4:fd6afcd8b705 121 // Slider Scale ( 0 - 1.0)
destradafilm 4:fd6afcd8b705 122 float sliderScale = 0.0;
destradafilm 4:fd6afcd8b705 123
destradafilm 4:fd6afcd8b705 124 int currentImage = 0;
destradafilm 3:c6e262633952 125
destradafilm 3:c6e262633952 126 float DisplayTime = DATADISPDWELL;
destradafilm 3:c6e262633952 127 int outState = false;
destradafilm 3:c6e262633952 128 char lcdData[10]; //buffer needs places for decimal pt and colon
destradafilm 3:c6e262633952 129
destradafilm 3:c6e262633952 130
destradafilm 3:c6e262633952 131 #ifdef PRINTDBUG
destradafilm 4:fd6afcd8b705 132 //pc.printf(PROGNAME);
destradafilm 3:c6e262633952 133 #endif
destradafilm 3:c6e262633952 134
destradafilm 3:c6e262633952 135 // Initialze readings and sequence the LED's for dramtic effect.
destradafilm 3:c6e262633952 136 allLEDsOFF(NUMLEDS);
destradafilm 3:c6e262633952 137 blinkTimer.start();
destradafilm 3:c6e262633952 138 blinkTimer.reset();
destradafilm 3:c6e262633952 139 displayTimer.start();
destradafilm 3:c6e262633952 140 displayTimer.reset();
destradafilm 3:c6e262633952 141 dataTimer.start();
destradafilm 3:c6e262633952 142 dataTimer.reset();
destradafilm 3:c6e262633952 143
destradafilm 4:fd6afcd8b705 144 //pc.printf(PROGNAME);
destradafilm 3:c6e262633952 145 LCDMess(LCDSTRT1, BLINKTIME);
destradafilm 3:c6e262633952 146 LCDMess(LCDSTRT2, BLINKTIME);
destradafilm 3:c6e262633952 147 LCDMess(LCDSTRT3, BLINKTIME);
destradafilm 3:c6e262633952 148
destradafilm 3:c6e262633952 149 // MAIN PROGRAM ///
destradafilm 3:c6e262633952 150 while(true) {
destradafilm 3:c6e262633952 151
destradafilm 3:c6e262633952 152 // --------------------------------------------
destradafilm 3:c6e262633952 153 /// LCD DISPLAY INFO - Handle user input for display selections
destradafilm 3:c6e262633952 154 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted
destradafilm 3:c6e262633952 155 if (RButtonState){
destradafilm 3:c6e262633952 156 LCDMess(LCDSTRT3, BLINKTIME);
destradafilm 4:fd6afcd8b705 157 currentImage ++;
destradafilm 4:fd6afcd8b705 158 //currentImage = abs(currentImage % TOTALIMAGES);
destradafilm 4:fd6afcd8b705 159
destradafilm 3:c6e262633952 160 }
destradafilm 3:c6e262633952 161 LButtonState = !LftButton;
destradafilm 3:c6e262633952 162 if (LButtonState) { //Change data that is displayed
destradafilm 3:c6e262633952 163 displayState = (displayState + 1) % NUMSTATES;
destradafilm 3:c6e262633952 164
destradafilm 4:fd6afcd8b705 165 currentImage --;
destradafilm 4:fd6afcd8b705 166 if(currentImage < 0){
destradafilm 4:fd6afcd8b705 167 currentImage = 0;
destradafilm 4:fd6afcd8b705 168 }
destradafilm 4:fd6afcd8b705 169 //currentImage = abs(currentImage % TOTALIMAGES);
destradafilm 4:fd6afcd8b705 170
destradafilm 3:c6e262633952 171 switch (displayState){
destradafilm 3:c6e262633952 172 case XACTIVE: {
destradafilm 3:c6e262633952 173 LCDMess(REDMESS,BLINKTIME);
destradafilm 3:c6e262633952 174 break;
destradafilm 3:c6e262633952 175 }
destradafilm 3:c6e262633952 176 case YACTIVE: {
destradafilm 3:c6e262633952 177 LCDMess(GRNMESS,BLINKTIME);
destradafilm 3:c6e262633952 178 break;
destradafilm 3:c6e262633952 179 }
destradafilm 3:c6e262633952 180 case ZACTIVE: {
destradafilm 3:c6e262633952 181 LCDMess(BLUMESS,BLINKTIME);
destradafilm 3:c6e262633952 182 break;
destradafilm 3:c6e262633952 183 }
destradafilm 3:c6e262633952 184 }// switch
destradafilm 3:c6e262633952 185 }
destradafilm 3:c6e262633952 186
destradafilm 3:c6e262633952 187
destradafilm 3:c6e262633952 188
destradafilm 3:c6e262633952 189
destradafilm 3:c6e262633952 190 // --------------------------------------------
destradafilm 3:c6e262633952 191 /// ACCELEROMETER DATA - TIMER
destradafilm 3:c6e262633952 192
destradafilm 3:c6e262633952 193 while (dataTimer.read() > DATATIME){ // DATATIME = 0.1 SECONDS
destradafilm 3:c6e262633952 194 //Get accelerometer data - tilt angles minus offset for zero mark.
destradafilm 3:c6e262633952 195 // No offset
destradafilm 3:c6e262633952 196
destradafilm 4:fd6afcd8b705 197 // get X
destradafilm 4:fd6afcd8b705 198 //xAcc = abs(acc.getAccX());
destradafilm 4:fd6afcd8b705 199 xAcc = abs(acc.getAccX() - 1.0);
destradafilm 3:c6e262633952 200
destradafilm 4:fd6afcd8b705 201 //Slider
destradafilm 4:fd6afcd8b705 202 sliderScale = tsiScaling.readPercentage();
destradafilm 4:fd6afcd8b705 203
destradafilm 3:c6e262633952 204
destradafilm 3:c6e262633952 205 // PRINT X Y Z in SERIAL PORT - INTERPRETED BY PYTHON PROGRAM
destradafilm 4:fd6afcd8b705 206
destradafilm 4:fd6afcd8b705 207 pc.printf("a%4.2f:b%4.1f:c%d\r\n", xAcc, sliderScale, currentImage);
destradafilm 3:c6e262633952 208
destradafilm 4:fd6afcd8b705 209 // verions 1
destradafilm 4:fd6afcd8b705 210 //pc.printf("s = %4.3f t = %4.1f i = %d\r\n", xAcc, sliderScale, currentImage);
destradafilm 4:fd6afcd8b705 211
destradafilm 3:c6e262633952 212 dataTimer.reset();
destradafilm 3:c6e262633952 213 }
destradafilm 3:c6e262633952 214 #ifdef PRINTDBUG
destradafilm 4:fd6afcd8b705 215 //pc.printf("debug r val = %f\r\n", rVal);
destradafilm 3:c6e262633952 216 #endif
destradafilm 3:c6e262633952 217
destradafilm 3:c6e262633952 218
destradafilm 3:c6e262633952 219 // --------------------------------------------
destradafilm 3:c6e262633952 220 /// LCD MESSAGE - TIMER
destradafilm 3:c6e262633952 221
destradafilm 3:c6e262633952 222 // Display the appropriate data on the LCD based upon what mode was chosen
destradafilm 3:c6e262633952 223 while (displayTimer.read() > DisplayTime){
destradafilm 3:c6e262633952 224 switch (displayState) {
destradafilm 3:c6e262633952 225 case XACTIVE: {
destradafilm 4:fd6afcd8b705 226 //sprintf (lcdData,"%d",rVal);
destradafilm 3:c6e262633952 227 break;
destradafilm 3:c6e262633952 228 }
destradafilm 3:c6e262633952 229 case YACTIVE: {
destradafilm 4:fd6afcd8b705 230 //sprintf (lcdData,"%d",gVal);
destradafilm 3:c6e262633952 231 break;
destradafilm 3:c6e262633952 232 }
destradafilm 3:c6e262633952 233 case ZACTIVE: {
destradafilm 4:fd6afcd8b705 234 //sprintf (lcdData,"%d",bVal);
destradafilm 3:c6e262633952 235 break;
destradafilm 3:c6e262633952 236 }
destradafilm 3:c6e262633952 237 }
destradafilm 3:c6e262633952 238 LCDMessNoDwell(lcdData);
destradafilm 3:c6e262633952 239 displayTimer.reset();
destradafilm 3:c6e262633952 240 } // displaytimer
destradafilm 3:c6e262633952 241
destradafilm 3:c6e262633952 242
destradafilm 3:c6e262633952 243 // --------------------------------------------
destradafilm 3:c6e262633952 244 // LED BLINK - TIMER
destradafilm 3:c6e262633952 245 while(blinkTimer.read()> LEDDELAY) {
destradafilm 3:c6e262633952 246
destradafilm 3:c6e262633952 247 switch (displayState){
destradafilm 3:c6e262633952 248 case XACTIVE: {
destradafilm 3:c6e262633952 249
destradafilm 3:c6e262633952 250 // RED BLINK
destradafilm 3:c6e262633952 251 LEDs[0].write(outState);
destradafilm 3:c6e262633952 252 LEDs[1].write(true);
destradafilm 3:c6e262633952 253 break;
destradafilm 3:c6e262633952 254 }
destradafilm 3:c6e262633952 255 case YACTIVE: {
destradafilm 3:c6e262633952 256
destradafilm 3:c6e262633952 257 // GREEN BLINK
destradafilm 3:c6e262633952 258 LEDs[0].write(true);
destradafilm 3:c6e262633952 259 LEDs[1].write(outState);
destradafilm 3:c6e262633952 260 break;
destradafilm 3:c6e262633952 261 }
destradafilm 3:c6e262633952 262 case ZACTIVE: {
destradafilm 3:c6e262633952 263
destradafilm 3:c6e262633952 264 // RED & GREEN BLINK
destradafilm 3:c6e262633952 265 LEDs[0].write(outState);
destradafilm 3:c6e262633952 266 LEDs[1].write(outState);
destradafilm 3:c6e262633952 267 break;
destradafilm 3:c6e262633952 268 }
destradafilm 3:c6e262633952 269 }// switch
destradafilm 3:c6e262633952 270
destradafilm 3:c6e262633952 271 // Change State - ON / OFF
destradafilm 3:c6e262633952 272 outState = !outState;
destradafilm 3:c6e262633952 273 blinkTimer.reset();
destradafilm 3:c6e262633952 274
destradafilm 3:c6e262633952 275 }
destradafilm 3:c6e262633952 276 }//forever loop
destradafilm 3:c6e262633952 277 }// main