10/29/20 12:23

Dependencies:   mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library

Committer:
jmalone37
Date:
Fri Oct 30 19:24:27 2020 +0000
Revision:
1:fbdfdb9ecbd2
Parent:
0:00bd403e0742
Child:
2:35ef0b31a857
10/30/2020 3:24

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