ECE 4180 / Mbed 2 deprecated 4180-L3-P2_EC-Theramin

Dependencies:   mbed HC_SR04_Ultrasonic_Library

Files at this revision

API Documentation at this revision

Comitter:
glanier9
Date:
Sat Mar 06 18:40:19 2021 +0000
Commit message:
Final version

Changed in this revision

HC_SR04_Ultrasonic_Library.lib Show annotated file Show diff for this revision Revisions of this file
Speaker.h 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HC_SR04_Ultrasonic_Library.lib	Sat Mar 06 18:40:19 2021 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Sat Mar 06 18:40:19 2021 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+// a new class to play a note on Speaker based on PwmOut class
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        _pin.period(1.0/frequency);
+        _pin = volume/2.0;
+        wait(duration);
+        _pin = 0.0;
+    }
+
+private:
+    PwmOut _pin;
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 06 18:40:19 2021 +0000
@@ -0,0 +1,49 @@
+//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"
+ 
+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()
+{
+    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);
+}
+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()
+{
+    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.
+    }
+}
+ 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Mar 06 18:40:19 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file