KBrat-SSD541-Midterm-Q2b

Dependencies:   SLCD TSI mbed

Fork of KBrat-SSD541-Midterm-Q2b by Keisha Brathwaite

Committer:
tisbrat
Date:
Mon Oct 10 05:57:45 2016 +0000
Revision:
2:6d82d759361f
Parent:
1:44dcf262c7dd
KBrat-SSD541-Midterm-Q2b

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:04499bc54bee 1 #include "mbed.h"
tisbrat 2:6d82d759361f 2 #include <math.h>
tisbrat 2:6d82d759361f 3 #include <cmath>
scohennm 0:04499bc54bee 4 #include "TSISensor.h"
scohennm 0:04499bc54bee 5 #include "SLCD.h"
scohennm 1:44dcf262c7dd 6 #define LEDON false
scohennm 1:44dcf262c7dd 7 #define LEDOFF true
tisbrat 2:6d82d759361f 8 #define NUMBUTS 2 //two buttons
tisbrat 2:6d82d759361f 9 #define LBUT PTC12 //left button // port addresses for buttons
tisbrat 2:6d82d759361f 10 #define RBUT PTC3 //right button
tisbrat 2:6d82d759361f 11 #define ARGUMENTSTATE 0 //Switch case 0
tisbrat 2:6d82d759361f 12 #define ANSWERSTATE 1 //Switch case 1
tisbrat 2:6d82d759361f 13 #define LCDTIME 1.0 //LCD Timer 1 sec
scohennm 1:44dcf262c7dd 14 #define TSILIMIT 0.01
scohennm 1:44dcf262c7dd 15 #define PRINTDELTA 0.01
scohennm 0:04499bc54bee 16 #define LCDCHARLEN 10
scohennm 0:04499bc54bee 17 #define DATAINTERVAL 0.1
scohennm 1:44dcf262c7dd 18 #define BUTTONTIME 0.1
tisbrat 2:6d82d759361f 19 #define PROGNAME "KBrat-SSD541-Midterm-Q2b \nCubic Root\n\r"
scohennm 0:04499bc54bee 20
tisbrat 2:6d82d759361f 21 SLCD slcd; //define LCD display globally define
scohennm 0:04499bc54bee 22 Serial pc(USBTX, USBRX);
scohennm 0:04499bc54bee 23
tisbrat 2:6d82d759361f 24 Timer LCDTimer; //for reading lcd input state for display
tisbrat 2:6d82d759361f 25 Timer dataTimer; //for reading data input states(from the slider 1-100)
tisbrat 2:6d82d759361f 26 Timer ButtonTimer; //for reading button states
scohennm 1:44dcf262c7dd 27 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
scohennm 0:04499bc54bee 28 float tsidata;
tisbrat 2:6d82d759361f 29 //int displayState;
tisbrat 2:6d82d759361f 30 int displayState = ARGUMENTSTATE;//Make initial state ARGUMENTSTATE
scohennm 1:44dcf262c7dd 31
scohennm 1:44dcf262c7dd 32 void initialize_global_vars(){
scohennm 1:44dcf262c7dd 33 pc.printf(PROGNAME);
scohennm 1:44dcf262c7dd 34 // set up DAQ timers
scohennm 1:44dcf262c7dd 35 ButtonTimer.start();
scohennm 1:44dcf262c7dd 36 ButtonTimer.reset();
scohennm 1:44dcf262c7dd 37 dataTimer.start();
tisbrat 2:6d82d759361f 38 dataTimer.reset();
tisbrat 2:6d82d759361f 39 LCDTimer.start();
tisbrat 2:6d82d759361f 40 LCDTimer.reset();
scohennm 1:44dcf262c7dd 41 }
scohennm 0:04499bc54bee 42
scohennm 0:04499bc54bee 43 void LCDMess(char *lMess){
scohennm 0:04499bc54bee 44 slcd.Home();
scohennm 0:04499bc54bee 45 slcd.clear();
scohennm 0:04499bc54bee 46 slcd.printf(lMess);
scohennm 0:04499bc54bee 47 }
scohennm 0:04499bc54bee 48
scohennm 0:04499bc54bee 49 int main(void) {
scohennm 1:44dcf262c7dd 50 int i;
scohennm 0:04499bc54bee 51 char lcdData[LCDCHARLEN];
tisbrat 2:6d82d759361f 52 float lastTouch = 0.0; //intial lastTouch starts at 0.0
scohennm 1:44dcf262c7dd 53 TSISensor tsi;
scohennm 1:44dcf262c7dd 54 float tempTSI;
scohennm 0:04499bc54bee 55 PwmOut gled(LED_GREEN);
scohennm 0:04499bc54bee 56 PwmOut rled(LED_RED);
scohennm 1:44dcf262c7dd 57
scohennm 1:44dcf262c7dd 58 initialize_global_vars();
scohennm 0:04499bc54bee 59
scohennm 0:04499bc54bee 60 while (true) {
scohennm 1:44dcf262c7dd 61 if (ButtonTimer > BUTTONTIME){
tisbrat 2:6d82d759361f 62 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 //find buttons
scohennm 1:44dcf262c7dd 63 if(!buttons[i]) {
scohennm 1:44dcf262c7dd 64 displayState = i;
scohennm 1:44dcf262c7dd 65 } // if ! buttons
scohennm 1:44dcf262c7dd 66 }// for loop to look at buttons
scohennm 1:44dcf262c7dd 67 ButtonTimer.reset();
tisbrat 2:6d82d759361f 68 }
tisbrat 2:6d82d759361f 69
tisbrat 2:6d82d759361f 70
tisbrat 2:6d82d759361f 71 if(LCDTimer.read() > LCDTIME){
tisbrat 2:6d82d759361f 72 LCDTimer.reset();
tisbrat 2:6d82d759361f 73 switch (displayState){ //start switch case for displayState
tisbrat 2:6d82d759361f 74
tisbrat 2:6d82d759361f 75 case ARGUMENTSTATE: { // case #0
tisbrat 2:6d82d759361f 76 rled = 0.0;//red light on
tisbrat 2:6d82d759361f 77 gled = 1.0;//green light off
tisbrat 2:6d82d759361f 78
tisbrat 2:6d82d759361f 79 if(dataTimer.read() > DATAINTERVAL){
tisbrat 2:6d82d759361f 80 dataTimer.reset();
tisbrat 2:6d82d759361f 81 tempTSI = tsi.readPercentage();
tisbrat 2:6d82d759361f 82 if (tempTSI > TSILIMIT){
tisbrat 2:6d82d759361f 83 tsidata = tempTSI;
tisbrat 2:6d82d759361f 84 if (fabs(tsidata - lastTouch)> PRINTDELTA){
tisbrat 2:6d82d759361f 85 //int randomNum = rand() % 101 + (-50);
tisbrat 2:6d82d759361f 86 //tsidata = tsidata*randomNum;
tisbrat 2:6d82d759361f 87 //if(tsidata < 0.0)
tisbrat 2:6d82d759361f 88 //pc.printf("Position z%2.1f", fabs(tsidata));
tisbrat 2:6d82d759361f 89 pc.printf("Position %2.0f\n\r", tsidata*50); //print to computer tsidata*50 to get a range from 1-50
tisbrat 2:6d82d759361f 90
tisbrat 2:6d82d759361f 91 }
tisbrat 2:6d82d759361f 92 }
tisbrat 2:6d82d759361f 93 lastTouch=tsidata;
tisbrat 2:6d82d759361f 94 }
tisbrat 2:6d82d759361f 95
tisbrat 2:6d82d759361f 96 sprintf (lcdData,"%2.1f",tsidata*50); //print to lcd screen tsidata*50 to get a range from 1-50
tisbrat 2:6d82d759361f 97 LCDMess(lcdData);
tisbrat 2:6d82d759361f 98
tisbrat 2:6d82d759361f 99 /*sprintf (lcdData,"%2.1f",tsidata*50)&
tisbrat 2:6d82d759361f 100 sprintf (lcdData,"%2.1f",tsidata*(0-50));
tisbrat 2:6d82d759361f 101 if(tsidata < 0.0) sprint (lcdData,"z%2.1f", fabs(tsidata*(0-50));
tisbrat 2:6d82d759361f 102 LCDMess(lcdData);*/
tisbrat 2:6d82d759361f 103
tisbrat 2:6d82d759361f 104 break;
tisbrat 2:6d82d759361f 105 }
tisbrat 2:6d82d759361f 106
tisbrat 2:6d82d759361f 107 case ANSWERSTATE: {
tisbrat 2:6d82d759361f 108
tisbrat 2:6d82d759361f 109 rled = 1.0;//red light off
tisbrat 2:6d82d759361f 110 gled = 0.0;//green light on
tisbrat 2:6d82d759361f 111
tisbrat 2:6d82d759361f 112 double slid_input, cub_root;
tisbrat 2:6d82d759361f 113 slid_input = tsidata*50;
tisbrat 2:6d82d759361f 114 if (slid_input > 0){
tisbrat 2:6d82d759361f 115 cub_root = cbrt(slid_input);//built-in cubic root function
tisbrat 2:6d82d759361f 116 pc.printf ("Cubic root(%2.2f) = %2.2f\n", slid_input, cub_root);
tisbrat 2:6d82d759361f 117 sprintf (lcdData,"%2.2f", cub_root);
tisbrat 2:6d82d759361f 118 }
tisbrat 2:6d82d759361f 119 break;
tisbrat 2:6d82d759361f 120 }// end switch displaystate
tisbrat 2:6d82d759361f 121 }
scohennm 1:44dcf262c7dd 122 LCDMess(lcdData);
tisbrat 2:6d82d759361f 123 } // end LCD timer.read
scohennm 1:44dcf262c7dd 124
tisbrat 2:6d82d759361f 125 }// end while(true)
tisbrat 2:6d82d759361f 126
scohennm 0:04499bc54bee 127 }