파이썬만 보여서 대충 바꿔봄.
대충.. 2cm ~ 400cm로 제한을 줘야하나..
데이터 시트는 아래 링크 참조해서 작성
[링크 : https://docs.google.com/document/d/1Y-yZnNhMYy7rwhAgyL_pfa39RsB-x2qR4vP8saG73rE/edit]
시간재는건 아래 참조
[링크 : http://hwangdo83.tistory.com/123]
$ cat sr04.c /* * dht11.c: * Simple test program to test the wiringPi functions * DHT11 test */ #include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/time.h> #define TRIG_PIN 4 #define ECHO_PIN 5 void read_sr04_dat() { int counter = 0; struct timeval start_point, end_point; double operating_time; double len; pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); digitalWrite(TRIG_PIN, LOW); // delay(500); // 500 msec digitalWrite(TRIG_PIN, HIGH); delayMicroseconds( 10 ); digitalWrite(TRIG_PIN, LOW); while ( digitalRead(ECHO_PIN) == LOW) { counter++; delayMicroseconds( 1 ); if ( counter == 38000 ) { return; } } gettimeofday(&start_point, NULL); while ( digitalRead(ECHO_PIN) == HIGH) { counter++; delayMicroseconds( 1 ); if ( counter == 38000 ) { return; } } gettimeofday(&end_point, NULL); operating_time = (double)(end_point.tv_sec)+(double)(end_point.tv_usec)/1000000.0-(double)(start_point.tv_sec)-(double)(start_point.tv_usec)/1000000.0; len = operating_time * 34300 / 2; printf("%f sec %4.2f cm %d\n",operating_time, len, counter); } int main( void ) { if ( wiringPiSetup() == -1 ) exit( 1 ); while ( 1 ) { read_sr04_dat(); delay( 100 ); /* wait 1sec to refresh */ } return(0); |
한번 보냈는데 못 재면 이상한 값을 뱉는건 왜그럴까나..
'embeded > raspberry pi' 카테고리의 다른 글
redmine 백업 (0) | 2016.07.14 |
---|---|
초음파 거리 + 온/습도 센서 (0) | 2016.07.10 |
초음파/온습도 센서 데이터 시트 복사 (0) | 2016.07.10 |
rpi gstreamer mux (0) | 2016.06.28 |
beowulf / openMPI (0) | 2016.06.28 |