Homework2_2ForeBack_YuwenSun

Dependencies:   mbed

Committer:
sun831011
Date:
Tue Nov 30 22:18:16 2010 +0000
Revision:
0:257ce4f4805b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sun831011 0:257ce4f4805b 1 #include "mbed.h"
sun831011 0:257ce4f4805b 2
sun831011 0:257ce4f4805b 3 #define numsamples 1
sun831011 0:257ce4f4805b 4 int touchSense1(void); //function for the button 1
sun831011 0:257ce4f4805b 5
sun831011 0:257ce4f4805b 6 DigitalOut myled1(LED1);
sun831011 0:257ce4f4805b 7 AnalogIn input1(p20);
sun831011 0:257ce4f4805b 8 DigitalIn charger1(p19);
sun831011 0:257ce4f4805b 9 DigitalOut ground1(p18);
sun831011 0:257ce4f4805b 10 Serial pc(USBTX, USBRX); // tx, rx
sun831011 0:257ce4f4805b 11
sun831011 0:257ce4f4805b 12 int touchSense2(void); //function for the button 2
sun831011 0:257ce4f4805b 13
sun831011 0:257ce4f4805b 14 DigitalOut myled2(LED4);
sun831011 0:257ce4f4805b 15 AnalogIn input2(p15);
sun831011 0:257ce4f4805b 16 DigitalIn charger2(p16);
sun831011 0:257ce4f4805b 17 DigitalOut ground2(p17);
sun831011 0:257ce4f4805b 18
sun831011 0:257ce4f4805b 19 int getc();
sun831011 0:257ce4f4805b 20 void setcode();
sun831011 0:257ce4f4805b 21
sun831011 0:257ce4f4805b 22
sun831011 0:257ce4f4805b 23 char incode[10];
sun831011 0:257ce4f4805b 24 char strcode[10];
sun831011 0:257ce4f4805b 25 char matcode[10];
sun831011 0:257ce4f4805b 26 char temp;
sun831011 0:257ce4f4805b 27 int codelen = 1;
sun831011 0:257ce4f4805b 28 int matlen;
sun831011 0:257ce4f4805b 29 int i=0;
sun831011 0:257ce4f4805b 30 int j=0;
sun831011 0:257ce4f4805b 31 int flag1 = 0;
sun831011 0:257ce4f4805b 32 int flag2 = 0;
sun831011 0:257ce4f4805b 33 int failflag = 0;
sun831011 0:257ce4f4805b 34
sun831011 0:257ce4f4805b 35 int main() {
sun831011 0:257ce4f4805b 36
sun831011 0:257ce4f4805b 37 for (i=0;i<10;i++){ //initial the char array
sun831011 0:257ce4f4805b 38 incode[i] = 'a';
sun831011 0:257ce4f4805b 39 strcode[i] = 'a';
sun831011 0:257ce4f4805b 40 }
sun831011 0:257ce4f4805b 41
sun831011 0:257ce4f4805b 42 pc.attach(&setcode, Serial::RxIrq);
sun831011 0:257ce4f4805b 43
sun831011 0:257ce4f4805b 44 while(1) {
sun831011 0:257ce4f4805b 45
sun831011 0:257ce4f4805b 46
sun831011 0:257ce4f4805b 47 if (touchSense1()) {
sun831011 0:257ce4f4805b 48 wait (0.005); //wait 5ms for checking press
sun831011 0:257ce4f4805b 49 if (touchSense1()){
sun831011 0:257ce4f4805b 50 myled1 = 1;
sun831011 0:257ce4f4805b 51 if (flag1 == 0){
sun831011 0:257ce4f4805b 52 flag1 =1; //flag1 for symbol press
sun831011 0:257ce4f4805b 53 pc.printf("1");
sun831011 0:257ce4f4805b 54 matcode[matlen] = '1'; //record for compare
sun831011 0:257ce4f4805b 55 matlen ++;
sun831011 0:257ce4f4805b 56 }
sun831011 0:257ce4f4805b 57 }
sun831011 0:257ce4f4805b 58
sun831011 0:257ce4f4805b 59 } else {
sun831011 0:257ce4f4805b 60 wait(0.005); //wait 5ms confirm release
sun831011 0:257ce4f4805b 61 if (touchSense1()==0){
sun831011 0:257ce4f4805b 62 myled1 = 0;
sun831011 0:257ce4f4805b 63 flag1 = 0; //clear flag
sun831011 0:257ce4f4805b 64 }
sun831011 0:257ce4f4805b 65 }
sun831011 0:257ce4f4805b 66
sun831011 0:257ce4f4805b 67
sun831011 0:257ce4f4805b 68 if (touchSense2()) { //samiliar as above 1
sun831011 0:257ce4f4805b 69 wait (0.005);
sun831011 0:257ce4f4805b 70 if (touchSense2()){
sun831011 0:257ce4f4805b 71 myled2 = 1;
sun831011 0:257ce4f4805b 72 if (flag2 == 0){
sun831011 0:257ce4f4805b 73 flag2 =1;
sun831011 0:257ce4f4805b 74 pc.printf("0");
sun831011 0:257ce4f4805b 75 matcode[matlen] = '0';
sun831011 0:257ce4f4805b 76 matlen ++;
sun831011 0:257ce4f4805b 77 }
sun831011 0:257ce4f4805b 78 }
sun831011 0:257ce4f4805b 79
sun831011 0:257ce4f4805b 80 } else {
sun831011 0:257ce4f4805b 81 wait(0.005);
sun831011 0:257ce4f4805b 82 if (touchSense2()==0){
sun831011 0:257ce4f4805b 83 myled2 = 0;
sun831011 0:257ce4f4805b 84 flag2 = 0;
sun831011 0:257ce4f4805b 85 }
sun831011 0:257ce4f4805b 86 }
sun831011 0:257ce4f4805b 87
sun831011 0:257ce4f4805b 88 if (flag1 == 1 && flag2 == 1){ //detect press both button
sun831011 0:257ce4f4805b 89 pc.printf("\nError, please don't touch both!!");
sun831011 0:257ce4f4805b 90 //break;
sun831011 0:257ce4f4805b 91 }
sun831011 0:257ce4f4805b 92
sun831011 0:257ce4f4805b 93 if (matlen >= codelen){ //check input length match the set up
sun831011 0:257ce4f4805b 94 for (i=0;i<codelen;i++){
sun831011 0:257ce4f4805b 95 if (matcode[i] != strcode[i])
sun831011 0:257ce4f4805b 96 failflag = 1; //flag for symbol not match
sun831011 0:257ce4f4805b 97 }
sun831011 0:257ce4f4805b 98
sun831011 0:257ce4f4805b 99 if (failflag == 0){
sun831011 0:257ce4f4805b 100 pc.printf("\n Congratulations! Code is matched!!\n");
sun831011 0:257ce4f4805b 101 matlen = 0;
sun831011 0:257ce4f4805b 102
sun831011 0:257ce4f4805b 103 }
sun831011 0:257ce4f4805b 104
sun831011 0:257ce4f4805b 105 else{
sun831011 0:257ce4f4805b 106 pc.printf("\nSorry, the code is wrong, please input again!!\n");
sun831011 0:257ce4f4805b 107 failflag = 0;
sun831011 0:257ce4f4805b 108 matlen = 0;
sun831011 0:257ce4f4805b 109 }
sun831011 0:257ce4f4805b 110
sun831011 0:257ce4f4805b 111 }
sun831011 0:257ce4f4805b 112
sun831011 0:257ce4f4805b 113
sun831011 0:257ce4f4805b 114 }
sun831011 0:257ce4f4805b 115 }
sun831011 0:257ce4f4805b 116
sun831011 0:257ce4f4805b 117 int touchSense1(void) //function for touch sensor
sun831011 0:257ce4f4805b 118 {
sun831011 0:257ce4f4805b 119 float sample;
sun831011 0:257ce4f4805b 120 ground1 = 0;
sun831011 0:257ce4f4805b 121 charger1.mode(PullUp);
sun831011 0:257ce4f4805b 122 charger1.mode(PullNone);
sun831011 0:257ce4f4805b 123 sample=input1.read();
sun831011 0:257ce4f4805b 124 if (sample < 0.3) {
sun831011 0:257ce4f4805b 125 return 1;
sun831011 0:257ce4f4805b 126 } else {
sun831011 0:257ce4f4805b 127 return 0;
sun831011 0:257ce4f4805b 128 }
sun831011 0:257ce4f4805b 129 }
sun831011 0:257ce4f4805b 130 int touchSense2(void)
sun831011 0:257ce4f4805b 131 {
sun831011 0:257ce4f4805b 132 float sample;
sun831011 0:257ce4f4805b 133 ground2 = 0;
sun831011 0:257ce4f4805b 134 charger2.mode(PullUp);
sun831011 0:257ce4f4805b 135 charger2.mode(PullNone);
sun831011 0:257ce4f4805b 136 sample=input2.read();
sun831011 0:257ce4f4805b 137 if (sample < 0.3) {
sun831011 0:257ce4f4805b 138 return 1;
sun831011 0:257ce4f4805b 139 } else {
sun831011 0:257ce4f4805b 140 return 0;
sun831011 0:257ce4f4805b 141 }
sun831011 0:257ce4f4805b 142 }
sun831011 0:257ce4f4805b 143
sun831011 0:257ce4f4805b 144 void setcode(){
sun831011 0:257ce4f4805b 145 i=0;
sun831011 0:257ce4f4805b 146 j=0;
sun831011 0:257ce4f4805b 147 pc.printf("\nPlease inpunt the code:\n");
sun831011 0:257ce4f4805b 148 while ((temp = pc.getc())!= 'E') { //getchar
sun831011 0:257ce4f4805b 149 pc.printf("%c",temp);
sun831011 0:257ce4f4805b 150 incode[i] = temp; //put the original char in incode
sun831011 0:257ce4f4805b 151 i++;
sun831011 0:257ce4f4805b 152 }
sun831011 0:257ce4f4805b 153
sun831011 0:257ce4f4805b 154 incode[i] = 'E'; //put "E" last as the end symbol
sun831011 0:257ce4f4805b 155 pc.printf("%c",temp);
sun831011 0:257ce4f4805b 156 pc.printf("\nThanks for the string, your input is\n");
sun831011 0:257ce4f4805b 157
sun831011 0:257ce4f4805b 158 for (i=0;i<10;i++){ //extract 0/1 from incode to strcode
sun831011 0:257ce4f4805b 159 if (incode[i] == 'E')
sun831011 0:257ce4f4805b 160 break;
sun831011 0:257ce4f4805b 161 else{
sun831011 0:257ce4f4805b 162 if (incode[i] == '0' || incode[i] == '1'){
sun831011 0:257ce4f4805b 163 strcode[j] = incode[i];
sun831011 0:257ce4f4805b 164 j++;
sun831011 0:257ce4f4805b 165 }
sun831011 0:257ce4f4805b 166 }
sun831011 0:257ce4f4805b 167
sun831011 0:257ce4f4805b 168 }
sun831011 0:257ce4f4805b 169
sun831011 0:257ce4f4805b 170 codelen = j;
sun831011 0:257ce4f4805b 171
sun831011 0:257ce4f4805b 172 for (j=0;j<codelen;j++) //display the configuration
sun831011 0:257ce4f4805b 173 pc.printf("%c",strcode[j]);
sun831011 0:257ce4f4805b 174 pc.printf("\n");
sun831011 0:257ce4f4805b 175
sun831011 0:257ce4f4805b 176 matlen = 0;
sun831011 0:257ce4f4805b 177
sun831011 0:257ce4f4805b 178 pc.printf("\nPlease input the code:");
sun831011 0:257ce4f4805b 179 }