Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int y,old_y;
- double duration = 0; //パルスが行って帰ってくるまでの時間
- double distance = 0; //距離
- void setup() {
- Serial.begin( 9600 ); //デバッグのため
- //超音波センサのピン
- pinMode(3, INPUT);
- pinMode(2, OUTPUT);
- }
- void loop() {
- digitalWrite(2, LOW);
- delayMicroseconds(2);
- digitalWrite(2, HIGH); //超音波出力、START
- delayMicroseconds( 10 );
- digitalWrite(2, LOW); //超音波出力、STOP
- duration = pulseIn(3, HIGH);
- if (duration > 0) {
- duration = duration/2;
- distance = duration*340*100/1000000;
- y = 0.9 * old_y + 0.1 * distance;
- Serial.println(y);
- old_y = y;
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement