Reads a single line sensor and sends data through the usb serial port.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jmar11
Date:
Sun Oct 05 02:00:55 2014 +0000
Parent:
0:e0d4127520b7
Commit message:
Added a time-out for the sensor reading and removed some redundancies.

Changed in this revision

line_sensor_to_serial.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r e0d4127520b7 -r ecb1e7e5b3eb line_sensor_to_serial.cpp
--- a/line_sensor_to_serial.cpp	Sat Oct 04 23:07:09 2014 +0000
+++ b/line_sensor_to_serial.cpp	Sun Oct 05 02:00:55 2014 +0000
@@ -14,10 +14,10 @@
 
 Serial pc(USBTX, USBRX);
 Timer timer;
-DigitalInOut sens(PTD7);    //freedom board pin number
+DigitalInOut sens(PTB8);    //freedom board pin number
 
-int readLine(DigitalInOut&, Timer&);
-int setThresh(int);
+int readLine(int);
+void setThresh(int&);
 
 int main() {
     int thresh;
@@ -25,12 +25,12 @@
     pc.printf("Please put the sensor over white and press <enter>\n\r");
     pc.getc();
     
-    thresh = setThresh(readLine(sens, timer));
+    setThresh(thresh);
     
     pc.printf("The surface is:\n\r");
     
     while(1){
-        if (readLine(sens, timer) > thresh)
+        if (readLine(thresh) > thresh)
             pc.printf("Black\r");
         else
             pc.printf("White\r");
@@ -43,7 +43,7 @@
     pc.printf("The sensor value is:\n\r");
 
     while(1){        
-        count = readLine(sens, timer);
+        count = readLine(0x7A120);
         
         pc.printf("               \r");        
         pc.printf("%d\r",count);
@@ -52,7 +52,7 @@
     }*/
 }
 
-int readLine(DigitalInOut& sens, Timer& timer){
+int readLine(int timeOut = 0x7A120){
     timer.reset();
     
     sens.output(); 
@@ -61,13 +61,13 @@
     sens = 0;
     sens.input();
     timer.start();
-    while(sens == 1){
+    while(sens == 1 && timer.read_us() < (2 * timeOut)){
     }
     
     timer.stop();
     return timer.read_us();
 }
 
-int setThresh(int thresh){
-    return 9 * thresh;
+void setThresh(int& thresh){
+    thresh = readLine() * 9;
 }
\ No newline at end of file