for distance sensor

Dependencies:   mbed HCSR04

Files at this revision

API Documentation at this revision

Comitter:
mlosa010
Date:
Fri Nov 15 18:26:05 2019 +0000
Commit message:
working Distance sensor;

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 47ec319db65c HCSR04.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Fri Nov 15 18:26:05 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/grantphillips/code/HCSR04/#8286d0de19ce
diff -r 000000000000 -r 47ec319db65c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 15 18:26:05 2019 +0000
@@ -0,0 +1,57 @@
+#define LENOFBOT 2.5
+#define LENOFSENSOR 0.394
+HCSR04 mySensor(p26,p13);
+Serial pc(USBTX, USBRX);
+PwmOut trigger(p24);
+PwmOut trigger_b(p23);
+InterruptIn echoStart(p13,PullDown);
+InterruptIn echoStop(p14,PullDown);
+Timer timer;
+Timer timer_b;
+volatile float front_reading;
+volatile float back_sensor;
+void start(void){
+    timer.start();
+    }
+void stop (void){
+    timer.stop();
+    front_reading = (float)timer.read()*13504;
+    timer.reset();
+    }
+void start_back(void){
+    timer_b.start();
+    
+    }
+void stop_back (void){
+    timer_b.stop();
+    back_sensor = (float)timer_b.read()*13480;
+    timer_b.reset();
+    }
+int main() {
+    echoStart.rise(&start);
+    echoStart.fall(&stop);
+    echoStop.rise(&start_back);
+    echoStop.fall(&stop_back);
+    trigger.period(0.06);
+    int pi[]= {3,1,4,2};
+    int currentdigit =0;
+    int front_distance;
+    int back_distance;
+    while(1) {
+        trigger.pulsewidth_us(10);
+        front_distance = (3*pi[currentdigit])+9;
+        back_distance= 27-front_distance;
+        
+        if((front_reading/2)+LENOFBOT+LENOFSENSOR>front_distance){
+        pc.printf("moving back distance read: %.3f digit: %d\n\r",(front_reading/2)+LENOFBOT+LENOFSENSOR,pi[1]);
+        if(((back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance-0.1||(back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance+0.1))
+            int r =1;
+            //currentdigit++;
+        }else if((front_reading/2)+LENOFBOT+LENOFSENSOR<=front_distance){
+            pc.printf("moving forward distance read: %.3f should be %d\n\r",(front_reading/2)+LENOFBOT+LENOFSENSOR, pi[0]);
+        if((back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance-0.1||(back_sensor/2)+LENOFBOT+LENOFSENSOR <= back_distance+0.1)
+            int i=1;
+            //currentdigit++;
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 47ec319db65c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Nov 15 18:26:05 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file