10/29/20 12:23

Dependencies:   mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library

Committer:
jmalone37
Date:
Fri Oct 30 21:03:24 2020 +0000
Revision:
2:35ef0b31a857
Parent:
1:fbdfdb9ecbd2
Child:
3:ab0d9d3ae4d4
Updated 10/30/2020

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