Xinda Lin / Mbed 2 deprecated HW2-2

Dependencies:   mbed

Revision:
0:3ded4f3d3847
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 01 05:25:47 2010 +0000
@@ -0,0 +1,140 @@
+ #include "mbed.h"
+ 
+ #define numsamples 1
+ int touchSense1(void);
+ int touchSense2(void);
+ char * readIn();
+ char touchDetect ();
+
+ DigitalOut myled1(LED1);
+ DigitalOut myled2(LED2);
+ DigitalOut myled3(LED3);
+ AnalogIn input1(p20);
+ DigitalIn charger1(p19);
+ DigitalOut ground1(p18);
+ AnalogIn input2(p17);
+ DigitalIn charger2(p16);
+ DigitalOut ground2(p15);
+
+ 
+ Serial pc(USBTX, USBRX); // tx, rx
+
+ int main() {
+   char *target = new char [1000];
+  
+   while (1){
+    int i=1,j=0;
+    target=readIn();
+    wait (0.05);
+    while (target[j]!='E') {             //print the host value;
+    pc.printf ("%c", target[j]);
+    j++;
+    } 
+    pc.printf ("E");
+
+   while (1) {
+        pc.printf (" The next host value is :");   //Hint
+        pc.printf ("%c", target[i]);
+        if (target[i]=='E'){
+            pc.printf (" MATCH ");
+            pc.printf ("\r\n");
+            i=1;
+            break;  
+        }     
+        else if (target[i]==touchDetect()){                        
+           i++;
+           wait (0.5);
+        }   
+        else{ 
+            pc.printf (" TOUCH ERROR ");     //If the user touch both sensor, the retun value is 3, which will lead to touch error.
+            break;}                          //Also, if the user mistouch the sensor, it's also a touch error.
+    }  
+    }      
+ }
+ 
+ char * readIn()               //Get the PC host string starting with S.
+{
+    char *a =new char [1000];
+    char b;
+    int flag=1,i=0;
+    b=pc.getc();
+    if (b=='S'){
+        a[0]='S';
+        i++;
+        while (flag){
+            a[i]=pc.getc();         
+            if (a[i]=='E'){
+                flag=0;} 
+            else if (a[i]==' '){
+            i--;}
+            else if (a[i]=='S'){
+                pc.printf (" HOST ERROR ");
+                break;} 
+            i++;     
+        }           
+    }
+    else {
+    pc.printf (" HOST ERROR ");  } 
+    return a;    
+ }           
+    
+ 
+char touchDetect ()        //Detect the user input
+ {
+    while (1) {
+    if (touchSense1()){
+           pc.printf ("T1");     //Output T1 if senser 1 is touched
+           if (touchSense2()){
+               return '3';
+           }
+            while (touchSense1()){
+                wait(0.5);}
+           return '1';
+    }
+    else if (touchSense2()){      //Output T0 is senser 0 is touched
+           pc.printf ("T0");
+           if (touchSense1()){
+                return '3';
+           }
+             while (touchSense2()){
+                wait(0.5);}
+           return '0';
+           
+    }    
+    wait (0.05); 
+    }
+  }
+  
+ int touchSense1(void)
+ {
+     float sample;
+     ground1 = 0;
+     charger1.mode(PullUp);
+     charger1.mode(PullNone);
+     sample=input1.read();
+     if (sample < 0.3) {
+         return 1;
+     } else {
+         return 0;
+    }
+ }
+
+ int touchSense2(void)
+ {
+     float sample;
+     ground2 = 0;
+     charger2.mode(PullUp);
+     charger2.mode(PullNone);
+     sample=input2.read();
+     if (sample < 0.3) {
+         return 1;
+     } else {
+         return 0;
+    }
+ }
+ 
+
+
+
+
+