Tadao Iida
/
e-ruler
mbed/ARM 活用事例 第5章 赤外線距離センサを使う mbedで初めてのマイコン開発 その6 センサを使ってみよう<2> 赤外線センサを使って赤外線距離計を製作するプログラムです。
main.cpp@1:7de58ea468a1, 2011-10-04 (annotated)
- Committer:
- sunifu
- Date:
- Tue Oct 04 13:22:41 2011 +0000
- Revision:
- 1:7de58ea468a1
- Parent:
- 0:635c06588089
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:635c06588089 | 1 | #include "mbed.h" |
sunifu | 0:635c06588089 | 2 | #include "TextLCD.h" |
sunifu | 0:635c06588089 | 3 | |
sunifu | 0:635c06588089 | 4 | AnalogIn in(p20); |
sunifu | 0:635c06588089 | 5 | AnalogOut out(p18) ; |
sunifu | 0:635c06588089 | 6 | TextLCD lcd(p24, p26, p27, p28, p29, p30) ; |
sunifu | 0:635c06588089 | 7 | Ticker input; |
sunifu | 0:635c06588089 | 8 | float now, old = 0.0 ; |
sunifu | 0:635c06588089 | 9 | |
sunifu | 0:635c06588089 | 10 | void run(void); |
sunifu | 0:635c06588089 | 11 | int main() { |
sunifu | 0:635c06588089 | 12 | input.attach(&run,0.5); |
sunifu | 0:635c06588089 | 13 | while(1) { |
sunifu | 0:635c06588089 | 14 | |
sunifu | 0:635c06588089 | 15 | } |
sunifu | 0:635c06588089 | 16 | } |
sunifu | 0:635c06588089 | 17 | |
sunifu | 0:635c06588089 | 18 | void run(void) |
sunifu | 0:635c06588089 | 19 | { |
sunifu | 0:635c06588089 | 20 | float data; |
sunifu | 0:635c06588089 | 21 | old = now ; |
sunifu | 0:635c06588089 | 22 | now = in ; |
sunifu | 0:635c06588089 | 23 | |
sunifu | 0:635c06588089 | 24 | data = (now +old)/2.0 ; |
sunifu | 0:635c06588089 | 25 | lcd.locate(0,0) ; |
sunifu | 0:635c06588089 | 26 | if (0.121 <= data && data <= 0.970){ |
sunifu | 1:7de58ea468a1 | 27 | float range = 25.33 * pow((data*3.3),-1.21); |
sunifu | 0:635c06588089 | 28 | lcd.printf("Range: %4.1f[cm]",range) ; |
sunifu | 0:635c06588089 | 29 | out = 0.5; |
sunifu | 0:635c06588089 | 30 | }else{ |
sunifu | 0:635c06588089 | 31 | lcd.printf("Range: ----[cm]"); |
sunifu | 0:635c06588089 | 32 | out = 1.0; |
sunifu | 0:635c06588089 | 33 | } |
sunifu | 0:635c06588089 | 34 | } |