10/29/20 12:23

Dependencies:   mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library

Committer:
jmalone37
Date:
Thu Oct 29 16:23:43 2020 +0000
Revision:
0:00bd403e0742
Child:
1:fbdfdb9ecbd2
10/29/20 12:23

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmalone37 0:00bd403e0742 1
jmalone37 0:00bd403e0742 2 //Theremin style demo using HC-SR04 Sonar and a speaker
jmalone37 0:00bd403e0742 3 // moving a hand away/towards sonar changes audio frequency
jmalone37 0:00bd403e0742 4 #include "mbed.h"
jmalone37 0:00bd403e0742 5 #include "ultrasonic.h"
jmalone37 0:00bd403e0742 6 #include "DRV2605.h"
jmalone37 0:00bd403e0742 7
jmalone37 0:00bd403e0742 8 DigitalOut audio(p26); //output to speaker amp or audio jack
jmalone37 0:00bd403e0742 9 DigitalOut led(LED1);
jmalone37 0:00bd403e0742 10 DigitalOut led2(LED2);
jmalone37 0:00bd403e0742 11 DRV2605 haptics(p9, p10);
jmalone37 0:00bd403e0742 12
jmalone37 0:00bd403e0742 13 Timeout cycle;
jmalone37 0:00bd403e0742 14
jmalone37 0:00bd403e0742 15 volatile int half_cycle_time = 1;
jmalone37 0:00bd403e0742 16
jmalone37 0:00bd403e0742 17 //two calls to this interrupt routine generates a square wave
jmalone37 0:00bd403e0742 18 void toggle_interrupt()
jmalone37 0:00bd403e0742 19 {
jmalone37 0:00bd403e0742 20 if (half_cycle_time>22000) haptics.play_waveform(0);
jmalone37 0:00bd403e0742 21 else {
jmalone37 0:00bd403e0742 22 static int e1 = 1;
jmalone37 0:00bd403e0742 23 haptics.play_waveform(e1);
jmalone37 0:00bd403e0742 24 e1+=10;
jmalone37 0:00bd403e0742 25 if(e1 > 121) e1 = 1;
jmalone37 0:00bd403e0742 26 }
jmalone37 0:00bd403e0742 27 led = !led;
jmalone37 0:00bd403e0742 28
jmalone37 0:00bd403e0742 29 cycle.detach();
jmalone37 0:00bd403e0742 30 //update time for interrupt activation -change frequency of square wave
jmalone37 0:00bd403e0742 31 cycle.attach_us(&toggle_interrupt, half_cycle_time);
jmalone37 0:00bd403e0742 32 }
jmalone37 0:00bd403e0742 33 void newdist(int distance)
jmalone37 0:00bd403e0742 34 {
jmalone37 0:00bd403e0742 35 //update frequency based on new sonar data
jmalone37 0:00bd403e0742 36 led2 = !led2;
jmalone37 0:00bd403e0742 37 half_cycle_time = distance<<3;
jmalone37 0:00bd403e0742 38 }
jmalone37 0:00bd403e0742 39 //HC-SR04 Sonar module
jmalone37 0:00bd403e0742 40 ultrasonic mu(p7, p8, .07, 1, &newdist);
jmalone37 0:00bd403e0742 41 //Set the trigger pin to p6 and the echo pin to p7
jmalone37 0:00bd403e0742 42 //have updates every .07 seconds and a timeout after 1
jmalone37 0:00bd403e0742 43 //second, and call newdist when the distance changes
jmalone37 0:00bd403e0742 44
jmalone37 0:00bd403e0742 45 int main()
jmalone37 0:00bd403e0742 46 {
jmalone37 0:00bd403e0742 47 audio = 0;
jmalone37 0:00bd403e0742 48 led = 0;
jmalone37 0:00bd403e0742 49 cycle.attach(&toggle_interrupt, half_cycle_time);
jmalone37 0:00bd403e0742 50 mu.startUpdates();//start measuring the distance with the sonar
jmalone37 0:00bd403e0742 51 while(1) {
jmalone37 0:00bd403e0742 52 //Do something else here
jmalone37 0:00bd403e0742 53 mu.checkDistance();
jmalone37 0:00bd403e0742 54 //call checkDistance() as much as possible, as this is where
jmalone37 0:00bd403e0742 55 //the class checks if dist needs to be called.
jmalone37 0:00bd403e0742 56 }
jmalone37 0:00bd403e0742 57 }