Shane Barton / Mbed 2 deprecated sbart_recursion_count_v1

Dependencies:   SLCD TSI mbed

Fork of sbart_midterm_Q2 by Shane Barton

Committer:
sbart
Date:
Sun Nov 06 22:29:57 2016 +0000
Revision:
3:771f97716830
Parent:
2:6610fa50aa81
HW 12.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:04499bc54bee 1 #include "mbed.h"
scohennm 1:44dcf262c7dd 2 #include <math.h>
scohennm 0:04499bc54bee 3 #include "TSISensor.h"
scohennm 0:04499bc54bee 4 #include "SLCD.h"
scohennm 1:44dcf262c7dd 5
scohennm 1:44dcf262c7dd 6 #define LEDON false
scohennm 1:44dcf262c7dd 7 #define LEDOFF true
scohennm 1:44dcf262c7dd 8 #define NUMBUTS 2
scohennm 1:44dcf262c7dd 9 #define LBUT PTC12 // port addresses for buttons
scohennm 1:44dcf262c7dd 10 #define RBUT PTC3
scohennm 1:44dcf262c7dd 11 #define ARGUMENTSTATE 0
scohennm 1:44dcf262c7dd 12 #define ANSWERSTATE 1
scohennm 1:44dcf262c7dd 13 #define TSILIMIT 0.01
scohennm 1:44dcf262c7dd 14 #define PRINTDELTA 0.01
scohennm 0:04499bc54bee 15 #define LCDCHARLEN 10
scohennm 0:04499bc54bee 16 #define DATAINTERVAL 0.1
scohennm 1:44dcf262c7dd 17 #define BUTTONTIME 0.1
sbart 2:6610fa50aa81 18 #define LCDTIME 0.25
scohennm 1:44dcf262c7dd 19 #define PROGNAME "kl46z_slider_mid_v1\n\r"
scohennm 0:04499bc54bee 20
sbart 3:771f97716830 21 #define MIN_NUM -5
sbart 3:771f97716830 22 #define INCREMENT 2
sbart 3:771f97716830 23
sbart 3:771f97716830 24 #include <string>
sbart 3:771f97716830 25
sbart 3:771f97716830 26 //using namespace std;
sbart 3:771f97716830 27
scohennm 0:04499bc54bee 28 SLCD slcd; //define LCD display
scohennm 0:04499bc54bee 29 Serial pc(USBTX, USBRX);
scohennm 0:04499bc54bee 30
scohennm 1:44dcf262c7dd 31 Timer dataTimer;
scohennm 1:44dcf262c7dd 32 Timer ButtonTimer; // for reading button states
sbart 2:6610fa50aa81 33 Timer LCDTimer;
scohennm 1:44dcf262c7dd 34 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
scohennm 0:04499bc54bee 35 float tsidata;
scohennm 1:44dcf262c7dd 36 int displayState;
scohennm 1:44dcf262c7dd 37
scohennm 1:44dcf262c7dd 38 void initialize_global_vars(){
scohennm 1:44dcf262c7dd 39 pc.printf(PROGNAME);
scohennm 1:44dcf262c7dd 40 // set up DAQ timers
scohennm 1:44dcf262c7dd 41 ButtonTimer.start();
scohennm 1:44dcf262c7dd 42 ButtonTimer.reset();
scohennm 1:44dcf262c7dd 43 dataTimer.start();
scohennm 1:44dcf262c7dd 44 dataTimer.reset();
sbart 2:6610fa50aa81 45 LCDTimer.start();
sbart 2:6610fa50aa81 46 LCDTimer.reset();
sbart 2:6610fa50aa81 47 displayState = 3;
scohennm 1:44dcf262c7dd 48 }
scohennm 0:04499bc54bee 49
scohennm 0:04499bc54bee 50 void LCDMess(char *lMess){
scohennm 0:04499bc54bee 51 slcd.Home();
scohennm 0:04499bc54bee 52 slcd.clear();
scohennm 0:04499bc54bee 53 slcd.printf(lMess);
scohennm 0:04499bc54bee 54 }
scohennm 0:04499bc54bee 55
sbart 3:771f97716830 56 //float newtonSqrRt(float a) {
sbart 3:771f97716830 57 //
sbart 3:771f97716830 58 // float xnew = 0.0;
sbart 3:771f97716830 59 // int intmax = 20;
sbart 3:771f97716830 60 // //int n_int = 0;
sbart 3:771f97716830 61 // float epsilon = 1e-7;
sbart 3:771f97716830 62 // float xold = (a/2.5); //Make a guess
sbart 3:771f97716830 63 //
sbart 3:771f97716830 64 // for (int i = 0; i < intmax; i++)
sbart 3:771f97716830 65 // {
sbart 3:771f97716830 66 // xnew = 0.5 * (xold + (a/xold)); //Calculation
sbart 3:771f97716830 67 // float delta = fabs(xnew - xold); //Compare old and new values
sbart 3:771f97716830 68 //
sbart 3:771f97716830 69 // if (delta < epsilon)
sbart 3:771f97716830 70 // break;
sbart 3:771f97716830 71 // else
sbart 3:771f97716830 72 // xold = xnew;
sbart 3:771f97716830 73 // }
sbart 3:771f97716830 74 //
sbart 3:771f97716830 75 // return xnew;
sbart 3:771f97716830 76 //}
sbart 3:771f97716830 77
sbart 3:771f97716830 78 void backwardCount(float n, int delta, int minNum){
sbart 2:6610fa50aa81 79
sbart 3:771f97716830 80 pc.printf("current: %0f\n\r", n);
sbart 3:771f97716830 81
sbart 3:771f97716830 82 if(n <= minNum){
sbart 3:771f97716830 83 return;
sbart 3:771f97716830 84 } else {
sbart 3:771f97716830 85 backwardCount(n-delta, delta, minNum);
sbart 3:771f97716830 86 }
sbart 2:6610fa50aa81 87 }
sbart 2:6610fa50aa81 88
scohennm 0:04499bc54bee 89 int main(void) {
scohennm 1:44dcf262c7dd 90 int i;
scohennm 0:04499bc54bee 91 char lcdData[LCDCHARLEN];
scohennm 1:44dcf262c7dd 92 float lastTouch = 0.0;
scohennm 1:44dcf262c7dd 93 TSISensor tsi;
scohennm 1:44dcf262c7dd 94 float tempTSI;
scohennm 0:04499bc54bee 95 PwmOut gled(LED_GREEN);
scohennm 0:04499bc54bee 96 PwmOut rled(LED_RED);
sbart 3:771f97716830 97 pc.printf(PROGNAME);
scohennm 1:44dcf262c7dd 98
scohennm 1:44dcf262c7dd 99 initialize_global_vars();
scohennm 0:04499bc54bee 100
scohennm 0:04499bc54bee 101 while (true) {
scohennm 1:44dcf262c7dd 102 if (ButtonTimer > BUTTONTIME){
sbart 3:771f97716830 103 float count = 0;
scohennm 1:44dcf262c7dd 104 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
scohennm 1:44dcf262c7dd 105 if(!buttons[i]) {
scohennm 1:44dcf262c7dd 106 displayState = i;
scohennm 1:44dcf262c7dd 107 // do something here.
sbart 2:6610fa50aa81 108 switch (displayState) {
sbart 3:771f97716830 109 case ARGUMENTSTATE: //R BUTTON
sbart 3:771f97716830 110
sbart 3:771f97716830 111 count = tsidata * 100;
sbart 3:771f97716830 112 pc.printf("Position %0f\n\r", count);
sbart 2:6610fa50aa81 113 rled = 1.0;
sbart 2:6610fa50aa81 114 gled = 0.0;
sbart 2:6610fa50aa81 115
sbart 3:771f97716830 116 count = lastTouch;
sbart 3:771f97716830 117
sbart 2:6610fa50aa81 118 break;
sbart 3:771f97716830 119 case ANSWERSTATE: //L BUTTON
sbart 3:771f97716830 120 count = tsidata * 100;
sbart 3:771f97716830 121 pc.printf("Count before: %0f\n\r", count);
sbart 3:771f97716830 122 backwardCount(count, INCREMENT, MIN_NUM);
sbart 2:6610fa50aa81 123 rled = 0.0;
sbart 2:6610fa50aa81 124 gled = 1.0;
sbart 3:771f97716830 125
sbart 3:771f97716830 126
sbart 2:6610fa50aa81 127
sbart 2:6610fa50aa81 128 break;
sbart 2:6610fa50aa81 129 }
scohennm 1:44dcf262c7dd 130 } // if ! buttons
scohennm 1:44dcf262c7dd 131 }// for loop to look at buttons
scohennm 1:44dcf262c7dd 132 ButtonTimer.reset();
sbart 2:6610fa50aa81 133 //rled = 1.0;
sbart 2:6610fa50aa81 134 //gled = 0.0;
scohennm 0:04499bc54bee 135 }
scohennm 1:44dcf262c7dd 136 if(dataTimer.read() > DATAINTERVAL){
scohennm 1:44dcf262c7dd 137 dataTimer.reset();
scohennm 1:44dcf262c7dd 138 tempTSI = tsi.readPercentage();
scohennm 1:44dcf262c7dd 139 if (tempTSI > TSILIMIT){
sbart 3:771f97716830 140 tsidata = tempTSI/2;
scohennm 1:44dcf262c7dd 141 if (fabs(tsidata - lastTouch)> PRINTDELTA){
sbart 3:771f97716830 142 //pc.printf("Position %2f\n\r", tsidata);
scohennm 1:44dcf262c7dd 143 }
scohennm 1:44dcf262c7dd 144 }
scohennm 1:44dcf262c7dd 145 lastTouch=tsidata;
scohennm 1:44dcf262c7dd 146 }
sbart 2:6610fa50aa81 147
sbart 2:6610fa50aa81 148 if (LCDTimer.read() > LCDTIME) {
sbart 3:771f97716830 149
sbart 3:771f97716830 150 //float count = 0;
sbart 2:6610fa50aa81 151 LCDTimer.reset();
sbart 2:6610fa50aa81 152 switch(displayState) {
sbart 3:771f97716830 153 case 0: //R BUtton
sbart 3:771f97716830 154 //pc.printf("R Button");
sbart 3:771f97716830 155 //count = lastTouch;
sbart 3:771f97716830 156 //sprintf (lcdData,"%0f", count * 10);
sbart 3:771f97716830 157 //LCDMess(lcdData);
sbart 2:6610fa50aa81 158 break;
sbart 2:6610fa50aa81 159 case 1: //L Button
sbart 3:771f97716830 160 //pc.printf("L Button");
sbart 3:771f97716830 161 //count = lastTouch * 10;
sbart 3:771f97716830 162 //backwardCount(count, INCREMENT, MIN_NUM);
sbart 3:771f97716830 163 //LCDMess(lcdData);
sbart 2:6610fa50aa81 164 break;
sbart 2:6610fa50aa81 165 default:
sbart 2:6610fa50aa81 166 break;
sbart 2:6610fa50aa81 167 }
sbart 2:6610fa50aa81 168 }
scohennm 0:04499bc54bee 169 }
scohennm 0:04499bc54bee 170 }