![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FRDM_touch2RGB
main.cpp@0:9f96889b0328, 2014-10-07 (annotated)
- Committer:
- foivosHrist
- Date:
- Tue Oct 07 23:32:46 2014 +0000
- Revision:
- 0:9f96889b0328
touch2RGB v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
foivosHrist | 0:9f96889b0328 | 1 | // touch2RGB |
foivosHrist | 0:9f96889b0328 | 2 | //program for FRDM-KL25Z |
foivosHrist | 0:9f96889b0328 | 3 | //each touch on the touch sensor switches embedded led between Red Green and Blue colours |
foivosHrist | 0:9f96889b0328 | 4 | |
foivosHrist | 0:9f96889b0328 | 5 | #include "mbed.h" |
foivosHrist | 0:9f96889b0328 | 6 | #include "TSISensor.h" |
foivosHrist | 0:9f96889b0328 | 7 | |
foivosHrist | 0:9f96889b0328 | 8 | 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 |
foivosHrist | 0:9f96889b0328 | 9 | enum colour { RED=0,GREEN,BLUE}; |
foivosHrist | 0:9f96889b0328 | 10 | |
foivosHrist | 0:9f96889b0328 | 11 | bool analog2dig(float ); |
foivosHrist | 0:9f96889b0328 | 12 | void setColour(colour); |
foivosHrist | 0:9f96889b0328 | 13 | |
foivosHrist | 0:9f96889b0328 | 14 | DigitalOut r (LED_RED); |
foivosHrist | 0:9f96889b0328 | 15 | DigitalOut g (LED_GREEN); |
foivosHrist | 0:9f96889b0328 | 16 | DigitalOut b (LED_BLUE); |
foivosHrist | 0:9f96889b0328 | 17 | colour currentColour=RED; |
foivosHrist | 0:9f96889b0328 | 18 | |
foivosHrist | 0:9f96889b0328 | 19 | Serial pc(USBTX,USBRX); //serial connection with pc |
foivosHrist | 0:9f96889b0328 | 20 | TSISensor tsi; //touchSensor |
foivosHrist | 0:9f96889b0328 | 21 | |
foivosHrist | 0:9f96889b0328 | 22 | |
foivosHrist | 0:9f96889b0328 | 23 | |
foivosHrist | 0:9f96889b0328 | 24 | int main() { |
foivosHrist | 0:9f96889b0328 | 25 | bool previousState=analog2dig(tsi.readPercentage()); |
foivosHrist | 0:9f96889b0328 | 26 | bool currentState; |
foivosHrist | 0:9f96889b0328 | 27 | |
foivosHrist | 0:9f96889b0328 | 28 | setColour(currentColour); //set initial colour |
foivosHrist | 0:9f96889b0328 | 29 | pc.printf("begin - initial state=%d\n",previousState); |
foivosHrist | 0:9f96889b0328 | 30 | |
foivosHrist | 0:9f96889b0328 | 31 | while(1){ |
foivosHrist | 0:9f96889b0328 | 32 | currentState=analog2dig(tsi.readPercentage()); |
foivosHrist | 0:9f96889b0328 | 33 | //pc.printf("state=%d\n",currentState); |
foivosHrist | 0:9f96889b0328 | 34 | if(previousState==false && currentState==true){ //touch detected |
foivosHrist | 0:9f96889b0328 | 35 | switch (currentColour){ |
foivosHrist | 0:9f96889b0328 | 36 | case RED: |
foivosHrist | 0:9f96889b0328 | 37 | setColour(GREEN); |
foivosHrist | 0:9f96889b0328 | 38 | break; |
foivosHrist | 0:9f96889b0328 | 39 | case GREEN: |
foivosHrist | 0:9f96889b0328 | 40 | setColour(BLUE); |
foivosHrist | 0:9f96889b0328 | 41 | break; |
foivosHrist | 0:9f96889b0328 | 42 | case BLUE: |
foivosHrist | 0:9f96889b0328 | 43 | setColour(RED); |
foivosHrist | 0:9f96889b0328 | 44 | break; |
foivosHrist | 0:9f96889b0328 | 45 | };//end switch |
foivosHrist | 0:9f96889b0328 | 46 | }//end if |
foivosHrist | 0:9f96889b0328 | 47 | previousState=currentState; |
foivosHrist | 0:9f96889b0328 | 48 | wait(0.1); |
foivosHrist | 0:9f96889b0328 | 49 | } |
foivosHrist | 0:9f96889b0328 | 50 | } |
foivosHrist | 0:9f96889b0328 | 51 | |
foivosHrist | 0:9f96889b0328 | 52 | //--------------------------------------------- |
foivosHrist | 0:9f96889b0328 | 53 | void setColour(colour col){ |
foivosHrist | 0:9f96889b0328 | 54 | switch(col){ |
foivosHrist | 0:9f96889b0328 | 55 | case RED: |
foivosHrist | 0:9f96889b0328 | 56 | r=0; // 0 enables the led |
foivosHrist | 0:9f96889b0328 | 57 | g=1; // 1 disables the led |
foivosHrist | 0:9f96889b0328 | 58 | b=1; |
foivosHrist | 0:9f96889b0328 | 59 | currentColour=RED; |
foivosHrist | 0:9f96889b0328 | 60 | break; |
foivosHrist | 0:9f96889b0328 | 61 | case GREEN: |
foivosHrist | 0:9f96889b0328 | 62 | r=1; |
foivosHrist | 0:9f96889b0328 | 63 | g=0; |
foivosHrist | 0:9f96889b0328 | 64 | b=1; |
foivosHrist | 0:9f96889b0328 | 65 | currentColour=GREEN; |
foivosHrist | 0:9f96889b0328 | 66 | break; |
foivosHrist | 0:9f96889b0328 | 67 | case BLUE: |
foivosHrist | 0:9f96889b0328 | 68 | r=1; |
foivosHrist | 0:9f96889b0328 | 69 | g=1; |
foivosHrist | 0:9f96889b0328 | 70 | b=0; |
foivosHrist | 0:9f96889b0328 | 71 | currentColour=BLUE; |
foivosHrist | 0:9f96889b0328 | 72 | break; |
foivosHrist | 0:9f96889b0328 | 73 | } |
foivosHrist | 0:9f96889b0328 | 74 | return; |
foivosHrist | 0:9f96889b0328 | 75 | } |
foivosHrist | 0:9f96889b0328 | 76 | |
foivosHrist | 0:9f96889b0328 | 77 | |
foivosHrist | 0:9f96889b0328 | 78 | //----------------------------------------------- |
foivosHrist | 0:9f96889b0328 | 79 | bool analog2dig(float analogRead){ |
foivosHrist | 0:9f96889b0328 | 80 | return (analogRead>=TOUCH_THRESHOLD)?true:false; |
foivosHrist | 0:9f96889b0328 | 81 | |
foivosHrist | 0:9f96889b0328 | 82 | } |