![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
semi integrated
Dependencies: Speaker mbed 4DGL-uLCD-SE hcsr04
main.cpp@9:d1b00a2a8e25, 2020-12-16 (annotated)
- Committer:
- fiachra
- Date:
- Wed Dec 16 12:11:32 2020 +0000
- Revision:
- 9:d1b00a2a8e25
- Parent:
- 8:f3ab61374cd0
- Child:
- 10:f29ed3675509
Distance Sensor is working but LCD not yet implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tadhgjones | 8:f3ab61374cd0 | 1 | #include "mbed.h" |
tadhgjones | 8:f3ab61374cd0 | 2 | #include "hcsr04.h" |
tadhgjones | 8:f3ab61374cd0 | 3 | #include "Speaker.h" |
tadhgjones | 8:f3ab61374cd0 | 4 | |
tadhgjones | 8:f3ab61374cd0 | 5 | HCSR04 usensor(p25,p6); |
tadhgjones | 8:f3ab61374cd0 | 6 | Serial pc(USBTX, USBRX); |
tadhgjones | 8:f3ab61374cd0 | 7 | Speaker mySpeaker(p18); |
tadhgjones | 8:f3ab61374cd0 | 8 | |
tadhgjones | 8:f3ab61374cd0 | 9 | unsigned int dist; |
fiachra | 9:d1b00a2a8e25 | 10 | |
fiachra | 9:d1b00a2a8e25 | 11 | float averageDistance(); |
fiachra | 9:d1b00a2a8e25 | 12 | void beeping(); |
fiachra | 9:d1b00a2a8e25 | 13 | float i; |
fiachra | 9:d1b00a2a8e25 | 14 | |
tadhgjones | 8:f3ab61374cd0 | 15 | int main() |
tadhgjones | 8:f3ab61374cd0 | 16 | { |
tadhgjones | 8:f3ab61374cd0 | 17 | |
tadhgjones | 8:f3ab61374cd0 | 18 | while(1) { |
tadhgjones | 8:f3ab61374cd0 | 19 | usensor.start(); |
tadhgjones | 8:f3ab61374cd0 | 20 | |
fiachra | 9:d1b00a2a8e25 | 21 | averageDistance(); |
tadhgjones | 8:f3ab61374cd0 | 22 | |
fiachra | 9:d1b00a2a8e25 | 23 | beeping(); |
tadhgjones | 8:f3ab61374cd0 | 24 | pc.printf("cm:%ld\n",dist ); |
fiachra | 9:d1b00a2a8e25 | 25 | wait_ms(50); |
fiachra | 9:d1b00a2a8e25 | 26 | } |
fiachra | 9:d1b00a2a8e25 | 27 | } |
fiachra | 9:d1b00a2a8e25 | 28 | float averageDistance() /** Divide the running total by 10 to get the average distance to get more accurate readings on the sensor**/ |
fiachra | 9:d1b00a2a8e25 | 29 | { |
fiachra | 9:d1b00a2a8e25 | 30 | int a = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 31 | int b = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 32 | int c = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 33 | int d = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 34 | int e = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 35 | int f = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 36 | int g = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 37 | int h = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 38 | int i = usensor.get_dist_cm(); |
fiachra | 9:d1b00a2a8e25 | 39 | int j = usensor.get_dist_cm(); |
tadhgjones | 8:f3ab61374cd0 | 40 | |
fiachra | 9:d1b00a2a8e25 | 41 | dist = (a+b+c+d+e+f+g+h+i+j)/10; |
fiachra | 9:d1b00a2a8e25 | 42 | return dist; |
fiachra | 9:d1b00a2a8e25 | 43 | } |
fiachra | 9:d1b00a2a8e25 | 44 | void beeping() |
fiachra | 9:d1b00a2a8e25 | 45 | /* |
fiachra | 9:d1b00a2a8e25 | 46 | @Brief Function to create the effect of varying beeping sounds depending on how close the sensor is to an object interspersed with |
fiachra | 9:d1b00a2a8e25 | 47 | the led changing color depending on the distance |
fiachra | 9:d1b00a2a8e25 | 48 | */ |
fiachra | 9:d1b00a2a8e25 | 49 | { |
fiachra | 9:d1b00a2a8e25 | 50 | int distance = averageDistance(); |
fiachra | 9:d1b00a2a8e25 | 51 | |
fiachra | 9:d1b00a2a8e25 | 52 | if (distance > 2000) i=0.05; //sensor defaults to 2k+ when dist<~3cm |
fiachra | 9:d1b00a2a8e25 | 53 | if (distance < 150) i=0.6; |
fiachra | 9:d1b00a2a8e25 | 54 | if (distance < 100) i=0.55; |
fiachra | 9:d1b00a2a8e25 | 55 | if (distance < 90) i=0.5; |
fiachra | 9:d1b00a2a8e25 | 56 | if (distance < 80) i=0.45; |
fiachra | 9:d1b00a2a8e25 | 57 | if (distance < 70) i=0.4; |
fiachra | 9:d1b00a2a8e25 | 58 | if (distance < 60) i=0.35; |
fiachra | 9:d1b00a2a8e25 | 59 | if (distance < 50) i=0.3; |
fiachra | 9:d1b00a2a8e25 | 60 | if (distance < 40) i=0.25; |
fiachra | 9:d1b00a2a8e25 | 61 | if (distance < 30) i=0.2; |
fiachra | 9:d1b00a2a8e25 | 62 | if (distance < 20) i=0.15; |
fiachra | 9:d1b00a2a8e25 | 63 | if (distance < 10) i=0.1; |
fiachra | 9:d1b00a2a8e25 | 64 | if (distance < 5) i=0.05; |
fiachra | 9:d1b00a2a8e25 | 65 | mySpeaker.PlayNote(329.63, i, 1.0); |
fiachra | 9:d1b00a2a8e25 | 66 | |
tadhgjones | 8:f3ab61374cd0 | 67 | } |