![](/media/cache/group/DffTeam_Yf25WvB.jpg.50x50_q85.jpg)
Esercitazione sensore ultrasuoni
Dependencies: HCSR04 TextLCD mbed
main.cpp@0:182c2bb05bb1, 2016-10-24 (annotated)
- Committer:
- dfalanga
- Date:
- Mon Oct 24 10:52:06 2016 +0000
- Revision:
- 0:182c2bb05bb1
Esercitazione 4 - 4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dfalanga | 0:182c2bb05bb1 | 1 | #include "mbed.h" |
dfalanga | 0:182c2bb05bb1 | 2 | #include "TextLCD.h" |
dfalanga | 0:182c2bb05bb1 | 3 | #include "HCSR04.h" |
dfalanga | 0:182c2bb05bb1 | 4 | TextLCD lcd(D12, D11, D5, D4, D3, D2, TextLCD::LCD16x2); |
dfalanga | 0:182c2bb05bb1 | 5 | DigitalOut myled(LED1); |
dfalanga | 0:182c2bb05bb1 | 6 | |
dfalanga | 0:182c2bb05bb1 | 7 | int main(){ |
dfalanga | 0:182c2bb05bb1 | 8 | lcd.printf("Distance Calculator."); |
dfalanga | 0:182c2bb05bb1 | 9 | wait(1); |
dfalanga | 0:182c2bb05bb1 | 10 | lcd.cls(); |
dfalanga | 0:182c2bb05bb1 | 11 | lcd.locate(0,0); |
dfalanga | 0:182c2bb05bb1 | 12 | HCSR04 sensor(D7, D8); //echo pin, trig pin |
dfalanga | 0:182c2bb05bb1 | 13 | sensor.setRanges(1, 150); |
dfalanga | 0:182c2bb05bb1 | 14 | lcd.printf("Min. = %g cm", sensor.getMinRange()); |
dfalanga | 0:182c2bb05bb1 | 15 | lcd.locate(0,1); |
dfalanga | 0:182c2bb05bb1 | 16 | lcd.printf("Max. = %g cm",sensor.getMaxRange()); |
dfalanga | 0:182c2bb05bb1 | 17 | wait(2); |
dfalanga | 0:182c2bb05bb1 | 18 | lcd.cls(); |
dfalanga | 0:182c2bb05bb1 | 19 | lcd.locate(0,0); |
dfalanga | 0:182c2bb05bb1 | 20 | lcd.printf("Distance: "); |
dfalanga | 0:182c2bb05bb1 | 21 | while(1){ |
dfalanga | 0:182c2bb05bb1 | 22 | lcd.locate(0,1); |
dfalanga | 0:182c2bb05bb1 | 23 | lcd.printf("%5.3f cm", sensor.getDistance_cm()); |
dfalanga | 0:182c2bb05bb1 | 24 | wait_ms(1000); |
dfalanga | 0:182c2bb05bb1 | 25 | lcd.locate(0,1); |
dfalanga | 0:182c2bb05bb1 | 26 | lcd.printf(" "); |
dfalanga | 0:182c2bb05bb1 | 27 | } |
dfalanga | 0:182c2bb05bb1 | 28 | } |