s2 project 1st try

Dependencies:   TB6612 HCSR04 UoY-serial

Revision:
4:137b119e5198
Parent:
3:2d20926426fa
Child:
5:57196959be02
--- a/main.cpp	Thu Mar 18 18:22:06 2021 +0000
+++ b/main.cpp	Tue May 11 18:35:32 2021 +0000
@@ -1,39 +1,80 @@
 #include "mbed.h"
-
-// Initialise D2 as an OPEN DRAIN output
-// It can be used in the same way as a normal DigitalOut
-DigitalInOut trig(D2, PIN_OUTPUT, OpenDrain, 0);
-InterruptIn echoint(D3);    //Initialise echo as an InterruptIn class
-Timer t;                    //initialise timer
+#include "hcsr04.h"
 
-//create a callback that reesets and starts the timer (attaches start and reset to timer)
-void echoCallback1(){
-    t.reset();
-    t.start();
-}
+HCSR04 hcsr04(D2,D3); //Create hcsr04 object of type HCSR04 (class made in hcsr04.cpp), with pins D2 and D3 assigned to trig and echo respectively
+DigitalOut beep(D4); //using code made for motor modified to be just a pwm object, create beep of this type and assign D4 to it, this can then be used to control beep speed
+DigitalIn gButton(BUTTON1);
 
-//create a callback that stops the timer
-void echoCallback2(){
-    t.stop();
-}
 
 int main()
 {
-    unsigned char d; //initialise variable d of unsigned char type
+    while(true) {
 
-    while(true) {
-        // Send 10us trigger pulse - change to a ticker?
-        trig = true;
-        wait_us(10);
-        trig = false;
+        if (gButton == 0) {
+            hcsr04.distance();
+            while (gButton == 1) {
 
-        echoint.rise(echoCallback1);
-        echoint.fall(echoCallback2);
-
-        d = t.read()*160*100; //reads timer value, converts to distance then that to cm
-
-        printf("%hhu cm\r\n",d); //prints the distance in cm
-        thread_sleep_for(500); //puts thread to sleep for 500ms
-
+                if (hcsr04.distance() <= 15) { //retrive the distance value and compare against a condition, depending on condition it meets print a message and set the beep speed
+                    printf("Uncomfortably Close \r\n");
+                    beep = true;
+                    thread_sleep_for(100);
+                } else {
+                    if (hcsr04.distance() <= 30) {
+                        printf("Very Close \r\n");
+                        beep = true ;
+                        thread_sleep_for(100);
+                        beep = false;
+                        thread_sleep_for(100);
+                    } else {
+                        if (hcsr04.distance() <= 60) {
+                            printf("Close \r\n");
+                            beep = true;
+                            thread_sleep_for(100);
+                            beep = false;
+                            thread_sleep_for(200);
+                        } else {
+                            if (hcsr04.distance() <= 100) {
+                                printf("Safe \r\n");
+                                beep = true;
+                                thread_sleep_for(100);
+                                beep = false;
+                                thread_sleep_for(400);
+                            } else {
+                                if (hcsr04.distance() <= 200) {
+                                    printf("Far \r\n");
+                                    beep = true;
+                                    thread_sleep_for(100);
+                                    beep = false;
+                                    thread_sleep_for(700);
+                                } else {
+                                    if (hcsr04.distance() <= 300) {
+                                        printf("Very Far \r\n");
+                                        beep = true;
+                                        thread_sleep_for(100);
+                                        beep = false;
+                                        thread_sleep_for(1200);
+                                    } else {
+                                        if (hcsr04.distance() <= 400) {
+                                            printf("Distant \r\n");
+                                            beep = false;
+                                        } else {
+                                            if (hcsr04.distance() > 400) {
+                                                printf("Out of Range \r\n");
+                                                beep = false;
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        } else {
+            beep = false;
+        }
+        //thread_sleep_for(500); //put the thread to sleep for 500ms so the distance isn't read uncontrollably fast.
     }
 }
+
+//AAAAAAAAAAAAAAAAAa