yuwen sun / Mbed 2 deprecated TouchSensor_3

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define numsamples 1
00004 int touchSense1(void);  //function for the button 1
00005 
00006 DigitalOut myled1(LED1);
00007 AnalogIn input1(p20);
00008 DigitalIn charger1(p19);
00009 DigitalOut ground1(p18);
00010 Serial pc(USBTX, USBRX); // tx, rx
00011 
00012 int touchSense2(void);  //function for the button 2
00013 
00014 DigitalOut myled2(LED4);
00015 AnalogIn input2(p15);
00016 DigitalIn charger2(p16);
00017 DigitalOut ground2(p17);
00018 
00019 int getc();
00020      
00021 int main() {
00022 char incode[10];
00023 char strcode[10];
00024 char matcode[10];
00025 char temp;
00026 int codelen,matlen;
00027 int i=0;
00028 int j=0;
00029 int flag1 = 0;
00030 int flag2 = 0;
00031 int failflag = 0;
00032 
00033   for (i=0;i<10;i++){   //initial the char array
00034     incode[i] = 'a';    
00035     strcode[i] = 'a';
00036   }
00037   i=0;
00038   pc.printf("\nPlease inpunt the code:\n");
00039   while ((temp = pc.getc())!= 'E') {    //getchar
00040     pc.printf("%c",temp);
00041     incode[i] = temp;    //put the original char in incode      
00042     i++;
00043   }    
00044   
00045   incode[i] = 'E'; //put "E" last as the end symbol
00046   pc.printf("%c",temp);
00047   pc.printf("\nThanks for the string, your input is\n");
00048   
00049   for (i=0;i<10;i++){   //extract 0/1 from incode to strcode
00050     if (incode[i] == 'E')
00051         break;
00052     else{
00053         if (incode[i] == '0' || incode[i] == '1'){
00054             strcode[j] = incode[i];
00055             j++;
00056         }
00057     }
00058         
00059   }
00060   
00061   codelen = j;
00062   
00063   for (j=0;j<codelen;j++)   //display the configuration
00064     pc.printf("%c",strcode[j]);
00065   pc.printf("\n");
00066   
00067   matlen = 0;  
00068   
00069   pc.printf("\nPlease input the code:");
00070   while(1) {
00071 
00072    if (touchSense1()) { 
00073        wait (0.005);    //wait 5ms for checking press
00074        if (touchSense1()){
00075             myled1 = 1;
00076             if (flag1 == 0){
00077                 flag1 =1;   //flag1 for symbol press
00078                 pc.printf("1");
00079                 matcode[matlen] = '1'; //record for compare
00080                 matlen ++;
00081              }
00082         }
00083          
00084     } else {
00085         wait(0.005);  //wait 5ms confirm release
00086         if (touchSense1()==0){
00087             myled1 = 0;
00088             flag1 = 0; //clear flag
00089         }
00090     }
00091     
00092  
00093     if (touchSense2()) { //samiliar as above 1
00094        wait (0.005);
00095        if (touchSense2()){
00096             myled2 = 1;
00097             if (flag2 == 0){
00098                 flag2 =1;
00099                 pc.printf("0");
00100                 matcode[matlen] = '0';
00101                 matlen ++;
00102              }
00103         }
00104          
00105     } else {
00106         wait(0.005);
00107         if (touchSense2()==0){
00108             myled2 = 0;
00109             flag2 = 0;
00110         }
00111     }
00112     
00113     if (flag1 == 1 && flag2 == 1){ //detect press both button
00114         pc.printf("\nError, please don't touch both!!");
00115         //break;
00116     }
00117     
00118     if (matlen >= codelen){ //check input length match the set up
00119         for (i=0;i<codelen;i++){
00120             if (matcode[i] != strcode[i])
00121                 failflag = 1; //flag for symbol not match
00122         }
00123         
00124         if (failflag == 0){
00125             pc.printf("\n Congratulations! Code is matched!!\n");
00126             matlen = 0;
00127             
00128         }
00129         
00130         else{
00131             pc.printf("\nSorry, the code is wrong, please input again!!\n");
00132             failflag = 0;
00133             matlen = 0;
00134         }
00135         
00136     }
00137     
00138    
00139   }
00140 }
00141 
00142 int touchSense1(void) //function for touch sensor
00143 {
00144     float sample;
00145     ground1 = 0;
00146     charger1.mode(PullUp);
00147     charger1.mode(PullNone);
00148     sample=input1.read();
00149     if (sample < 0.3) {
00150         return 1;
00151     } else {
00152         return 0;
00153     }
00154 }
00155 int touchSense2(void)
00156 {
00157     float sample;
00158     ground2 = 0;
00159     charger2.mode(PullUp);
00160     charger2.mode(PullNone);
00161     sample=input2.read();
00162     if (sample < 0.3) {
00163         return 1;
00164     } else {
00165         return 0;
00166     }
00167 }
00168