10/29/20 12:23
Dependencies: mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library
main.cpp
- Committer:
- jmalone37
- Date:
- 2020-10-29
- Revision:
- 0:00bd403e0742
- Child:
- 1:fbdfdb9ecbd2
File content as of revision 0:00bd403e0742:
//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" #include "DRV2605.h" DigitalOut audio(p26); //output to speaker amp or audio jack DigitalOut led(LED1); DigitalOut led2(LED2); DRV2605 haptics(p9, p10); 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) haptics.play_waveform(0); else { static int e1 = 1; haptics.play_waveform(e1); e1+=10; if(e1 > 121) e1 = 1; } 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(p7, p8, .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. } }