Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // touch2RGB 00002 //program for FRDM-KL25Z 00003 //each touch on the touch sensor switches embedded led between Red Green and Blue colours 00004 00005 #include "mbed.h" 00006 #include "TSISensor.h" 00007 00008 const float TOUCH_THRESHOLD=0.5; //sensor returns analog value from 0.0(no touch) to 1.0(full touch) to determine when there is a touch 00009 enum colour { RED=0,GREEN,BLUE}; 00010 00011 bool analog2dig(float ); 00012 void setColour(colour); 00013 00014 DigitalOut r (LED_RED); 00015 DigitalOut g (LED_GREEN); 00016 DigitalOut b (LED_BLUE); 00017 colour currentColour=RED; 00018 00019 Serial pc(USBTX,USBRX); //serial connection with pc 00020 TSISensor tsi; //touchSensor 00021 00022 00023 00024 int main() { 00025 bool previousState=analog2dig(tsi.readPercentage()); 00026 bool currentState; 00027 00028 setColour(currentColour); //set initial colour 00029 pc.printf("begin - initial state=%d\n",previousState); 00030 00031 while(1){ 00032 currentState=analog2dig(tsi.readPercentage()); 00033 //pc.printf("state=%d\n",currentState); 00034 if(previousState==false && currentState==true){ //touch detected 00035 switch (currentColour){ 00036 case RED: 00037 setColour(GREEN); 00038 break; 00039 case GREEN: 00040 setColour(BLUE); 00041 break; 00042 case BLUE: 00043 setColour(RED); 00044 break; 00045 };//end switch 00046 }//end if 00047 previousState=currentState; 00048 wait(0.1); 00049 } 00050 } 00051 00052 //--------------------------------------------- 00053 void setColour(colour col){ 00054 switch(col){ 00055 case RED: 00056 r=0; // 0 enables the led 00057 g=1; // 1 disables the led 00058 b=1; 00059 currentColour=RED; 00060 break; 00061 case GREEN: 00062 r=1; 00063 g=0; 00064 b=1; 00065 currentColour=GREEN; 00066 break; 00067 case BLUE: 00068 r=1; 00069 g=1; 00070 b=0; 00071 currentColour=BLUE; 00072 break; 00073 } 00074 return; 00075 } 00076 00077 00078 //----------------------------------------------- 00079 bool analog2dig(float analogRead){ 00080 return (analogRead>=TOUCH_THRESHOLD)?true:false; 00081 00082 }
Generated on Sat Aug 6 2022 22:48:07 by
1.7.2