ECE 4180 Lab 3 Part 1 Extra Credit

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Fork of LPC1768_HCSR04_HelloWorld by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
abraha2d
Date:
Tue Oct 16 00:23:27 2018 +0000
Parent:
2:3566c2d1e86b
Commit message:
Save point

Changed in this revision

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
--- a/main.cpp	Tue Sep 13 18:16:42 2016 +0000
+++ b/main.cpp	Tue Oct 16 00:23:27 2018 +0000
@@ -1,23 +1,48 @@
+//Theremin style demo using HC-SR04 Sonar and a speaker
+// moving a hand away/towards sonar changes audio frequency
 #include "mbed.h"
 #include "ultrasonic.h"
-
- void dist(int distance)
+ 
+DigitalOut audio(p26); //output to speaker amp or audio jack
+DigitalOut led(LED1); 
+DigitalOut led2(LED2);
+ 
+Timeout cycle;
+ 
+volatile int half_cycle_time = 1;
+ 
+//two calls to this interrupt routine generates a square wave
+void toggle_interrupt()
 {
-    //put code here to execute when the distance has changed
-    printf("Distance %d mm\r\n", distance);
+    if (half_cycle_time>22000) audio=0; //mute if nothing in range
+    else audio = !audio; //toggle to make half a square wave
+    led = !led;
+    cycle.detach();
+    //update time for interrupt activation -change frequency of square wave
+    cycle.attach_us(&toggle_interrupt, half_cycle_time);
 }
-
-ultrasonic mu(p6, p7, .1, 1, &dist);    //Set the trigger pin to D8 and the echo pin to D9
-                                        //have updates every .1 seconds and a timeout after 1
-                                        //second, and call dist when the distance changes
-
+void newdist(int distance)
+{
+    //update frequency based on new sonar data
+    led2 = !led2;
+    half_cycle_time = distance<<3;
+}
+//HC-SR04 Sonar module
+ultrasonic mu(p6, p7, .07, 1, &newdist);    
+//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
+ 
 int main()
 {
-    mu.startUpdates();//start measuring the distance
-    while(1)
-    {
+    audio = 0;
+    led = 0;
+    cycle.attach(&toggle_interrupt, half_cycle_time);
+    mu.startUpdates();//start measuring the distance with the sonar
+    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.
     }
 }
--- a/mbed.bld	Tue Sep 13 18:16:42 2016 +0000
+++ b/mbed.bld	Tue Oct 16 00:23:27 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187
\ No newline at end of file