10/29/20 12:23
Dependencies: mbed DRV2605 HCSR04 HC_SR04_Ultrasonic_Library
main.cpp@3:ab0d9d3ae4d4, 2020-10-31 (annotated)
- Committer:
- jmalone37
- Date:
- Sat Oct 31 18:44:09 2020 +0000
- Revision:
- 3:ab0d9d3ae4d4
- Parent:
- 2:35ef0b31a857
- Child:
- 4:111b818ede8c
10/31/2020
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:ab0d9d3ae4d4 | 13 | bool isERM = true; |
jmalone37 | 3:ab0d9d3ae4d4 | 14 | DRV2605 hap1(i2c1); |
jmalone37 | 3:ab0d9d3ae4d4 | 15 | DRV2605 hap2(i2c2); |
jmalone37 | 3:ab0d9d3ae4d4 | 16 | |
jmalone37 | 1:fbdfdb9ecbd2 | 17 | |
jmalone37 | 0:00bd403e0742 | 18 | |
jmalone37 | 0:00bd403e0742 | 19 | Timeout cycle; |
jmalone37 | 0:00bd403e0742 | 20 | |
jmalone37 | 3:ab0d9d3ae4d4 | 21 | //ultrasonic mu(p7, p8, .07, 1, &newdist); |
jmalone37 | 0:00bd403e0742 | 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 | 0:00bd403e0742 | 25 | void toggle_interrupt() |
jmalone37 | 0:00bd403e0742 | 26 | { |
jmalone37 | 3:ab0d9d3ae4d4 | 27 | // getCurrentDistance(0)=distance; |
jmalone37 | 2:35ef0b31a857 | 28 | if (distance > 10) { |
jmalone37 | 2:35ef0b31a857 | 29 | hap1.setWaveform(0); |
jmalone37 | 2:35ef0b31a857 | 30 | hap1.playRtp(10); |
jmalone37 | 2:35ef0b31a857 | 31 | } else if (distance > 50) { |
jmalone37 | 2:35ef0b31a857 | 32 | hap1.seWaveform(0); |
jmalone37 | 2:35ef0b31a857 | 33 | hap1.playRtp(50); |
jmalone37 | 2:35ef0b31a857 | 34 | } |
jmalone37 | 2:35ef0b31a857 | 35 | |
jmalone37 | 2:35ef0b31a857 | 36 | /*if (half_cycle_time>22000) haptics.play_waveform(0); |
jmalone37 | 0:00bd403e0742 | 37 | else { |
jmalone37 | 0:00bd403e0742 | 38 | static int e1 = 1; |
jmalone37 | 0:00bd403e0742 | 39 | haptics.play_waveform(e1); |
jmalone37 | 0:00bd403e0742 | 40 | e1+=10; |
jmalone37 | 0:00bd403e0742 | 41 | if(e1 > 121) e1 = 1; |
jmalone37 | 0:00bd403e0742 | 42 | } |
jmalone37 | 2:35ef0b31a857 | 43 | led = !led;*/ |
jmalone37 | 0:00bd403e0742 | 44 | |
jmalone37 | 0:00bd403e0742 | 45 | cycle.detach(); |
jmalone37 | 0:00bd403e0742 | 46 | //update time for interrupt activation -change frequency of square wave |
jmalone37 | 0:00bd403e0742 | 47 | cycle.attach_us(&toggle_interrupt, half_cycle_time); |
jmalone37 | 0:00bd403e0742 | 48 | } |
jmalone37 | 2:35ef0b31a857 | 49 | |
jmalone37 | 2:35ef0b31a857 | 50 | /* |
jmalone37 | 0:00bd403e0742 | 51 | void newdist(int distance) |
jmalone37 | 0:00bd403e0742 | 52 | { |
jmalone37 | 0:00bd403e0742 | 53 | //update frequency based on new sonar data |
jmalone37 | 0:00bd403e0742 | 54 | led2 = !led2; |
jmalone37 | 0:00bd403e0742 | 55 | half_cycle_time = distance<<3; |
jmalone37 | 0:00bd403e0742 | 56 | } |
jmalone37 | 2:35ef0b31a857 | 57 | */ |
jmalone37 | 0:00bd403e0742 | 58 | //HC-SR04 Sonar module |
jmalone37 | 3:ab0d9d3ae4d4 | 59 | // ultrasonic mu(p7, p8, .07, 1, &newdist); |
jmalone37 | 0:00bd403e0742 | 60 | //Set the trigger pin to p6 and the echo pin to p7 |
jmalone37 | 0:00bd403e0742 | 61 | //have updates every .07 seconds and a timeout after 1 |
jmalone37 | 0:00bd403e0742 | 62 | //second, and call newdist when the distance changes |
jmalone37 | 0:00bd403e0742 | 63 | |
jmalone37 | 0:00bd403e0742 | 64 | int main() |
jmalone37 | 0:00bd403e0742 | 65 | { |
jmalone37 | 1:fbdfdb9ecbd2 | 66 | //audio = 0; |
jmalone37 | 1:fbdfdb9ecbd2 | 67 | //led = 0; |
jmalone37 | 0:00bd403e0742 | 68 | cycle.attach(&toggle_interrupt, half_cycle_time); |
jmalone37 | 0:00bd403e0742 | 69 | mu.startUpdates();//start measuring the distance with the sonar |
jmalone37 | 0:00bd403e0742 | 70 | while(1) { |
jmalone37 | 0:00bd403e0742 | 71 | //Do something else here |
jmalone37 | 0:00bd403e0742 | 72 | mu.checkDistance(); |
jmalone37 | 0:00bd403e0742 | 73 | //call checkDistance() as much as possible, as this is where |
jmalone37 | 0:00bd403e0742 | 74 | //the class checks if dist needs to be called. |
jmalone37 | 0:00bd403e0742 | 75 | } |
jmalone37 | 0:00bd403e0742 | 76 | } |