10/29/20 12:23

Dependencies:   mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library

Files at this revision

API Documentation at this revision

Comitter:
jmalone37
Date:
Mon Nov 02 18:17:47 2020 +0000
Parent:
3:ab0d9d3ae4d4
Commit message:
11/2/2020

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Mon Nov 02 18:17:47 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/antoniolinux/code/HCSR04/#86b2086be101
--- a/main.cpp	Sat Oct 31 18:44:09 2020 +0000
+++ b/main.cpp	Mon Nov 02 18:17:47 2020 +0000
@@ -1,10 +1,10 @@
 
-//Theremin style demo using HC-SR04 Sonar and a speaker
-// moving a hand away/towards sonar changes audio frequency
+//Theremin style demo using HC-SR04 Sonar and vibrating motors
+// moving a hand away/towards sonar changes vibrational frequency
 #include "mbed.h"
 #include "ultrasonic.h"
 #include "DRV2605.h"
- 
+//#include "hcsr04.h" 
 
 //DRV2605 haptics(p9, p10);
 
@@ -13,25 +13,19 @@
 bool isERM = true;
 DRV2605 hap1(i2c1);
 DRV2605 hap2(i2c2);
+//HCSR04 sensor1(p7, p8);
 
  
- 
-Timeout cycle;
+//Timeout cycle;
  
 //ultrasonic mu(p7, p8, .07, 1, &newdist);   
-volatile int half_cycle_time = 1;
+//volatile int half_cycle_time = 1;
  
 //two calls to this interrupt routine generates a square wave
-void toggle_interrupt()
-{
+//void toggle_interrupt()
+//{
   //  getCurrentDistance(0)=distance;
-    if (distance > 10) {
-        hap1.setWaveform(0);
-        hap1.playRtp(10);
-        } else if (distance > 50) {
-            hap1.seWaveform(0);
-            hap1.playRtp(50);
-        }
+  
     
     /*if (half_cycle_time>22000) haptics.play_waveform(0);
     else {
@@ -42,10 +36,10 @@
         }
     led = !led;*/
     
-    cycle.detach();
+    //cycle.detach();
     //update time for interrupt activation -change frequency of square wave
-    cycle.attach_us(&toggle_interrupt, half_cycle_time);
-}
+    //cycle.attach_us(&toggle_interrupt, half_cycle_time);
+//}
 
 /*
 void newdist(int distance)
@@ -60,17 +54,49 @@
 //Set the trigger pin to p6 and the echo pin to p7
 //have updates every .07 seconds and a timeout after 1
 //second, and call newdist when the distance changes
+
+ void dist(int distance)
+{
+    //put code here to execute when the distance has changed
+    if (distance > 10) {
+        hap1.setWaveform(0);
+        hap1.playRtp(10);
+        } else if (distance > 25) {
+            hap1.setWaveform(0);
+            hap1.playRtp(25);
+        }  
+    //printf("Distance %d mm\r\n", distance);
+}
+ 
+ultrasonic mu(p7, p8, .1, 1, &dist);    //Set the trigger pin to p6 and the echo pin to p7
+                                        //have updates every .1 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes
  
 int main()
 {
-    //audio = 0;
-    //led = 0;
-    cycle.attach(&toggle_interrupt, half_cycle_time);
-    mu.startUpdates();//start measuring the distance with the sonar
-    while(1) {
+    mu.startUpdates();//start measuring the distance
+    while(1)
+    {
         //Do something else here
-        mu.checkDistance();     
-        //call checkDistance() as much as possible, as this is where
-        //the class checks if dist needs to be called.
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
     }
-}
\ No newline at end of file
+}
+ 
+/*int main()
+{
+    while(1) {
+        
+     long distance = sensor1.distance(); 
+       if (distance > 10) {
+        hap1.setWaveform(0);
+        hap1.playRtp(10);
+        } else if (distance > 25) {
+            hap1.setWaveform(0);
+            hap1.playRtp(100);
+        }  
+      //printf("distance  %d  \n",distance);
+     // wait(1.0); // 1 sec  
+        
+    }
+} */
\ No newline at end of file