10/29/20 12:23

Dependencies:   mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library

Committer:
jmalone37
Date:
Mon Nov 02 18:17:47 2020 +0000
Revision:
4:111b818ede8c
Parent:
3:ab0d9d3ae4d4
11/2/2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmalone37 0:00bd403e0742 1
jmalone37 4:111b818ede8c 2 //Theremin style demo using HC-SR04 Sonar and vibrating motors
jmalone37 4:111b818ede8c 3 // moving a hand away/towards sonar changes vibrational frequency
jmalone37 0:00bd403e0742 4 #include "mbed.h"
jmalone37 0:00bd403e0742 5 #include "ultrasonic.h"
jmalone37 0:00bd403e0742 6 #include "DRV2605.h"
jmalone37 4:111b818ede8c 7 //#include "hcsr04.h"
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 3:ab0d9d3ae4d4 13 bool isERM = true;
jmalone37 3:ab0d9d3ae4d4 14 DRV2605 hap1(i2c1);
jmalone37 3:ab0d9d3ae4d4 15 DRV2605 hap2(i2c2);
jmalone37 4:111b818ede8c 16 //HCSR04 sensor1(p7, p8);
jmalone37 3:ab0d9d3ae4d4 17
jmalone37 1:fbdfdb9ecbd2 18
jmalone37 4:111b818ede8c 19 //Timeout cycle;
jmalone37 0:00bd403e0742 20
jmalone37 3:ab0d9d3ae4d4 21 //ultrasonic mu(p7, p8, .07, 1, &newdist);
jmalone37 4:111b818ede8c 22 //volatile int half_cycle_time = 1;
jmalone37 0:00bd403e0742 23
jmalone37 0:00bd403e0742 24 //two calls to this interrupt routine generates a square wave
jmalone37 4:111b818ede8c 25 //void toggle_interrupt()
jmalone37 4:111b818ede8c 26 //{
jmalone37 3:ab0d9d3ae4d4 27 // getCurrentDistance(0)=distance;
jmalone37 4:111b818ede8c 28
jmalone37 2:35ef0b31a857 29
jmalone37 2:35ef0b31a857 30 /*if (half_cycle_time>22000) haptics.play_waveform(0);
jmalone37 0:00bd403e0742 31 else {
jmalone37 0:00bd403e0742 32 static int e1 = 1;
jmalone37 0:00bd403e0742 33 haptics.play_waveform(e1);
jmalone37 0:00bd403e0742 34 e1+=10;
jmalone37 0:00bd403e0742 35 if(e1 > 121) e1 = 1;
jmalone37 0:00bd403e0742 36 }
jmalone37 2:35ef0b31a857 37 led = !led;*/
jmalone37 0:00bd403e0742 38
jmalone37 4:111b818ede8c 39 //cycle.detach();
jmalone37 0:00bd403e0742 40 //update time for interrupt activation -change frequency of square wave
jmalone37 4:111b818ede8c 41 //cycle.attach_us(&toggle_interrupt, half_cycle_time);
jmalone37 4:111b818ede8c 42 //}
jmalone37 2:35ef0b31a857 43
jmalone37 2:35ef0b31a857 44 /*
jmalone37 0:00bd403e0742 45 void newdist(int distance)
jmalone37 0:00bd403e0742 46 {
jmalone37 0:00bd403e0742 47 //update frequency based on new sonar data
jmalone37 0:00bd403e0742 48 led2 = !led2;
jmalone37 0:00bd403e0742 49 half_cycle_time = distance<<3;
jmalone37 0:00bd403e0742 50 }
jmalone37 2:35ef0b31a857 51 */
jmalone37 0:00bd403e0742 52 //HC-SR04 Sonar module
jmalone37 3:ab0d9d3ae4d4 53 // ultrasonic mu(p7, p8, .07, 1, &newdist);
jmalone37 0:00bd403e0742 54 //Set the trigger pin to p6 and the echo pin to p7
jmalone37 0:00bd403e0742 55 //have updates every .07 seconds and a timeout after 1
jmalone37 0:00bd403e0742 56 //second, and call newdist when the distance changes
jmalone37 4:111b818ede8c 57
jmalone37 4:111b818ede8c 58 void dist(int distance)
jmalone37 4:111b818ede8c 59 {
jmalone37 4:111b818ede8c 60 //put code here to execute when the distance has changed
jmalone37 4:111b818ede8c 61 if (distance > 10) {
jmalone37 4:111b818ede8c 62 hap1.setWaveform(0);
jmalone37 4:111b818ede8c 63 hap1.playRtp(10);
jmalone37 4:111b818ede8c 64 } else if (distance > 25) {
jmalone37 4:111b818ede8c 65 hap1.setWaveform(0);
jmalone37 4:111b818ede8c 66 hap1.playRtp(25);
jmalone37 4:111b818ede8c 67 }
jmalone37 4:111b818ede8c 68 //printf("Distance %d mm\r\n", distance);
jmalone37 4:111b818ede8c 69 }
jmalone37 4:111b818ede8c 70
jmalone37 4:111b818ede8c 71 ultrasonic mu(p7, p8, .1, 1, &dist); //Set the trigger pin to p6 and the echo pin to p7
jmalone37 4:111b818ede8c 72 //have updates every .1 seconds and a timeout after 1
jmalone37 4:111b818ede8c 73 //second, and call dist when the distance changes
jmalone37 0:00bd403e0742 74
jmalone37 0:00bd403e0742 75 int main()
jmalone37 0:00bd403e0742 76 {
jmalone37 4:111b818ede8c 77 mu.startUpdates();//start measuring the distance
jmalone37 4:111b818ede8c 78 while(1)
jmalone37 4:111b818ede8c 79 {
jmalone37 0:00bd403e0742 80 //Do something else here
jmalone37 4:111b818ede8c 81 mu.checkDistance(); //call checkDistance() as much as possible, as this is where
jmalone37 4:111b818ede8c 82 //the class checks if dist needs to be called.
jmalone37 0:00bd403e0742 83 }
jmalone37 4:111b818ede8c 84 }
jmalone37 4:111b818ede8c 85
jmalone37 4:111b818ede8c 86 /*int main()
jmalone37 4:111b818ede8c 87 {
jmalone37 4:111b818ede8c 88 while(1) {
jmalone37 4:111b818ede8c 89
jmalone37 4:111b818ede8c 90 long distance = sensor1.distance();
jmalone37 4:111b818ede8c 91 if (distance > 10) {
jmalone37 4:111b818ede8c 92 hap1.setWaveform(0);
jmalone37 4:111b818ede8c 93 hap1.playRtp(10);
jmalone37 4:111b818ede8c 94 } else if (distance > 25) {
jmalone37 4:111b818ede8c 95 hap1.setWaveform(0);
jmalone37 4:111b818ede8c 96 hap1.playRtp(100);
jmalone37 4:111b818ede8c 97 }
jmalone37 4:111b818ede8c 98 //printf("distance %d \n",distance);
jmalone37 4:111b818ede8c 99 // wait(1.0); // 1 sec
jmalone37 4:111b818ede8c 100
jmalone37 4:111b818ede8c 101 }
jmalone37 4:111b818ede8c 102 } */