HW_5.2_pt2

Dependencies:   SLCD TSI mbed

Fork of lightsense_kl46z_basic by Stanley Cohen

Committer:
sbart
Date:
Mon Sep 19 06:03:21 2016 +0000
Revision:
10:f8c028e50ea4
Parent:
9:f619cdaa7a65
SBart-SSD341-HW-5.2-pt2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:e23fffd4b9a7 1 #include "mbed.h"
scohennm 1:51f8c2b04ce2 2 #include "SLCD.h"
sbart 10:f8c028e50ea4 3 #include "TSISensor.h"
scohennm 3:64e28ee5719b 4
sbart 10:f8c028e50ea4 5 #define PROGNAME "sbart_lightsense_kl46z pt2\n\r"
scohennm 8:3b19ecdc4261 6 #define DATATIME 400 // milliseconds
scohennm 1:51f8c2b04ce2 7 #define LCDLEN 10
scohennm 8:3b19ecdc4261 8 #define LIGHTSENSORPORT PTE22
scohennm 6:710e18c552f5 9
scohennm 4:bd42ab18979b 10 SLCD slcd; //define LCD display globally define
scohennm 1:51f8c2b04ce2 11 Serial pc(USBTX, USBRX);
scohennm 8:3b19ecdc4261 12 Timer LEDTimer;
sbart 10:f8c028e50ea4 13 InterruptIn btnRight(PTC3);
sbart 10:f8c028e50ea4 14 InterruptIn btnLeft(PTC12);
sbart 10:f8c028e50ea4 15 //AnalogIn lightInRaw();
sbart 10:f8c028e50ea4 16 bool bLCDSwitch = true; //When true, display floating point number
sbart 10:f8c028e50ea4 17 short rawData;
sbart 10:f8c028e50ea4 18 float tsiData;
scohennm 1:51f8c2b04ce2 19
scohennm 1:51f8c2b04ce2 20 void LCDMess(char *lMess){
scohennm 1:51f8c2b04ce2 21 slcd.Home();
scohennm 1:51f8c2b04ce2 22 slcd.clear();
scohennm 1:51f8c2b04ce2 23 slcd.printf(lMess);
scohennm 1:51f8c2b04ce2 24 }
scohennm 0:e23fffd4b9a7 25
sbart 10:f8c028e50ea4 26 void btnRightPressed(){
sbart 10:f8c028e50ea4 27 bLCDSwitch = true;
sbart 10:f8c028e50ea4 28 pc.printf("btnRightPressed()");
sbart 10:f8c028e50ea4 29
sbart 10:f8c028e50ea4 30 }
sbart 10:f8c028e50ea4 31
sbart 10:f8c028e50ea4 32 void btnLeftPressed(){
sbart 10:f8c028e50ea4 33 bLCDSwitch = false;
sbart 10:f8c028e50ea4 34 pc.printf("btnLeftPressed()");
sbart 10:f8c028e50ea4 35 }
scohennm 9:f619cdaa7a65 36
scohennm 0:e23fffd4b9a7 37 int main() {
scohennm 8:3b19ecdc4261 38 AnalogIn LightSensor(LIGHTSENSORPORT);
scohennm 6:710e18c552f5 39 float lightData;
scohennm 1:51f8c2b04ce2 40 char lcdData[LCDLEN];
sbart 10:f8c028e50ea4 41 PwmOut gled(LED_GREEN);
sbart 10:f8c028e50ea4 42 PwmOut rled(LED_RED);
sbart 10:f8c028e50ea4 43
sbart 10:f8c028e50ea4 44 TSISensor tsi;
sbart 10:f8c028e50ea4 45
scohennm 8:3b19ecdc4261 46 int timeToChangeDF = DATATIME;
scohennm 3:64e28ee5719b 47 LEDTimer.start();
scohennm 3:64e28ee5719b 48 LEDTimer.reset();
scohennm 1:51f8c2b04ce2 49 pc.printf(PROGNAME);
scohennm 1:51f8c2b04ce2 50
sbart 10:f8c028e50ea4 51 btnRight.fall(&btnRightPressed);
sbart 10:f8c028e50ea4 52 btnLeft.fall(&btnLeftPressed);
sbart 10:f8c028e50ea4 53
scohennm 8:3b19ecdc4261 54 while(true) {
scohennm 8:3b19ecdc4261 55 if (LEDTimer.read_ms() > timeToChangeDF){ // check for timer time out transtion
sbart 10:f8c028e50ea4 56 lightData = (1.0 - LightSensor.read()); // show as increasiing with increasing intensity
sbart 10:f8c028e50ea4 57 if(bLCDSwitch == true) {
sbart 10:f8c028e50ea4 58 sprintf(lcdData,"%4.3f",lightData);
sbart 10:f8c028e50ea4 59 pc.printf(lcdData);
sbart 10:f8c028e50ea4 60 pc.printf("\r\n");
sbart 10:f8c028e50ea4 61 }
sbart 10:f8c028e50ea4 62 else {
sbart 10:f8c028e50ea4 63 rawData = LightSensor.read_u16();
sbart 10:f8c028e50ea4 64 tsiData = tsi.readPercentage();
sbart 10:f8c028e50ea4 65 if(tsiData > .5)
sbart 10:f8c028e50ea4 66 rawData = rawData/10;
sbart 10:f8c028e50ea4 67 sprintf(lcdData, "%u", rawData);
sbart 10:f8c028e50ea4 68 pc.printf(lcdData);
sbart 10:f8c028e50ea4 69 pc.printf("\r\n");
sbart 10:f8c028e50ea4 70 }
sbart 10:f8c028e50ea4 71 LCDMess(lcdData);
sbart 10:f8c028e50ea4 72 gled = lightData;
sbart 10:f8c028e50ea4 73 rled = lightData;
scohennm 8:3b19ecdc4261 74 timeToChangeDF = DATATIME;
scohennm 8:3b19ecdc4261 75 LEDTimer.reset();
scohennm 8:3b19ecdc4261 76 }
scohennm 4:bd42ab18979b 77 }// emd while
scohennm 4:bd42ab18979b 78 }