Homework for week 5, #2. Sets up light sensor to control brightness of LEDs.

Dependencies:   SLCD mbed TSI

Committer:
CKMonroe
Date:
Sun Sep 18 03:19:00 2016 +0000
Revision:
1:050d003386cb
Parent:
0:a9d218e72251
HW 5.2 revised after instructor update with extra code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CKMonroe 0:a9d218e72251 1 #include "mbed.h"
CKMonroe 0:a9d218e72251 2 #include "SLCD.h"
CKMonroe 1:050d003386cb 3 #include "TSISensor.h"
CKMonroe 0:a9d218e72251 4
CKMonroe 0:a9d218e72251 5
CKMonroe 0:a9d218e72251 6 #define PROGNAME "lightsense_kl46z_basic modified by CKM v1\n\r"
CKMonroe 0:a9d218e72251 7 #define DATATIME 400 // milliseconds
CKMonroe 0:a9d218e72251 8 #define LCDLEN 10
CKMonroe 0:a9d218e72251 9 #define LIGHTSENSORPORT PTE22
CKMonroe 1:050d003386cb 10 #define TSILIMIT 0.99
CKMonroe 1:050d003386cb 11 #define TSIHALFPOINT 0.5 //used to determine halfpoint of slider
CKMonroe 1:050d003386cb 12
CKMonroe 1:050d003386cb 13 // port addresses for buttons
CKMonroe 1:050d003386cb 14 #define LBUTTON PTC12
CKMonroe 1:050d003386cb 15 #define RBUTTON PTC3
CKMonroe 1:050d003386cb 16
CKMonroe 1:050d003386cb 17 //timer for button read
CKMonroe 1:050d003386cb 18 #define BUTTONTIME 0.2
CKMonroe 1:050d003386cb 19 //defined variable for number of buttons
CKMonroe 1:050d003386cb 20 #define NUMBUTS 2
CKMonroe 0:a9d218e72251 21
CKMonroe 0:a9d218e72251 22
CKMonroe 0:a9d218e72251 23
CKMonroe 0:a9d218e72251 24 SLCD slcd; //define LCD display globally define
CKMonroe 0:a9d218e72251 25 Serial pc(USBTX, USBRX);
CKMonroe 0:a9d218e72251 26 Timer LEDTimer;
CKMonroe 0:a9d218e72251 27
CKMonroe 1:050d003386cb 28 //set up buttons and timer for reading states
CKMonroe 1:050d003386cb 29 DigitalIn leftButton(LBUTTON);
CKMonroe 1:050d003386cb 30 DigitalIn rightButton(RBUTTON);
CKMonroe 1:050d003386cb 31 Timer ButtonTimer;
CKMonroe 1:050d003386cb 32
CKMonroe 1:050d003386cb 33 //boolean to determine mode of reading.
CKMonroe 1:050d003386cb 34 // 0(false) = 16 bit mode.
CKMonroe 1:050d003386cb 35 // 1(true) = floating point mode.
CKMonroe 1:050d003386cb 36 bool displayMode;
CKMonroe 1:050d003386cb 37
CKMonroe 0:a9d218e72251 38 void LCDMess(char *lMess){
CKMonroe 0:a9d218e72251 39 slcd.Home();
CKMonroe 0:a9d218e72251 40 slcd.clear();
CKMonroe 0:a9d218e72251 41 slcd.printf(lMess);
CKMonroe 0:a9d218e72251 42 }
CKMonroe 0:a9d218e72251 43
CKMonroe 0:a9d218e72251 44
CKMonroe 0:a9d218e72251 45 int main() {
CKMonroe 0:a9d218e72251 46 //set up LEDs
CKMonroe 0:a9d218e72251 47 PwmOut gled(LED_GREEN);
CKMonroe 0:a9d218e72251 48 PwmOut rled(LED_RED);
CKMonroe 0:a9d218e72251 49
CKMonroe 1:050d003386cb 50 //create Analogin object with port PTE22
CKMonroe 0:a9d218e72251 51 AnalogIn LightSensor(LIGHTSENSORPORT);
CKMonroe 1:050d003386cb 52
CKMonroe 1:050d003386cb 53 //create TSI (slider) object to read tsidata (put into a float below)
CKMonroe 1:050d003386cb 54 TSISensor tsi;
CKMonroe 1:050d003386cb 55
CKMonroe 1:050d003386cb 56 //start up button timer
CKMonroe 1:050d003386cb 57 ButtonTimer.start();
CKMonroe 1:050d003386cb 58 ButtonTimer.reset();
CKMonroe 1:050d003386cb 59
CKMonroe 1:050d003386cb 60 float tsidata;
CKMonroe 0:a9d218e72251 61 float lightData;
CKMonroe 1:050d003386cb 62 float ledData;
CKMonroe 0:a9d218e72251 63 char lcdData[LCDLEN];
CKMonroe 0:a9d218e72251 64
CKMonroe 0:a9d218e72251 65 int timeToChangeDF = DATATIME;
CKMonroe 0:a9d218e72251 66 LEDTimer.start();
CKMonroe 0:a9d218e72251 67 LEDTimer.reset();
CKMonroe 0:a9d218e72251 68 pc.printf(PROGNAME);
CKMonroe 0:a9d218e72251 69
CKMonroe 0:a9d218e72251 70 while(true) {
CKMonroe 0:a9d218e72251 71 if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
CKMonroe 0:a9d218e72251 72
CKMonroe 1:050d003386cb 73 if(displayMode == 1){ //if in floating point mode, display floating point numbers
CKMonroe 1:050d003386cb 74 lightData = (1.0 - LightSensor.read()); // show as increasiing with increasing intensity
CKMonroe 1:050d003386cb 75 sprintf(lcdData,"%4.3f",lightData); //print data to lcd screen
CKMonroe 1:050d003386cb 76 }
CKMonroe 1:050d003386cb 77 else if (displayMode == 0){ //if in 16 bit mode, display 16 bit numbers
CKMonroe 1:050d003386cb 78 lightData = LightSensor.read_u16();
CKMonroe 1:050d003386cb 79
CKMonroe 1:050d003386cb 80 //SLIDER STUFF//
CKMonroe 1:050d003386cb 81 //read slider data, put into float variable
CKMonroe 1:050d003386cb 82 tsidata = tsi.readPercentage();
CKMonroe 1:050d003386cb 83
CKMonroe 1:050d003386cb 84 if (tsidata < TSILIMIT){//if tsi data is read before the half point
CKMonroe 1:050d003386cb 85 if (tsidata < TSIHALFPOINT){ //show first 4 digits if slider reads left side
CKMonroe 1:050d003386cb 86 float divData = (int)lightData/10;
CKMonroe 1:050d003386cb 87 sprintf(lcdData, "%4.0f", divData);//dividing by ten to show first 4
CKMonroe 1:050d003386cb 88 }
CKMonroe 1:050d003386cb 89 else if (tsidata > TSIHALFPOINT){ //if tsidata is read past half point
CKMonroe 1:050d003386cb 90 float modData = (int)lightData%10000;
CKMonroe 1:050d003386cb 91 sprintf(lcdData, "%4.0f", modData);//modulus by 10k to show last 4
CKMonroe 1:050d003386cb 92
CKMonroe 1:050d003386cb 93 }//end inner if-elseif (tsidata < TSHIHALFPOINT)
CKMonroe 1:050d003386cb 94 }//end outer if (tsidata <TSILIMIT)
CKMonroe 1:050d003386cb 95 }//end outer if-elseif (displayMode)
CKMonroe 1:050d003386cb 96
CKMonroe 0:a9d218e72251 97 //set LEDs to light data value for brightness
CKMonroe 0:a9d218e72251 98 //they get brighter in dark rooms and dimmer in bright rooms
CKMonroe 1:050d003386cb 99 //these values only use the floating point format since
CKMonroe 1:050d003386cb 100 //ADC format can't be translated to the LEDs as easily.
CKMonroe 1:050d003386cb 101 ledData = (1.0 - LightSensor.read());
CKMonroe 1:050d003386cb 102 gled = ledData;
CKMonroe 1:050d003386cb 103 rled = ledData;
CKMonroe 0:a9d218e72251 104
CKMonroe 0:a9d218e72251 105 LCDMess(lcdData);
CKMonroe 0:a9d218e72251 106 timeToChangeDF = DATATIME;
CKMonroe 0:a9d218e72251 107 LEDTimer.reset();
CKMonroe 1:050d003386cb 108 }//end if (LEDtimer)
CKMonroe 1:050d003386cb 109
CKMonroe 1:050d003386cb 110 if (ButtonTimer > BUTTONTIME){
CKMonroe 1:050d003386cb 111 if(!leftButton) { //on left button push, set mode to 0 (float)
CKMonroe 1:050d003386cb 112 displayMode = 0;
CKMonroe 1:050d003386cb 113 }
CKMonroe 1:050d003386cb 114 if(!rightButton){ //on right button push, set mode to 1 (ADC)
CKMonroe 1:050d003386cb 115 displayMode = 1;
CKMonroe 1:050d003386cb 116 }//end inner if statement (!leftButton)
CKMonroe 1:050d003386cb 117 ButtonTimer.reset();
CKMonroe 1:050d003386cb 118 }//end outer if statement (buttonTimer)
CKMonroe 1:050d003386cb 119 }// end while (true)
CKMonroe 1:050d003386cb 120 }//end main