Recursion 12.2

Dependencies:   SLCD TSI mbed

Fork of kl46z_slider_mid_v1_amart by AnnaLouise Martinez

Committer:
annalou
Date:
Fri Nov 04 21:33:36 2016 +0000
Revision:
3:2bb5ccf119fc
Parent:
2:bb868c525c5c
Homework 12.2 Recursion

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
annalou 3:2bb5ccf119fc 18 #define PROGNAME "Recursive_C++_AnnaLouise Martinez\n\r"
scohennm 0:04499bc54bee 19
scohennm 0:04499bc54bee 20 SLCD slcd; //define LCD display
scohennm 0:04499bc54bee 21 Serial pc(USBTX, USBRX);
scohennm 0:04499bc54bee 22
scohennm 1:44dcf262c7dd 23 Timer dataTimer;
scohennm 1:44dcf262c7dd 24 Timer ButtonTimer; // for reading button states
scohennm 1:44dcf262c7dd 25 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
scohennm 0:04499bc54bee 26 float tsidata;
scohennm 1:44dcf262c7dd 27 int displayState;
scohennm 1:44dcf262c7dd 28
scohennm 1:44dcf262c7dd 29 void initialize_global_vars(){
scohennm 1:44dcf262c7dd 30 pc.printf(PROGNAME);
scohennm 1:44dcf262c7dd 31 // set up DAQ timers
scohennm 1:44dcf262c7dd 32 ButtonTimer.start();
scohennm 1:44dcf262c7dd 33 ButtonTimer.reset();
scohennm 1:44dcf262c7dd 34 dataTimer.start();
scohennm 1:44dcf262c7dd 35 dataTimer.reset();
scohennm 1:44dcf262c7dd 36 }
scohennm 0:04499bc54bee 37
scohennm 0:04499bc54bee 38 void LCDMess(char *lMess){
scohennm 0:04499bc54bee 39 slcd.Home();
scohennm 0:04499bc54bee 40 slcd.clear();
scohennm 0:04499bc54bee 41 slcd.printf(lMess);
scohennm 0:04499bc54bee 42 }
scohennm 0:04499bc54bee 43
annalou 3:2bb5ccf119fc 44 int countdown(int number, int increm, int minNum)
annalou 2:bb868c525c5c 45 {
annalou 3:2bb5ccf119fc 46 int increment = increm;
annalou 3:2bb5ccf119fc 47 int min_number = minNum;
annalou 3:2bb5ccf119fc 48 int intmax = number;
annalou 3:2bb5ccf119fc 49
annalou 3:2bb5ccf119fc 50 if(intmax <= min_number)
annalou 2:bb868c525c5c 51 {
annalou 3:2bb5ccf119fc 52 pc.printf("%d\n\r", intmax);
annalou 3:2bb5ccf119fc 53 return intmax;
annalou 2:bb868c525c5c 54 }
annalou 3:2bb5ccf119fc 55 else
annalou 3:2bb5ccf119fc 56 {
annalou 3:2bb5ccf119fc 57 pc.printf("%d\n\r", intmax);
annalou 3:2bb5ccf119fc 58 return countdown(intmax - increment, increment, min_number);
annalou 3:2bb5ccf119fc 59 }
annalou 2:bb868c525c5c 60 }
annalou 2:bb868c525c5c 61
scohennm 0:04499bc54bee 62 int main(void) {
scohennm 1:44dcf262c7dd 63 int i;
scohennm 0:04499bc54bee 64 char lcdData[LCDCHARLEN];
scohennm 1:44dcf262c7dd 65 float lastTouch = 0.0;
scohennm 1:44dcf262c7dd 66 TSISensor tsi;
scohennm 1:44dcf262c7dd 67 float tempTSI;
scohennm 0:04499bc54bee 68 PwmOut gled(LED_GREEN);
scohennm 0:04499bc54bee 69 PwmOut rled(LED_RED);
annalou 3:2bb5ccf119fc 70 int num = 50;
annalou 3:2bb5ccf119fc 71 int incr = 2;
annalou 3:2bb5ccf119fc 72 int minNum = 2;
scohennm 1:44dcf262c7dd 73
scohennm 1:44dcf262c7dd 74 initialize_global_vars();
scohennm 0:04499bc54bee 75
scohennm 0:04499bc54bee 76 while (true) {
scohennm 1:44dcf262c7dd 77 if (ButtonTimer > BUTTONTIME){
scohennm 1:44dcf262c7dd 78 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
scohennm 1:44dcf262c7dd 79 if(!buttons[i]) {
scohennm 1:44dcf262c7dd 80 displayState = i;
annalou 3:2bb5ccf119fc 81 int numReturn = countdown(num, incr, minNum);
scohennm 1:44dcf262c7dd 82 // do something here.
scohennm 1:44dcf262c7dd 83 } // if ! buttons
scohennm 1:44dcf262c7dd 84 }// for loop to look at buttons
scohennm 1:44dcf262c7dd 85 ButtonTimer.reset();
scohennm 1:44dcf262c7dd 86 sprintf (lcdData,"%0.4f",tsidata);
scohennm 1:44dcf262c7dd 87 LCDMess(lcdData);
scohennm 0:04499bc54bee 88 rled = 0.0;
scohennm 1:44dcf262c7dd 89 gled = 1.0;
scohennm 0:04499bc54bee 90 }
annalou 2:bb868c525c5c 91
scohennm 1:44dcf262c7dd 92 if(dataTimer.read() > DATAINTERVAL){
scohennm 1:44dcf262c7dd 93 dataTimer.reset();
annalou 2:bb868c525c5c 94 tempTSI = tsi.readPercentage();
scohennm 1:44dcf262c7dd 95 if (tempTSI > TSILIMIT){
scohennm 1:44dcf262c7dd 96 tsidata = tempTSI;
scohennm 1:44dcf262c7dd 97 if (fabs(tsidata - lastTouch)> PRINTDELTA){
annalou 3:2bb5ccf119fc 98
annalou 3:2bb5ccf119fc 99 }
scohennm 1:44dcf262c7dd 100 }
scohennm 1:44dcf262c7dd 101 lastTouch=tsidata;
scohennm 1:44dcf262c7dd 102 }
scohennm 0:04499bc54bee 103 }
scohennm 0:04499bc54bee 104 }