l
Dependencies: mbed Pulse LM61CIZ
main.cpp@0:a183af1b1216, 2019-09-02 (annotated)
- Committer:
- Ryosei
- Date:
- Mon Sep 02 12:47:53 2019 +0000
- Revision:
- 0:a183af1b1216
k;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ryosei | 0:a183af1b1216 | 1 | #include "mbed.h" |
Ryosei | 0:a183af1b1216 | 2 | #include "Pulse.h" |
Ryosei | 0:a183af1b1216 | 3 | #include "LM61CIZ.h" |
Ryosei | 0:a183af1b1216 | 4 | #define echoPin D4 |
Ryosei | 0:a183af1b1216 | 5 | #define trigPin D5 |
Ryosei | 0:a183af1b1216 | 6 | #define TempPin A0 |
Ryosei | 0:a183af1b1216 | 7 | |
Ryosei | 0:a183af1b1216 | 8 | Serial pc(USBTX,USBRX); |
Ryosei | 0:a183af1b1216 | 9 | AnalogIn temptemp(A0); |
Ryosei | 0:a183af1b1216 | 10 | PulseInOut echo(echoPin); |
Ryosei | 0:a183af1b1216 | 11 | PulseInOut trig(trigPin); |
Ryosei | 0:a183af1b1216 | 12 | LM61CIZ Temp(TempPin); |
Ryosei | 0:a183af1b1216 | 13 | |
Ryosei | 0:a183af1b1216 | 14 | long map(long x, long in_min, long in_max, long out_min, long out_max) { |
Ryosei | 0:a183af1b1216 | 15 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
Ryosei | 0:a183af1b1216 | 16 | } |
Ryosei | 0:a183af1b1216 | 17 | |
Ryosei | 0:a183af1b1216 | 18 | double Duration = 0; |
Ryosei | 0:a183af1b1216 | 19 | double Distance=0; |
Ryosei | 0:a183af1b1216 | 20 | |
Ryosei | 0:a183af1b1216 | 21 | |
Ryosei | 0:a183af1b1216 | 22 | int main(){ |
Ryosei | 0:a183af1b1216 | 23 | pc.baud(9600); |
Ryosei | 0:a183af1b1216 | 24 | while(1){ |
Ryosei | 0:a183af1b1216 | 25 | int temp=Temp.getTemperature(); |
Ryosei | 0:a183af1b1216 | 26 | trig.write_us(1,10); |
Ryosei | 0:a183af1b1216 | 27 | Duration=echo.read_high_us(5000); |
Ryosei | 0:a183af1b1216 | 28 | if(Duration>0){ |
Ryosei | 0:a183af1b1216 | 29 | Duration=Duration/2; |
Ryosei | 0:a183af1b1216 | 30 | double sspead=331.5+0.6*temp; |
Ryosei | 0:a183af1b1216 | 31 | Distance=Duration*sspead*100/1000000; |
Ryosei | 0:a183af1b1216 | 32 | pc.printf("%d %lfm\n",temp,Distance); |
Ryosei | 0:a183af1b1216 | 33 | } |
Ryosei | 0:a183af1b1216 | 34 | wait(0.01); |
Ryosei | 0:a183af1b1216 | 35 | } |
Ryosei | 0:a183af1b1216 | 36 | } |