'

https://os.mbed.com/media/uploads/danijel28/indikatorudaljenostiobjekta.cpp

Files at this revision

API Documentation at this revision

Comitter:
danijel28
Date:
Tue Feb 04 17:57:14 2020 +0000
Parent:
0:86b2086be101
Commit message:
I adjusted HCSR04 and buzzer library for school project

Changed in this revision

hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
diff -r 86b2086be101 -r 6f2201f12897 hcsr04.cpp
--- a/hcsr04.cpp	Mon Apr 14 08:23:09 2014 +0000
+++ b/hcsr04.cpp	Tue Feb 04 17:57:14 2020 +0000
@@ -1,29 +1,28 @@
 #include "hcsr04.h"
 #include "mbed.h"
-/*
-*HCSR04.cpp
-*/
-HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {}
- long HCSR04::echo_duration() {
-        
-    timer.reset();  //reset timer
-    trig=0;   // trigger low 
-    wait_us(2); //  wait 
-    trig=1;   //  trigger high
+
+HCSR04::HCSR04(PinName t, PinName e) : triger(t), echo(e) {}
+long HCSR04::echo_trajanjeImpulsa()
+{
+
+    timer.reset();  //Resetiranje timera na 0
+    triger=0;   // Triger u logičkoj "0"
+    wait_us(2);
+    triger=1;   //  Triger u logičkoj "1"
     wait_us(10);
-    trig=0;  // trigger low
-         while(!echo); // start pulseIN
-      timer.start();
-     while(echo);
-      timer.stop();
-     return timer.read_us(); 
- 
+    triger=0;  // Triger u logičkoj "0"
+    while(!echo);
+    timer.start(); // Timer počinje brojati kada triger pošalje impuls
+    while(echo);
+    timer.stop();
+    return timer.read_us();
+
 }
- 
-//return distance in cm 
-long HCSR04::distance(){
-    duration = echo_duration();
-  distance_cm = (duration/2)/29.1  ;
-        return distance_cm;
 
+//Vraća udaljenost u centimetrima
+long HCSR04::udaljenost()
+{
+    trajanjeImpulsa = echo_trajanjeImpulsa();
+    udaljenost_cm = (trajanjeImpulsa/2)/29.1; 
+    return udaljenost_cm;
 }
\ No newline at end of file
diff -r 86b2086be101 -r 6f2201f12897 hcsr04.h
--- a/hcsr04.h	Mon Apr 14 08:23:09 2014 +0000
+++ b/hcsr04.h	Tue Feb 04 17:57:14 2020 +0000
@@ -1,50 +1,18 @@
-/* File: HCSR04.h
- * Author: Antonio Buonanno  
- *Board: STM NUCLEO F401RE, 
- *Hardware: Ultrasonic Range HC-SR04,  
- * 
- *This work derived from Arduino library, 
- *
- * Desc: driver for HCSR04 Ultrasonic Range Finder.  The returned range
- *       is in units of meters.
- *  
- *       
- *
-*/
-
-/* EXAMPLE
-#include "mbed.h"
-#include "hcsr04.h"
-
-//D12 TRIGGER D11 ECHO
-   HCSR04 sensor(D12, D11); 
-int main() {
-    while(1) {
-        
-     long distance = sensor.distance();   
-      printf("distanza  %d  \n",distance);
-      wait(1.0); // 1 sec  
-        
-    }
-}
-*/
 #ifndef hcsr04_H
 #define hcsr04_H
 #include "mbed.h"
-
-
  
 class HCSR04 {
   public:
     HCSR04(PinName t, PinName e);
-    long echo_duration();
-    long distance();
+    long echo_trajanjeImpulsa();
+    long udaljenost();
  
     private:
-        DigitalOut trig;
+        DigitalOut triger;
         DigitalIn echo;
         Timer timer;
-        long duration,distance_cm;
+        long trajanjeImpulsa,udaljenost_cm;
 };
  
 #endif
\ No newline at end of file